Add simple tear down test method

This commit is contained in:
Stefan Kostic
2022-02-11 12:09:30 +01:00
parent 140ee03f7c
commit 53ec45b482
2 changed files with 22 additions and 3 deletions

View File

@@ -91,8 +91,13 @@ func (suite *GetTxTestSuite) SetupSuite() {
suite.userToken = userTokens[0] 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() { func (suite *GetTxTestSuite) TestGetOutgoingInvoices() {

View File

@@ -26,9 +26,9 @@ import (
func LndHubTestServiceInit() (svc *service.LndhubService, err error) { func LndHubTestServiceInit() (svc *service.LndhubService, err error) {
// change this if you want to run tests using sqlite // 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 //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{ c := &service.Config{
DatabaseUri: dbUri, DatabaseUri: dbUri,
JWTSecret: []byte("SECRET"), JWTSecret: []byte("SECRET"),
@@ -84,6 +84,20 @@ func LndHubTestServiceInit() (svc *service.LndhubService, err error) {
return svc, nil 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) { func createUsers(svc *service.LndhubService, usersToCreate int) (logins []controllers.CreateUserResponseBody, tokens []string, err error) {
logins = []controllers.CreateUserResponseBody{} logins = []controllers.CreateUserResponseBody{}
tokens = []string{} tokens = []string{}