Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - GetConnectionCode #2367

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
}
}
Loading