package repositories_test import ( "database/sql" "testing" "time" "git.jamestombleson.com/jtom38/go-cook/internal/repositories" _ "github.com/glebarez/go-sqlite" "github.com/pressly/goose/v3" ) func TestRefreshTokenCreate(t *testing.T) { conn, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } client := repositories.NewRefreshTokenRepository(conn) rows, err := client.Create("tester", "BadTokenDontUse", time.Now().Add(time.Hour+1)) if err != nil { t.Log(err) t.FailNow() } if rows == 0 { t.Log("expected one row to come back but got 0") } } func TestRefreshTokenGetByUsername(t *testing.T) { conn, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } client := repositories.NewRefreshTokenRepository(conn) rows, err := client.Create("tester", "BadTokenDoNotUse", time.Now().Add(time.Hour+1)) if err != nil { t.Log(err) t.FailNow() } if rows != 1 { t.Log("expected a row to be added but not the wrong value back") t.FailNow() } model, err := client.GetByUsername("tester") if err != nil { t.Log(err) t.FailNow() } if model.Username != "tester" { t.Log("got the wrong user back") t.FailNow() } } func TestRefreshTokenDeleteById(t *testing.T) { conn, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } client := repositories.NewRefreshTokenRepository(conn) _, err = client.Create("tester", "BadTokenDoNotUse", time.Now().Add(time.Hour+1)) if err != nil { t.Log(err) t.FailNow() } model, err := client.GetByUsername("tester") if err != nil { t.Log(err) t.FailNow() } updated, err := client.DeleteById(model.Id) if err != nil { t.Log(err) t.FailNow() } if updated != 1 { t.Log("deleted the wrong number of records") t.FailNow() } } func setupInMemoryDb() (*sql.DB, error) { db, err := sql.Open("sqlite", ":memory:") if err != nil { return nil, err } err = goose.SetDialect("sqlite3") if err != nil { return nil, err } err = goose.Up(db, "../migrations") if err != nil { return nil, err } return db, nil }