diff --git a/integration_tests/gettxs_test.go b/integration_tests/gettxs_test.go index fc345ad..a11fbc4 100644 --- a/integration_tests/gettxs_test.go +++ b/integration_tests/gettxs_test.go @@ -91,8 +91,13 @@ func (suite *GetTxTestSuite) SetupSuite() { suite.userToken = userTokens[0] } -func (suite *GetTxTestSuite) TearDownSuite() { +func (suite *GetTxTestSuite) TearDownSuite() {} +func (suite *GetTxTestSuite) TearDownTest() { + err := clearTable("invoices", suite.Service.Config.DatabaseUri) + if err != nil { + fmt.Printf("Error tearing down test %s\n", err.Error()) + } } func (suite *GetTxTestSuite) TestGetOutgoingInvoices() { diff --git a/integration_tests/util.go b/integration_tests/util.go index 0d726a3..51ae24f 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -26,9 +26,9 @@ import ( func LndHubTestServiceInit() (svc *service.LndhubService, err error) { // change this if you want to run tests using sqlite - //dbUri := "file:data_test.db" + dbUri := "file:data_test.db" //make sure the datbase is empty every time you run the test suite - dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable" + //dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable" c := &service.Config{ DatabaseUri: dbUri, JWTSecret: []byte("SECRET"), @@ -84,6 +84,20 @@ func LndHubTestServiceInit() (svc *service.LndhubService, err error) { return svc, nil } +// simple method to use in tear down test, there might be a better way +func clearTable(table string, dbUri string) error { + dbConn, err := db.Open(dbUri) + if err != nil { + return fmt.Errorf("failed to connect to database: %w", err) + } + + _, err = dbConn.Exec(fmt.Sprintf("DELETE FROM %s", table)) + if err != nil { + return fmt.Errorf("failed to clear table: %w", err) + } + return nil +} + func createUsers(svc *service.LndhubService, usersToCreate int) (logins []controllers.CreateUserResponseBody, tokens []string, err error) { logins = []controllers.CreateUserResponseBody{} tokens = []string{}