From d4f929e3db03c52147eb6504ee51bcfc690814db Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Tue, 25 Jun 2024 19:03:31 +0500 Subject: [PATCH 1/2] Refactor TestGetConnectionCode To Use A Real Postgres DB For The Test --- handlers/auth_test.go | 52 +++++++++++++++++++++++++++++++++---------- handlers/bots_test.go | 3 +-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/handlers/auth_test.go b/handlers/auth_test.go index f2e10ec9e..bd98c2745 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -139,30 +139,58 @@ func TestCreateConnectionCode(t *testing.T) { } func TestGetConnectionCode(t *testing.T) { - mockDb := mocks.NewDatabase(t) - aHandler := NewAuthHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + aHandler := NewAuthHandler(db.TestDB) t.Run("should return connection code from db", func(t *testing.T) { - creationDate, _ := time.Parse(time.RFC3339, "2000-01-01T00:00:00Z") - existingConnectionCode := db.ConnectionCodesShort{ - ConnectionString: "test", - DateCreated: &creationDate, + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(aHandler.GetConnectionCode) + + codeStrArr := []string{"sampleCode1"} + + codeArr := []db.ConnectionCodes{} + now := time.Now() + + for i, code := range codeStrArr { + code := db.ConnectionCodes{ + ID: uint(i), + ConnectionString: code, + IsUsed: false, + DateCreated: &now, + } + + codeArr = append(codeArr, code) } - mockDb.On("GetConnectionCode").Return(existingConnectionCode).Once() + + // Ensure codeArr has at least one element + codeShort := db.ConnectionCodesShort{ + ConnectionString: codeArr[0].ConnectionString, + DateCreated: codeArr[0].DateCreated, + } + + db.TestDB.CreateConnectionCode(codeArr) + req, err := http.NewRequest("GET", "/connectioncodes", nil) if err != nil { t.Fatal(err) } - rr := httptest.NewRecorder() - handler := http.HandlerFunc(aHandler.GetConnectionCode) + + fetchedCodes := db.TestDB.GetConnectionCode() handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) - expected := `{"connection_string":"test","date_created":"2000-01-01T00:00:00Z"}` - assert.EqualValues(t, expected, strings.TrimRight(rr.Body.String(), "\n")) - }) + assert.EqualValues(t, codeShort.ConnectionString, fetchedCodes.ConnectionString) + tolerance := time.Millisecond + timeDifference := codeShort.DateCreated.Sub(*fetchedCodes.DateCreated) + if timeDifference < 0 { + timeDifference = -timeDifference + } + assert.True(t, timeDifference <= tolerance, "Expected DateCreated to be within tolerance") + }) } func TestGetIsAdmin(t *testing.T) { diff --git a/handlers/bots_test.go b/handlers/bots_test.go index d13d5f080..6771951df 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -14,9 +14,8 @@ import ( "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" - dbMocks "github.com/stakwork/sphinx-tribes/mocks" + "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) func TestGetBotByUniqueName(t *testing.T) { From 66ac6e82d6df52c88ad67f6a19810698369fe500 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Tue, 25 Jun 2024 19:51:27 +0500 Subject: [PATCH 2/2] GetBot test corrected --- handlers/bots_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/handlers/bots_test.go b/handlers/bots_test.go index 6771951df..c48dec0ab 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -516,9 +516,6 @@ func TestGetBot(t *testing.T) { err = json.Unmarshal(rr.Body.Bytes(), &returnedBot) assert.Equal(t, http.StatusOK, rr.Code) - returnedBot.Tsv = "" - fetchedBot.Tsv = "" - assert.Equal(t, bot, returnedBot) assert.Equal(t, bot, fetchedBot) })