Browse Source

Made tests work again

master
konrad 1 year ago
parent
commit
c097264bde
Signed by: kolaente <konrad@kola-entertainments.de> GPG Key ID: F40E70337AB24C9B
5 changed files with 66 additions and 45 deletions
  1. 14
    9
      models/author_test.go
  2. 11
    7
      models/book_test.go
  3. 10
    6
      models/item_test.go
  4. 10
    6
      models/publisher_test.go
  5. 21
    17
      models/user_test.go

+ 14
- 9
models/author_test.go View File

@@ -11,17 +11,22 @@ func TestAddOrUpdateAuthor(t *testing.T) {

// TODO delete all existing authors from eventual previuous tests

// Get our doer
doer, exx, err := GetUserByID(1)
assert.True(t, exx)
assert.NoError(t, err)

// Bootstrap our test author
testauthor := Author{Forename: "test", Lastname: "tsting"}

// Create a new author
author1, err := AddOrUpdateAuthor(testauthor)
author1, err := AddOrUpdateAuthor(testauthor, &doer)
assert.NoError(t, err)
assert.Equal(t, testauthor.Forename, author1.Forename)
assert.Equal(t, testauthor.Lastname, author1.Lastname)

// And anotherone
author2, err := AddOrUpdateAuthor(testauthor)
author2, err := AddOrUpdateAuthor(testauthor, &doer)
assert.NoError(t, err)
assert.Equal(t, testauthor.Forename, author2.Forename)
assert.Equal(t, testauthor.Lastname, author2.Lastname)
@@ -52,20 +57,20 @@ func TestAddOrUpdateAuthor(t *testing.T) {
assert.Equal(t, gotauthor.Lastname, testauthor.Lastname)

// Pass an empty author to see if it fails
_, err = AddOrUpdateAuthor(Author{})
_, err = AddOrUpdateAuthor(Author{}, &doer)
assert.Error(t, err)
assert.True(t, IsErrAuthorCannotBeEmpty(err))

// Update the author
testauthor.ID = author1.ID
testauthor.Forename = "Lorem Ipsum"
author1updated, err := AddOrUpdateAuthor(testauthor)
author1updated, err := AddOrUpdateAuthor(testauthor, &doer)
assert.NoError(t, err)
assert.Equal(t, testauthor.Forename, author1updated.Forename)
assert.Equal(t, testauthor.Lastname, author1updated.Lastname)

// Delete the author
err = DeleteAuthorByID(author1.ID)
err = DeleteAuthorByID(author1.ID, &doer)
assert.NoError(t, err)

// Check if it is gone
@@ -74,7 +79,7 @@ func TestAddOrUpdateAuthor(t *testing.T) {
assert.False(t, exists)

// Try deleting an author with ID = 0
err = DeleteAuthorByID(0)
err = DeleteAuthorByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))

@@ -85,15 +90,15 @@ func TestAddOrUpdateAuthor(t *testing.T) {
x.DropTables(Author{})

// Test inserting
_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"})
_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"}, &doer)
assert.Error(t, err)

// Test updating
_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"})
_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"}, &doer)
assert.Error(t, err)

// Delete from some nonexistent
err = DeleteAuthorByID(3)
err = DeleteAuthorByID(3, &doer)
assert.Error(t, err)

// And get from nonexistant

+ 11
- 7
models/book_test.go View File

@@ -9,9 +9,13 @@ func TestAddOrUpdateBook(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())

// Get our doer
doer, _, err := GetUserByID(1)
assert.NoError(t, err)

// Create a new author for testing purposes
testauthor1 := Author{Forename: "Testauthor wich", Lastname: "already exists"}
testauthorin1, err := AddOrUpdateAuthor(testauthor1)
testauthorin1, err := AddOrUpdateAuthor(testauthor1, &doer)
assert.NoError(t, err)

// Bootstrap our test book
@@ -44,7 +48,7 @@ func TestAddOrUpdateBook(t *testing.T) {
}

// Insert one new Testbook
book1, err := AddOrUpdateBook(testbook)
book1, err := AddOrUpdateBook(testbook, &doer)
assert.NoError(t, err)

// Check if everything was inserted correctly
@@ -67,7 +71,7 @@ func TestAddOrUpdateBook(t *testing.T) {
assert.Equal(t, book1.Authors[2].Forename, testauthor1.Forename)

// And anotherone
book2, err := AddOrUpdateBook(testbook)
book2, err := AddOrUpdateBook(testbook, &doer)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book2.Title) // If this works, the rest should work too so we don't need to recheck everythin again

@@ -94,14 +98,14 @@ func TestAddOrUpdateBook(t *testing.T) {
assert.Equal(t, testbook.Title, gotBook.Title)

// Pass an empty Book to see if it fails
_, err = AddOrUpdateBook(Book{})
_, err = AddOrUpdateBook(Book{}, &doer)
assert.Error(t, err)
assert.True(t, IsErrBookTitleCannotBeEmpty(err))

// Update the book
testbook.ID = book1.ID
testbook.Title = "LormIspmus"
book1updated, err := AddOrUpdateBook(testbook)
book1updated, err := AddOrUpdateBook(testbook, &doer)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book1updated.Title)

@@ -128,7 +132,7 @@ func TestAddOrUpdateBook(t *testing.T) {
assert.Equal(t, int64(99), qty2)

// Delete the book
err = DeleteBookByID(book1.ID)
err = DeleteBookByID(book1.ID, &doer)
assert.NoError(t, err)

// Check if its gone
@@ -137,7 +141,7 @@ func TestAddOrUpdateBook(t *testing.T) {
assert.False(t, exists)

// Try deleting one with ID = 0
err = DeleteBookByID(0)
err = DeleteBookByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

+ 10
- 6
models/item_test.go View File

@@ -9,6 +9,10 @@ func TestAddOrUpdateItem(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())

// Get our doer
doer, _, err := GetUserByID(1)
assert.NoError(t, err)

// Bootstrap our test item
testitem := Item{
Title: "Testitem",
@@ -18,7 +22,7 @@ func TestAddOrUpdateItem(t *testing.T) {
}

// Create a new item
item1, err := AddOrUpdateItem(testitem)
item1, err := AddOrUpdateItem(testitem, &doer)
assert.NoError(t, err)
assert.Equal(t, testitem.Title, item1.Title)
assert.Equal(t, testitem.Price, item1.Price)
@@ -26,7 +30,7 @@ func TestAddOrUpdateItem(t *testing.T) {
assert.Equal(t, testitem.Other, item1.Other)

// And anotherone
item2, err := AddOrUpdateItem(testitem)
item2, err := AddOrUpdateItem(testitem, &doer)
assert.NoError(t, err)
assert.Equal(t, testitem.Title, item2.Title)

@@ -62,14 +66,14 @@ func TestAddOrUpdateItem(t *testing.T) {
assert.Equal(t, testitem.Other, gotitem.Other)

// Pass an empty item to see if it fails
_, err = AddOrUpdateItem(Item{})
_, err = AddOrUpdateItem(Item{}, &doer)
assert.Error(t, err)
assert.True(t, IsErrItemTitleCannotBeEmpty(err))

// Update the item
testitem.ID = item1.ID
testitem.Title = "Lorem Ipsum"
item1updated, err := AddOrUpdateItem(testitem)
item1updated, err := AddOrUpdateItem(testitem, &doer)
assert.NoError(t, err)
assert.Equal(t, testitem.Title, item1updated.Title)
assert.Equal(t, testitem.Price, item1updated.Price)
@@ -90,7 +94,7 @@ func TestAddOrUpdateItem(t *testing.T) {
assert.Equal(t, int64(99), qty2)

// Delete the item
err = DeleteItemByID(item1.ID)
err = DeleteItemByID(item1.ID, &doer)
assert.NoError(t, err)

// Check if it is gone
@@ -99,7 +103,7 @@ func TestAddOrUpdateItem(t *testing.T) {
assert.False(t, exists)

// Try deleting one with ID = 0
err = DeleteItemByID(0)
err = DeleteItemByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

+ 10
- 6
models/publisher_test.go View File

@@ -9,6 +9,10 @@ func TestAddOrUpdatePublisher(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())

// Get our doer
doer, _, err := GetUserByID(1)
assert.NoError(t, err)

// Bootstrap our test publisher
testpublisher := Publisher{
Name: "Testpublisher",
@@ -21,7 +25,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
for _, publisher := range allpublishers {

// Delete
err = DeletePublisherByID(publisher.ID)
err = DeletePublisherByID(publisher.ID, &doer)
assert.NoError(t, err)

// Check if it is gone
@@ -31,7 +35,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
}

// Create a new publisher
publisher1, err := AddOrUpdatePublisher(testpublisher)
publisher1, err := AddOrUpdatePublisher(testpublisher, &doer)
assert.NoError(t, err)
assert.Equal(t, testpublisher.Name, publisher1.Name)

@@ -42,14 +46,14 @@ func TestAddOrUpdatePublisher(t *testing.T) {
assert.Equal(t, testpublisher.Name, gotpublisher.Name)

// Pass an empty publisher to see if it fails
_, err = AddOrUpdatePublisher(Publisher{})
_, err = AddOrUpdatePublisher(Publisher{}, &doer)
assert.Error(t, err)
assert.True(t, IsErrNoPublisherName(err))

// Update the publisher
testpublisher.ID = publisher1.ID
testpublisher.Name = "Lorem Ipsum"
publisher1updated, err := AddOrUpdatePublisher(testpublisher)
publisher1updated, err := AddOrUpdatePublisher(testpublisher, &doer)
assert.NoError(t, err)
assert.Equal(t, testpublisher.Name, publisher1updated.Name)

@@ -59,7 +63,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
assert.NotNil(t, allpublishers[0])

// Delete the publisher
err = DeletePublisherByID(publisher1.ID)
err = DeletePublisherByID(publisher1.ID, &doer)
assert.NoError(t, err)

// Check if it is gone
@@ -68,7 +72,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
assert.False(t, exists)

// Try deleting one with ID = 0
err = DeletePublisherByID(0)
err = DeletePublisherByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

+ 21
- 17
models/user_test.go View File

@@ -9,6 +9,10 @@ func TestCreateUser(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())

// Get our doer
doer, _, err := GetUserByID(1)
assert.NoError(t, err)

// Our dummy user for testing
dummyuser := User{
Name: "noooem, dief",
@@ -19,45 +23,45 @@ func TestCreateUser(t *testing.T) {
}

// Delete every preexisting user to have a fresh start
_, err := x.Where("1 = 1").Delete(&User{})
_, err = x.Where("1 = 1").Delete(&User{})
assert.NoError(t, err)

allusers, err := ListUsers("")
assert.NoError(t, err)
for _, user := range allusers {
// Delete it
err := DeleteUserByID(user.ID)
err := DeleteUserByID(user.ID, &doer)
assert.NoError(t, err)
}

// Create a new user
createdUser, err := CreateUser(dummyuser)
createdUser, err := CreateUser(dummyuser, &doer)
assert.NoError(t, err)

// Create a second new user
createdUser2, err := CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password})
createdUser2, err := CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password}, &doer)
assert.NoError(t, err)

// Check if it fails to create the same user again
_, err = CreateUser(dummyuser)
_, err = CreateUser(dummyuser, &doer)
assert.Error(t, err)

// Check if it fails to create a user with just the same username
_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"})
_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"}, &doer)
assert.Error(t, err)
assert.True(t, IsErrUsernameExists(err))

// Check if it fails to create one with the same email
_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email})
_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email}, &doer)
assert.Error(t, err)
assert.True(t, IsErrUserEmailExists(err))

// Check if it fails to create a user without password and username
_, err = CreateUser(User{})
_, err = CreateUser(User{}, &doer)
assert.Error(t, err)
assert.True(t, IsErrNoUsernamePassword(err))

_, err = CreateUser(User{Name: "blub"})
_, err = CreateUser(User{Name: "blub"}, &doer)
assert.Error(t, err)
assert.True(t, IsErrNoUsernamePassword(err))

@@ -92,20 +96,20 @@ func TestCreateUser(t *testing.T) {

// Update the user
newname := "Test_te"
uuser, err := UpdateUser(User{ID: theuser.ID, Name: newname, Password: "444444"})
uuser, err := UpdateUser(User{ID: theuser.ID, Name: newname, Password: "444444"}, &doer)
assert.NoError(t, err)
assert.Equal(t, newname, uuser.Name)
assert.Equal(t, theuser.Password, uuser.Password) // Password should not change
assert.Equal(t, theuser.Username, uuser.Username) // Username should not change either

// Try updating one which does not exist
_, err = UpdateUser(User{ID: 99999, Username: "dg"})
_, err = UpdateUser(User{ID: 99999, Username: "dg"}, &doer)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))

// Update a users password
newpassword := "55555"
err = UpdateUserPassword(theuser.ID, newpassword)
err = UpdateUserPassword(theuser.ID, newpassword, &doer)
assert.NoError(t, err)

// Check if it was changed
@@ -123,26 +127,26 @@ func TestCreateUser(t *testing.T) {
assert.True(t, len(all) > 0)

// Try updating the password of a nonexistent user (should fail)
err = UpdateUserPassword(9999, newpassword)
err = UpdateUserPassword(9999, newpassword, &doer)
assert.Error(t, err)
assert.True(t, IsErrUserDoesNotExist(err))

// Delete it
err = DeleteUserByID(theuser.ID)
err = DeleteUserByID(theuser.ID, &doer)
assert.NoError(t, err)

// Try deleting one with ID = 0
err = DeleteUserByID(0)
err = DeleteUserByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))

// Try delete the last user (Should fail)
err = DeleteUserByID(createdUser2.ID)
err = DeleteUserByID(createdUser2.ID, &doer)
assert.Error(t, err)
assert.True(t, IsErrCannotDeleteLastUser(err))

// Log some user action
err = logAction("test something", User{ID: 1}, 1)
err = logAction(ActionTypeUserUpdated, &doer, 1)
assert.NoError(t, err)

}

Loading…
Cancel
Save