[WIP] migration to psql
TODO: [ ] stats [ ] indexes
This commit is contained in:
parent
e1bd235785
commit
e72de38725
24 changed files with 648 additions and 936 deletions
|
@ -2,38 +2,50 @@ package database
|
|||
|
||||
import "testing"
|
||||
|
||||
var book = map[string]interface{}{
|
||||
"title": "some title",
|
||||
"author": []string{"Alice", "Bob"},
|
||||
"id": "r_m-IOzzIbA6QK5w",
|
||||
var book = Book{
|
||||
Id: "r_m-IOzzIbA6QK5w",
|
||||
Title: "some title",
|
||||
Author: []string{"Alice", "Bob"},
|
||||
}
|
||||
|
||||
func TestAddBook(t *testing.T) {
|
||||
db := Init(test_host, test_coll)
|
||||
defer del(db)
|
||||
func TestAddAndDeleteBook(t *testing.T) {
|
||||
db, dbclose := testDbInit(t)
|
||||
defer dbclose()
|
||||
|
||||
tAddBook(t, db)
|
||||
testAddBook(t, db)
|
||||
|
||||
books, num, err := db.GetNewBooks("", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetBooks() return an error: ", err)
|
||||
t.Fatal("db.GetNewBooks() return an error: ", err)
|
||||
}
|
||||
if num < 1 {
|
||||
t.Fatalf("db.GetBooks() didn't find any result.")
|
||||
t.Fatalf("db.GetNewBooks() didn't find any result.")
|
||||
}
|
||||
if len(books) < 1 {
|
||||
t.Fatalf("db.GetBooks() didn't return any result.")
|
||||
t.Fatalf("db.GetNewBooks() didn't return any result.")
|
||||
}
|
||||
if books[0].Title != book["title"] {
|
||||
t.Error("Book title don't match : '", books[0].Title, "' <=> '", book["title"], "'")
|
||||
if books[0].Title != book.Title {
|
||||
t.Error("Book title don't match : '", books[0].Title, "' <=> '", book.Title, "'")
|
||||
}
|
||||
|
||||
err = db.DeleteBook(books[0].Id)
|
||||
if err != nil {
|
||||
t.Fatal("db.DeleteBook() return an error: ", err)
|
||||
}
|
||||
books, num, err = db.GetNewBooks("", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetNewBooks() return an error after delete: ", err)
|
||||
}
|
||||
if num != 0 {
|
||||
t.Fatalf("the book was not deleted.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestActiveBook(t *testing.T) {
|
||||
db := Init(test_host, test_coll)
|
||||
defer del(db)
|
||||
db, dbclose := testDbInit(t)
|
||||
defer dbclose()
|
||||
|
||||
tAddBook(t, db)
|
||||
testAddBook(t, db)
|
||||
books, _, _ := db.GetNewBooks("", 1, 0)
|
||||
id := books[0].Id
|
||||
|
||||
|
@ -46,58 +58,57 @@ func TestActiveBook(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal("db.GetBookId(", id, ") return an error: ", err)
|
||||
}
|
||||
if !b.Active {
|
||||
t.Error("Book is not activated")
|
||||
}
|
||||
if b.Author[0] != books[0].Author[0] {
|
||||
t.Error("Book author don't match : '", b.Author, "' <=> '", book["author"], "'")
|
||||
t.Error("Book author don't match : '", b.Author, "' <=> '", book.Author, "'")
|
||||
}
|
||||
|
||||
bs, num, err := db.GetBooks(book.Title, 20, 0)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetBooks(", book.Title, ") return an error: ", err)
|
||||
}
|
||||
if num != 1 || len(bs) != 1 {
|
||||
t.Fatal("We got a un expected number of books: ", num, bs)
|
||||
}
|
||||
if bs[0].Author[0] != book.Author[0] {
|
||||
t.Error("Book author don't match : '", bs[0].Author, "' <=> '", book.Author, "'")
|
||||
}
|
||||
|
||||
bs, num, err = db.GetBooks("none", 20, 0)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetBooks(none) return an error: ", err)
|
||||
}
|
||||
if num != 0 || len(bs) != 0 {
|
||||
t.Error("We got books: ", num, bs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlag(t *testing.T) {
|
||||
db := Init(test_host, test_coll)
|
||||
defer del(db)
|
||||
func TestUpdateBook(t *testing.T) {
|
||||
db, dbclose := testDbInit(t)
|
||||
defer dbclose()
|
||||
|
||||
tAddBook(t, db)
|
||||
id, _ := book["id"].(string)
|
||||
db.ActiveBook(id)
|
||||
id2 := "tfgrBvd2ps_K4iYt"
|
||||
b2 := book
|
||||
b2["id"] = id2
|
||||
err := db.AddBook(b2)
|
||||
testAddBook(t, db)
|
||||
|
||||
newTitle := "other title"
|
||||
err := db.UpdateBook(book.Id, map[string]interface{}{
|
||||
"title": newTitle,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error("db.AddBook(", book, ") return an error:", err)
|
||||
}
|
||||
db.ActiveBook(id2)
|
||||
id3 := "tfgrBvd2ps_K4iY2"
|
||||
b3 := book
|
||||
b3["id"] = id3
|
||||
err = db.AddBook(b3)
|
||||
if err != nil {
|
||||
t.Error("db.AddBook(", book, ") return an error:", err)
|
||||
}
|
||||
db.ActiveBook(id3)
|
||||
|
||||
db.FlagBadQuality(id, "1")
|
||||
db.FlagBadQuality(id, "2")
|
||||
db.FlagBadQuality(id3, "1")
|
||||
|
||||
b, _ := db.GetBookId(id)
|
||||
if b.BadQuality != 2 {
|
||||
t.Error("The bad quality flag was not increased")
|
||||
}
|
||||
b, _ = db.GetBookId(id3)
|
||||
if b.BadQuality != 1 {
|
||||
t.Error("The bad quality flag was not increased")
|
||||
t.Fatal("db.UpdateBook() return an error: ", err)
|
||||
}
|
||||
|
||||
books, _, _ := db.GetBooks("flag:bad_quality", 2, 0)
|
||||
if len(books) != 2 {
|
||||
t.Fatal("Not the right number of results to the flag search:", len(books))
|
||||
books, num, err := db.GetNewBooks("", 1, 0)
|
||||
if err != nil || num != 1 || len(books) != 1 {
|
||||
t.Fatal("db.GetNewBooks() return an error: ", err)
|
||||
}
|
||||
if books[0].Id != id {
|
||||
t.Error("Search for flag bad_quality is not sort right")
|
||||
if books[0].Title != newTitle {
|
||||
t.Error("Book title don't match : '", books[0].Title, "' <=> '", newTitle, "'")
|
||||
}
|
||||
}
|
||||
|
||||
func tAddBook(t *testing.T, db DB) {
|
||||
func testAddBook(t *testing.T, db DB) {
|
||||
err := db.AddBook(book)
|
||||
if err != nil {
|
||||
t.Error("db.AddBook(", book, ") return an error:", err)
|
||||
|
|
Reference in a new issue