added tests for db and user
Some checks reported errors
arbeitszeitmessung/pipeline/head Something is wrong with the build of this commit
Some checks reported errors
arbeitszeitmessung/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
56
Backend/models/user_test.go
Normal file
56
Backend/models/user_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"database/sql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testUser models.User = models.User{Vorname: "Kim", Name: "Mustermensch", PersonalNummer: 456, CardUID: "aaaa-aaaa", ArbeitszeitPerTag: 8}
|
||||
|
||||
func SetupUserFixture(t *testing.T, db models.IDatabase) {
|
||||
t.Helper()
|
||||
db.Exec(`INSERT INTO "s_personal_daten" ("personal_nummer", "aktiv_beschaeftigt", "vorname", "nachname", "geburtsdatum", "plz", "adresse", "geschlecht", "card_uid", "hauptbeschaeftigungs_ort", "arbeitszeit_per_tag", "arbeitszeit_min_start", "arbeitszeit_max_ende", "vorgesetzter_pers_nr") VALUES
|
||||
(456, 't', 'Kim', 'Mustermensch', '2003-02-01', '08963', 'Altenburger Str. 44A', 1, 'aaaa-aaaa', 1, 8, '07:00:00', '20:00:00', 0);`)
|
||||
}
|
||||
|
||||
func TestGetUserByPersonalNr(t *testing.T) {
|
||||
tc := SetupDBFixture(t)
|
||||
SetupUserFixture(t, tc.Database)
|
||||
|
||||
models.DB = tc.Database
|
||||
|
||||
user, err := models.GetUserByPersonalNr(testUser.PersonalNummer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user != testUser {
|
||||
t.Error("Retrieved user not the same as testUser!")
|
||||
}
|
||||
|
||||
_, err = models.GetUserByPersonalNr(000)
|
||||
if err != sql.ErrNoRows {
|
||||
t.Error("Wrong error handling, when retrieving wrong personalnummer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckAnwesenheit(t *testing.T) {
|
||||
tc := SetupDBFixture(t)
|
||||
models.DB = tc.Database
|
||||
SetupUserFixture(t, tc.Database)
|
||||
|
||||
var actual bool
|
||||
|
||||
if actual = testUser.CheckAnwesenheit(); actual != false {
|
||||
t.Errorf("Checkabwesenheit with no booking should be false but is %t", actual)
|
||||
}
|
||||
tc.Database.Exec("INSERT INTO anwesenheit (timestamp, card_uid, check_in_out, geraet_id) VALUES (NOW() - INTERVAL '2 hour', 'aaaa-aaaa', 1, 1);")
|
||||
if actual = testUser.CheckAnwesenheit(); actual != true {
|
||||
t.Errorf("Checkabwesenheit with 'kommen' booking should be true but is %t", actual)
|
||||
}
|
||||
|
||||
tc.Database.Exec("INSERT INTO anwesenheit (timestamp, card_uid, check_in_out, geraet_id) VALUES (NOW() - INTERVAL '1 hour', 'aaaa-aaaa', 2, 1);")
|
||||
if actual = testUser.CheckAnwesenheit(); actual != false {
|
||||
t.Errorf("Checkabwesenheit with 'gehen' booking should be false but is %t", actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user