Compare commits
2 Commits
34bd44db5c
...
23e05b8cb5
| Author | SHA1 | Date | |
|---|---|---|---|
| 23e05b8cb5 | |||
| 50bec238a4 |
@@ -5,10 +5,8 @@ import (
|
|||||||
"arbeitszeitmessung/models"
|
"arbeitszeitmessung/models"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/golang-migrate/migrate/v4"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func OpenDatabase() (models.IDatabase, error) {
|
func OpenDatabase() (models.IDatabase, error) {
|
||||||
@@ -18,18 +16,5 @@ func OpenDatabase() (models.IDatabase, error) {
|
|||||||
dbPassword := helper.GetEnv("POSTGRES_API_PASS", "password")
|
dbPassword := helper.GetEnv("POSTGRES_API_PASS", "password")
|
||||||
|
|
||||||
connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=Europe/Berlin", dbUser, dbPassword, dbHost, dbName)
|
connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=Europe/Berlin", dbUser, dbPassword, dbHost, dbName)
|
||||||
db, err := sql.Open("postgres", connStr)
|
return sql.Open("postgres", connStr)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
return db, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func MigrateDB(db models.IDatabase, connStr string) {
|
|
||||||
m, err := migrate.New("file:///../migrations/", connStr)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Error starting migration", err)
|
|
||||||
}
|
|
||||||
m.Up()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,10 +36,10 @@ func SetupDBFixture(t *testing.T) *DBFixture {
|
|||||||
t.Fatalf("failed to connect to database: %v", err)
|
t.Fatalf("failed to connect to database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = MigrateDB(db, "file://../../migrations")
|
// err = MigrateDB(db, "file://../../migrations")
|
||||||
if err != nil && err != migrate.ErrNoChange {
|
// if err != nil && err != migrate.ErrNoChange {
|
||||||
t.Fatalf("Failed to migrate database: %v", err)
|
// t.Fatalf("Failed to migrate database: %v", err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -44,12 +44,12 @@ func TestCheckAnwesenheit(t *testing.T) {
|
|||||||
if actual = testUser.CheckAnwesenheit(); actual != false {
|
if actual = testUser.CheckAnwesenheit(); actual != false {
|
||||||
t.Errorf("Checkabwesenheit with no booking should be false but is %t", actual)
|
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);")
|
tc.Database.Exec("INSERT INTO anwesenheit (timestamp, card_uid, check_in_out, geraet_id) VALUES (NOW() - INTERVAL '2 minute', 'aaaa-aaaa', 1, 1);")
|
||||||
if actual = testUser.CheckAnwesenheit(); actual != true {
|
if actual = testUser.CheckAnwesenheit(); actual != true {
|
||||||
t.Errorf("Checkabwesenheit with 'kommen' booking should be true but is %t", actual)
|
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);")
|
tc.Database.Exec("INSERT INTO anwesenheit (timestamp, card_uid, check_in_out, geraet_id) VALUES (NOW() - INTERVAL '1 minute', 'aaaa-aaaa', 2, 1);")
|
||||||
if actual = testUser.CheckAnwesenheit(); actual != false {
|
if actual = testUser.CheckAnwesenheit(); actual != false {
|
||||||
t.Errorf("Checkabwesenheit with 'gehen' booking should be false but is %t", actual)
|
t.Errorf("Checkabwesenheit with 'gehen' booking should be false but is %t", actual)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user