Skip to content

Commit

Permalink
Merge pull request #2367 from MahtabBukhari/DB_getconnection_code
Browse files Browse the repository at this point in the history
[Unit Tests] - GetConnectionCode
  • Loading branch information
elraphty authored Jan 8, 2025
2 parents 1e615d5 + b878cf5 commit 549a810
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,82 @@ func TestIncrementProofCount(t *testing.T) {
})
}
}

func parseDate(dateStr string) *time.Time {
date, err := time.Parse("2006-01-02", dateStr)
if err != nil {
panic("Error parsing date: " + err.Error())
}
return &date
}

func TestGetConnectionCode(t *testing.T) {
InitTestDB()

defer CloseTestDB()

cleanup := func() {
TestDB.db.Exec("DELETE FROM connectioncodes")
}

tests := []struct {
name string
connectionCodes []ConnectionCodes
expected ConnectionCodesShort
expectUpdateCall bool
}{
{
name: "Basic Functionality",
connectionCodes: []ConnectionCodes{
{ConnectionString: "code1", DateCreated: parseDate("2006-01-02"), IsUsed: false},
{ConnectionString: "code2", DateCreated: parseDate("2023-10-02"), IsUsed: false},
},
expected: ConnectionCodesShort{ConnectionString: "code2", DateCreated: parseDate("2023-10-02")},
expectUpdateCall: true,
},
{
name: "No Unused Connection Codes",
connectionCodes: []ConnectionCodes{
{ConnectionString: "code1", DateCreated: parseDate("2023-10-01"), IsUsed: true},
},
expected: ConnectionCodesShort{},
expectUpdateCall: false,
},
{
name: "Single Unused Connection Code",
connectionCodes: []ConnectionCodes{
{ConnectionString: "code1", DateCreated: parseDate("2023-10-01"), IsUsed: false},
},
expected: ConnectionCodesShort{ConnectionString: "code1", DateCreated: parseDate("2023-10-01")},
expectUpdateCall: true,
},
{
name: "Multiple Unused Connection Codes",
connectionCodes: []ConnectionCodes{
{ConnectionString: "code1", DateCreated: parseDate("2023-10-01"), IsUsed: false},
{ConnectionString: "code2", DateCreated: parseDate("2023-10-02"), IsUsed: false},
},
expected: ConnectionCodesShort{ConnectionString: "code2", DateCreated: parseDate("2023-10-02")},
expectUpdateCall: true,
},
{
name: "Edge Case: Empty Database",
connectionCodes: []ConnectionCodes{},
expected: ConnectionCodesShort{},
expectUpdateCall: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

TestDB.CreateConnectionCode(tt.connectionCodes)

result := TestDB.GetConnectionCode()

assert.Equal(t, tt.expected.ConnectionString, result.ConnectionString)

cleanup()
})
}
}

0 comments on commit 549a810

Please sign in to comment.