diff --git a/models/author_test.go b/models/author_test.go index 4948654..752d30d 100644 --- a/models/author_test.go +++ b/models/author_test.go @@ -35,6 +35,15 @@ func TestAddOrUpdateAuthor(t *testing.T) { assert.Equal(t, testauthor.Lastname, author.Lastname) } + // Should find something + allauthors, err = ListAuthors("tst") + assert.NoError(t, err) + + for _, author := range allauthors { + assert.Equal(t, testauthor.Forename, author.Forename) + assert.Equal(t, testauthor.Lastname, author.Lastname) + } + // Get the new author gotauthor, exists, err := GetAuthorByID(author1.ID) assert.NoError(t, err) diff --git a/models/book_test.go b/models/book_test.go index 02f6e6e..64f9680 100644 --- a/models/book_test.go +++ b/models/book_test.go @@ -79,6 +79,14 @@ func TestAddOrUpdateBook(t *testing.T) { assert.Equal(t, book.Title, testbook.Title) } + // Search + allbooks, err = ListBooks("est") + assert.NoError(t, err) + + for _, book := range allbooks { + assert.Equal(t, book.Title, testbook.Title) + } + // Get the new book gotBook, exists, err := GetBookByID(book1.ID) assert.NoError(t, err) diff --git a/models/config_test.go b/models/config_test.go index 9f8221b..813f6cb 100644 --- a/models/config_test.go +++ b/models/config_test.go @@ -11,10 +11,39 @@ func TestSetConfig(t *testing.T) { // Create test database assert.NoError(t, PrepareTestDatabase()) - // Write a fake config - configString := `[General] + // This should fail as it is looking for a nonexistent config + err := SetConfig() + assert.Error(t, err) + + // Write an invalid config + configString := `[General JWTSecret = Supersecret -Interface = :8080 +Interface = ; This should make it automatically to :8080 + +[Database +Type = sqlite +Path = ./library.db + +[User +Name = nope +Username = user +Passw]ord = 1234 +Email = nope@none.com` + err = ioutil.WriteFile("config.ini", []byte(configString), 0644) + assert.NoError(t, err) + + // Test setConfig (should fail as we're trying to parse an invalid config) + err = SetConfig() + assert.Error(t, err) + + // Delete the invalid file + err = os.Remove("config.ini") + assert.NoError(t, err) + + // Write a fake config + configString = `[General] +JWTSecret = Supersecret +Interface = ; This should make it automatically to :8080 [Database] Type = sqlite @@ -25,7 +54,7 @@ Name = nope Username = user Password = 1234 Email = nope@none.com` - err := ioutil.WriteFile("config.ini", []byte(configString), 0644) + err = ioutil.WriteFile("config.ini", []byte(configString), 0644) assert.NoError(t, err) // Test setConfig diff --git a/models/item_test.go b/models/item_test.go index 478387a..02b2476 100644 --- a/models/item_test.go +++ b/models/item_test.go @@ -41,6 +41,17 @@ func TestAddOrUpdateItem(t *testing.T) { assert.Equal(t, testitem.Other, item.Other) } + // Search + allitems, err = ListItems("esti") + assert.NoError(t, err) + + for _, item := range allitems { + assert.Equal(t, testitem.Title, item.Title) + assert.Equal(t, testitem.Price, item.Price) + assert.Equal(t, testitem.Description, item.Description) + assert.Equal(t, testitem.Other, item.Other) + } + // Get the new item gotitem, exists, err := GetItemByID(item1.ID) assert.NoError(t, err) diff --git a/models/models.go b/models/models.go index 80e0018..33e805d 100644 --- a/models/models.go +++ b/models/models.go @@ -65,9 +65,7 @@ func SetEngine() (err error) { _, err = CreateUser(Config.FirstUser) if err != nil { - if !IsErrUsernameExists(err) { - return err - } + return err } fmt.Println("Created new user " + Config.FirstUser.Username) diff --git a/models/publisher_test.go b/models/publisher_test.go index 9e0d911..8e7bfef 100644 --- a/models/publisher_test.go +++ b/models/publisher_test.go @@ -53,6 +53,11 @@ func TestAddOrUpdatePublisher(t *testing.T) { assert.NoError(t, err) assert.Equal(t, testpublisher.Name, publisher1updated.Name) + // Search + allpublishers, err = ListPublishers("rem") + assert.NoError(t, err) + assert.NotNil(t, allpublishers[0]) + // Delete the publisher err = DeletePublisherByID(publisher1.ID) assert.NoError(t, err) diff --git a/models/user_test.go b/models/user_test.go index b443c97..cee3930 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -43,9 +43,11 @@ func TestCreateUser(t *testing.T) { // Check if it fails to create a user without password and username createdUser, err = CreateUser(User{}) assert.Error(t, err) + assert.True(t, IsErrNoUsernamePassword(err)) createdUser, err = CreateUser(User{Name: "blub"}) assert.Error(t, err) + assert.True(t, IsErrNoUsernamePassword(err)) // Check if he exists _, exists, err := GetUser(createdUser)