-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from NETWAYS/chore/tests
Add basic unittests for SQL operations
- Loading branch information
Showing
5 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= | ||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= | ||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= | ||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= | ||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | ||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= | ||
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
) | ||
|
||
func TestGetInstanceID_WithOK(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
expected := 1 | ||
|
||
rows := sqlmock.NewRows([]string{"instance_id"}). | ||
AddRow(expected) | ||
|
||
mock.ExpectQuery("SELECT instance_id FROM icinga_instances WHERE instance_name = ?").WillReturnRows(rows) | ||
|
||
id, err := getInstanceID(db, "default") | ||
|
||
if err != nil { | ||
t.Errorf("error was not expected while getting instance: %s", err) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
|
||
if id != expected { | ||
t.Errorf("actual: %d, expected: %d", id, expected) | ||
} | ||
} | ||
|
||
func TestGetInstanceID_WithError(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
_, DbErr := getInstanceID(db, "default") | ||
|
||
if err != nil { | ||
t.Errorf("error was not expected while getting instance: %s", err) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
|
||
if DbErr == nil { | ||
t.Errorf("expected an error got nil") | ||
} | ||
} | ||
|
||
func TestOldestTime_WithOK(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
expected := "2024-07-02 08:25:44" | ||
rows := sqlmock.NewRows([]string{"start_time"}). | ||
AddRow(expected) | ||
|
||
mock.ExpectQuery("SELECT start_time FROM icinga_notifications WHERE (.+) ORDER BY start_time ASC LIMIT 1").WillReturnRows(rows) | ||
|
||
table := Table{"notifications", "start_time"} | ||
oldest, err := table.OldestTime(db, 1) | ||
|
||
if "2024-07-02 08:25:44 +0000 UTC" != oldest.String() { | ||
t.Errorf("returned timestamp not expected: %s", oldest.String()) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("error was not expected while getting oldest time: %s", err) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestCleanup_WithOK(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
ts := time.Now() | ||
mock.ExpectExec("DELETE FROM icinga_notifications WHERE (.+) AND start_time (.+) LIMIT 10"). | ||
WithArgs(1, ts). | ||
WillReturnResult(sqlmock.NewResult(1, 1)) | ||
|
||
table := Table{"notifications", "start_time"} | ||
r, err := table.Cleanup(db, 1, ts, 10) | ||
|
||
if 1 != r { | ||
t.Errorf("returned rows not expected: %d", r) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("error was not expected while cleanup: %s", err) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestCount_WithOK(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
rows := sqlmock.NewRows([]string{"count(*)"}). | ||
AddRow(123) | ||
|
||
ts := time.Now() | ||
mock.ExpectQuery("SELECT count(.+) FROM icinga_notifications WHERE instance_id = (.+) AND start_time < (.+)"). | ||
WithArgs(1, ts). | ||
WillReturnRows(rows) | ||
|
||
table := Table{"notifications", "start_time"} | ||
r, err := table.Count(db, 1, ts) | ||
|
||
if 123 != r { | ||
t.Errorf("returned rows not expected: %d", r) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("error was not expected while cleanup: %s", err) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} |