From 56fedcedd1ec3c381a56f979ba660a0b13dcbc57 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 17 Jul 2021 23:59:17 -0400 Subject: [PATCH] Improve test coverage --- storage/store/database/database.go | 10 +++- storage/store/database/database_test.go | 66 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/storage/store/database/database.go b/storage/store/database/database.go index df195e71..becedf73 100644 --- a/storage/store/database/database.go +++ b/storage/store/database/database.go @@ -477,7 +477,15 @@ func (s *Store) getServiceStatusByKey(tx *sql.Tx, key string, parameters *paging } func (s *Store) getServiceIDGroupAndNameByKey(tx *sql.Tx, key string) (id int64, group, name string, err error) { - rows, err := tx.Query("SELECT service_id, service_group, service_name FROM service WHERE service_key = $1 LIMIT 1", key) + rows, err := tx.Query( + ` + SELECT service_id, service_group, service_name + FROM service + WHERE service_key = $1 + LIMIT 1 + `, + key, + ) if err != nil { return 0, "", "", err } diff --git a/storage/store/database/database_test.go b/storage/store/database/database_test.go index fb96f617..5f3ddebf 100644 --- a/storage/store/database/database_test.go +++ b/storage/store/database/database_test.go @@ -241,3 +241,69 @@ func TestStore_Persistence(t *testing.T) { } } } + +// TestStore_InvalidTransaction tests what happens if an invalid transaction is passed as parameter +func TestStore_InvalidTransaction(t *testing.T) { + store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_InvalidTransaction.db") + defer store.Close() + tx, _ := store.db.Begin() + tx.Commit() + if err := store.insertResult(tx, 1, &testSuccessfulResult); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if err := store.insertConditionResults(tx, 1, testSuccessfulResult.ConditionResults); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if err := store.updateServiceUptime(tx, 1, &testSuccessfulResult); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getAllServiceKeys(tx); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getServiceStatusByKey(tx, testService.Key(), paging.NewServiceStatusParams().WithResults(1, 20)); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getEventsByServiceID(tx, 1, 1, 50); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getResultsByServiceID(tx, 1, 1, 50); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if err := store.deleteOldServiceEvents(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if err := store.deleteOldServiceResults(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, _, err := store.getServiceUptime(tx, 1, time.Now(), time.Now()); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getServiceID(tx, &testService); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getNumberOfEventsByServiceID(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getNumberOfResultsByServiceID(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getAgeOfOldestServiceUptimeEntry(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } + if _, err := store.getLastServiceResultSuccessValue(tx, 1); err == nil { + t.Error("should've returned an error, because the transaction was already committed") + } +} + +func TestStore_NoRows(t *testing.T) { + store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_NoRows.db") + defer store.Close() + tx, _ := store.db.Begin() + defer tx.Rollback() + if _, err := store.getLastServiceResultSuccessValue(tx, 1); err != errNoRowsReturned { + t.Errorf("should've %v, got %v", errNoRowsReturned, err) + } + if _, err := store.getAgeOfOldestServiceUptimeEntry(tx, 1); err != errNoRowsReturned { + t.Errorf("should've %v, got %v", errNoRowsReturned, err) + } +}