newsbot-api/internal/repository/users_test.go

89 lines
1.7 KiB
Go

package repository_test
import (
"context"
"database/sql"
"log"
"testing"
"git.jamestombleson.com/jtom38/newsbot-api/internal/repository"
_ "github.com/glebarez/go-sqlite"
"github.com/pressly/goose/v3"
)
func TestCanCreateNewUser(t *testing.T) {
db, err := setupInMemoryDb()
if err != nil {
t.Log(err)
t.FailNow()
}
defer db.Close()
repo := repository.NewUserRepository(db)
updated, err := repo.Create(context.Background(), "testing", "NotSecure", "sessionToken", "placeholder")
if err != nil {
log.Println(err)
t.FailNow()
}
log.Println(updated)
}
func TestCanFindUserInTable(t *testing.T) {
db, err := setupInMemoryDb()
if err != nil {
log.Println("unable to open connection")
t.FailNow()
}
defer db.Close()
repo := repository.NewUserRepository(db)
updated, err := repo.Create(context.Background(), "testing", "NotSecure", "sessionToken", "placeholder")
if err != nil {
t.Log(err)
t.FailNow()
}
if updated != 1 {
t.Log("expected a row to come back")
t.FailNow()
}
user, err := repo.GetByName(context.Background(), "testing")
if err != nil {
log.Println(err)
t.FailNow()
}
log.Println(user)
}
func TestCheckUserHash(t *testing.T) {
db, err := setupInMemoryDb()
if err != nil {
log.Println("unable to open connection")
t.FailNow()
}
defer db.Close()
repo := repository.NewUserRepository(db)
repo.CheckUserHash(context.Background(), "testing", "NotSecure")
}
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, "../database/migrations")
if err != nil {
return nil, err
}
return db, nil
}