2024-03-20 17:54:23 -07:00
|
|
|
package repositories_test
|
|
|
|
|
2024-03-21 22:41:52 -07:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"go-cook/api/repositories"
|
|
|
|
"log"
|
|
|
|
"testing"
|
2024-03-20 17:54:23 -07:00
|
|
|
|
2024-03-23 08:27:35 -07:00
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
2024-03-21 22:41:52 -07:00
|
|
|
_ "github.com/glebarez/go-sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanCreateNewUser(t *testing.T) {
|
2024-03-23 08:27:35 -07:00
|
|
|
db, _, err := sqlmock.New()
|
|
|
|
//db, err := sql.Open("sqlite", "../../gocook.db")
|
2024-03-21 22:41:52 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println("unable to open connection")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
repo := repositories.NewUserRepository(db)
|
2024-03-26 17:53:22 -07:00
|
|
|
updated, err := repo.Create("testing", "NotSecure")
|
2024-03-21 22:41:52 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
log.Println(updated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanFindUserInTable(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite", "../../gocook.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("unable to open connection")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
repo := repositories.NewUserRepository(db)
|
2024-03-23 08:27:35 -07:00
|
|
|
user, err := repo.GetByName("testing")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
log.Println(user)
|
2024-03-21 22:41:52 -07:00
|
|
|
}
|
2024-03-23 08:27:35 -07:00
|
|
|
|
|
|
|
func TestCheckUserHash (t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite", "../../gocook.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("unable to open connection")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
repo := repositories.NewUserRepository(db)
|
|
|
|
repo.CheckUserHash("testing", "NotSecure")
|
|
|
|
}
|