From f2dc2d08f0d3eb8744bd7c7e44fd6e143c5b4d6c Mon Sep 17 00:00:00 2001 From: Ghvst Code Date: Wed, 19 May 2021 12:59:29 +0100 Subject: [PATCH] Fixed Broken Tests (#7) * Add Go-Reference badge * Refactor Operator Struct Refactor Operator Struct To Reflect Recent Changes To The Reloadly API Add More Tests To Improve Coverage * Fix broken Tests * Fix broken Tests --- airtime/operators_test.go | 145 +++++++++++++++++++++++++++++++------- 1 file changed, 121 insertions(+), 24 deletions(-) diff --git a/airtime/operators_test.go b/airtime/operators_test.go index 05d6f28..8be4a8c 100644 --- a/airtime/operators_test.go +++ b/airtime/operators_test.go @@ -63,64 +63,161 @@ func TestClient_GetOperatorsByISO(t *testing.T) { defer teardown() mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { - rw.WriteHeader(http.StatusOK) - data := reloadly.Operators{ - TotalPages: 5, - Size: 20, - } - - json.NewEncoder(rw).Encode(data) - + rw.WriteHeader(http.StatusInternalServerError) }) - body, err := client.GetOperatorsByISO("011") - if err != nil { - t.Errorf("Expected error to be nil but got %q", err) + body, err := client.GetOperatorsByISO("") + + if err == nil { + t.Errorf("Expected error but got nil") } - if body.Size != 20 { - t.Errorf("Expected Size to be 20 but got %v", body.Size) + if body != nil { + t.Errorf("Expected body to be nil but got %v", body) } } -func TestClient_GetOperators(t *testing.T) { + +func TestClient_GetOperatorsById(t *testing.T) { teardown := setup() defer teardown() - mux.HandleFunc("/operators", func(rw http.ResponseWriter, req *http.Request) { + mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(http.StatusOK) data := reloadly.Operators{ - TotalPages: 5, - Size: 20, + Name: "testOperator", } json.NewEncoder(rw).Encode(data) }) - body, err := client.GetOperators() + body, err := client.GetOperatorsById(7) if err != nil { t.Errorf("Expected error to be nil but got %q", err) } - if body.Size != 20 { - t.Errorf("Expected Size to be 20 but got %v", body.Size) + if body.Name != "testOperator" { + t.Errorf("Expected Size to be testOperator but got %v", body.Name) } } -func TestClient_GetOperatorsById(t *testing.T) { +func TestClient_GetOperators(t *testing.T) { teardown := setup() defer teardown() - mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { + mux.HandleFunc("/operators", func(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(http.StatusInternalServerError) - }) - _, err := client.GetOperators() + body, err := client.GetOperators() + if err == nil { t.Errorf("Expected error but got nil") } + + if body != nil { + t.Errorf("Expected body to be nil but got %v", body) + } +} + +func TestAddSuggestedAmounts(t *testing.T) { + cases := [] struct{ + SuggestedAmounts bool + ExpectedSuggestedAmounts bool + }{ + { + SuggestedAmounts: false, + ExpectedSuggestedAmounts: false, + }, + { + SuggestedAmounts: true, + ExpectedSuggestedAmounts: true, + }, + } + + + + for _, c := range cases { + res := reloadly.AddSuggestedAmounts(c.SuggestedAmounts) + o := &reloadly.OperatorOpts{} + res(o) + + if res != nil{ + if c.ExpectedSuggestedAmounts != o.SuggestedAmounts { + t.Fatalf("Expected SuggestedAmounts to be %t but got %t", c.SuggestedAmounts, o.SuggestedAmounts) + } + + } + + + } +} + +func TestAddBundles(t *testing.T) { + cases := [] struct{ + IncludeBundles bool + ExpectedIncludeBundles bool + }{ + { + IncludeBundles: false, + ExpectedIncludeBundles: false, + }, + { + IncludeBundles: true, + ExpectedIncludeBundles: true, + }, + } + + + + for _, c := range cases { + res := reloadly.AddBundles(c.IncludeBundles) + o := &reloadly.OperatorOpts{} + res(o) + + if res != nil{ + if c.ExpectedIncludeBundles != o.IncludeBundles { + t.Fatalf("Expected SuggestedAmounts to be %t but got %t", c.IncludeBundles, o.IncludeBundles) + } + + } + + + } +} + +func TestAddData(t *testing.T) { + cases := [] struct{ + IncludeData bool + ExpectedIncludeData bool + }{ + { + IncludeData: false, + ExpectedIncludeData: false, + }, + { + IncludeData: true, + ExpectedIncludeData: true, + }, + } + + + + for _, c := range cases { + res := reloadly.AddData(c.IncludeData) + o := &reloadly.OperatorOpts{} + res(o) + + if res != nil{ + if c.IncludeData != o.IncludeData { + t.Fatalf("Expected SuggestedAmounts to be %t but got %t", c.IncludeData, o.IncludeData) + } + + } + + + } } \ No newline at end of file