package handlers_test import ( "fmt" "net/http" "testing" "otaupdate/handlers" "otaupdate/services" "github.com/fiskerinc/cloud-services/pkg/db/queries/mocks" "github.com/fiskerinc/cloud-services/pkg/httpclient/tester" th "github.com/fiskerinc/cloud-services/pkg/testhelper" "github.com/fiskerinc/cloud-services/pkg/testrunner" ) func TestSupplierDelete(t *testing.T) { route := "/supplier/:email" results := mocks.MockORMResults{ ReturnedRows: 0, AffectedRows: 1, } mock := mocks.MockSupplierAccounts{ DBMockHelper: mocks.DBMockHelper{ ORMResponse: &results, }, } services.GetDB().SetSupplierAccount(&mock) tests := []testrunner.TestCase{ { Name: "Record not found", HttpTestCase: &tester.HttpTestCase{ Request: th.MakeTestRequest(http.MethodDelete, "http://example.com/supplier/no@supplier.com", nil), ExpectedStatus: http.StatusNotFound, ExpectedResponse: `{"message":"Nothing deleted","error":"Not Found"}`, }, DBTestCase: &mocks.DBTestCase{ SetupMockResponse: func() { results.AffectedRows = 0 }, }, }, { Name: "No id", HttpTestCase: &tester.HttpTestCase{ Request: th.MakeTestRequest(http.MethodDelete, "http://example.com/supplier", nil), ExpectedStatus: http.StatusNotFound, ExpectedResponse: err404NotFound, }, }, { Name: "Good id", HttpTestCase: &tester.HttpTestCase{ Request: th.MakeTestRequest(http.MethodDelete, "http://example.com/supplier/test@supplier.com", nil), ExpectedStatus: http.StatusOK, ExpectedResponse: `{"message":"Deleted"}`, }, DBTestCase: &mocks.DBTestCase{ SetupMockResponse: func() { results.AffectedRows = 1 }, }, }, { Name: "DB error", HttpTestCase: &tester.HttpTestCase{ Request: th.MakeTestRequest(http.MethodDelete, "http://example.com/supplier/test@supplier.com", nil), ExpectedStatus: http.StatusServiceUnavailable, ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`, }, DBTestCase: &mocks.DBTestCase{ MockError: fmt.Errorf("something went wrong"), SetupMockResponse: func() { results.AffectedRows = 1 }, }, }, } for _, test := range tests { if test.DBTestCase != nil { test.DBTestCase.SetupDB(&mock) } w := test.HttpTestCase.TestWithParamPath(handlers.HandleSupplierDelete, route) test.HttpTestCase.ValidateHttp(t, test.Name, w) } }