Browse Source

Made tests work again

master
konrad 10 months 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) {
11 11
 
12 12
 	// TODO delete all existing authors from eventual previuous tests
13 13
 
14
+	// Get our doer
15
+	doer, exx, err := GetUserByID(1)
16
+	assert.True(t, exx)
17
+	assert.NoError(t, err)
18
+
14 19
 	// Bootstrap our test author
15 20
 	testauthor := Author{Forename: "test", Lastname: "tsting"}
16 21
 
17 22
 	// Create a new author
18
-	author1, err := AddOrUpdateAuthor(testauthor)
23
+	author1, err := AddOrUpdateAuthor(testauthor, &doer)
19 24
 	assert.NoError(t, err)
20 25
 	assert.Equal(t, testauthor.Forename, author1.Forename)
21 26
 	assert.Equal(t, testauthor.Lastname, author1.Lastname)
22 27
 
23 28
 	// And anotherone
24
-	author2, err := AddOrUpdateAuthor(testauthor)
29
+	author2, err := AddOrUpdateAuthor(testauthor, &doer)
25 30
 	assert.NoError(t, err)
26 31
 	assert.Equal(t, testauthor.Forename, author2.Forename)
27 32
 	assert.Equal(t, testauthor.Lastname, author2.Lastname)
@@ -52,20 +57,20 @@ func TestAddOrUpdateAuthor(t *testing.T) {
52 57
 	assert.Equal(t, gotauthor.Lastname, testauthor.Lastname)
53 58
 
54 59
 	// Pass an empty author to see if it fails
55
-	_, err = AddOrUpdateAuthor(Author{})
60
+	_, err = AddOrUpdateAuthor(Author{}, &doer)
56 61
 	assert.Error(t, err)
57 62
 	assert.True(t, IsErrAuthorCannotBeEmpty(err))
58 63
 
59 64
 	// Update the author
60 65
 	testauthor.ID = author1.ID
61 66
 	testauthor.Forename = "Lorem Ipsum"
62
-	author1updated, err := AddOrUpdateAuthor(testauthor)
67
+	author1updated, err := AddOrUpdateAuthor(testauthor, &doer)
63 68
 	assert.NoError(t, err)
64 69
 	assert.Equal(t, testauthor.Forename, author1updated.Forename)
65 70
 	assert.Equal(t, testauthor.Lastname, author1updated.Lastname)
66 71
 
67 72
 	// Delete the author
68
-	err = DeleteAuthorByID(author1.ID)
73
+	err = DeleteAuthorByID(author1.ID, &doer)
69 74
 	assert.NoError(t, err)
70 75
 
71 76
 	// Check if it is gone
@@ -74,7 +79,7 @@ func TestAddOrUpdateAuthor(t *testing.T) {
74 79
 	assert.False(t, exists)
75 80
 
76 81
 	// Try deleting an author with ID = 0
77
-	err = DeleteAuthorByID(0)
82
+	err = DeleteAuthorByID(0, &doer)
78 83
 	assert.Error(t, err)
79 84
 	assert.True(t, IsErrIDCannotBeZero(err))
80 85
 
@@ -85,15 +90,15 @@ func TestAddOrUpdateAuthor(t *testing.T) {
85 90
 	x.DropTables(Author{})
86 91
 
87 92
 	// Test inserting
88
-	_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"})
93
+	_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"}, &doer)
89 94
 	assert.Error(t, err)
90 95
 
91 96
 	// Test updating
92
-	_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"})
97
+	_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"}, &doer)
93 98
 	assert.Error(t, err)
94 99
 
95 100
 	// Delete from some nonexistent
96
-	err = DeleteAuthorByID(3)
101
+	err = DeleteAuthorByID(3, &doer)
97 102
 	assert.Error(t, err)
98 103
 
99 104
 	// And get from nonexistant

+ 11
- 7
models/book_test.go View File

@@ -9,9 +9,13 @@ func TestAddOrUpdateBook(t *testing.T) {
9 9
 	// Create test database
10 10
 	assert.NoError(t, PrepareTestDatabase())
11 11
 
12
+	// Get our doer
13
+	doer, _, err := GetUserByID(1)
14
+	assert.NoError(t, err)
15
+
12 16
 	// Create a new author for testing purposes
13 17
 	testauthor1 := Author{Forename: "Testauthor wich", Lastname: "already exists"}
14
-	testauthorin1, err := AddOrUpdateAuthor(testauthor1)
18
+	testauthorin1, err := AddOrUpdateAuthor(testauthor1, &doer)
15 19
 	assert.NoError(t, err)
16 20
 
17 21
 	// Bootstrap our test book
@@ -44,7 +48,7 @@ func TestAddOrUpdateBook(t *testing.T) {
44 48
 	}
45 49
 
46 50
 	// Insert one new Testbook
47
-	book1, err := AddOrUpdateBook(testbook)
51
+	book1, err := AddOrUpdateBook(testbook, &doer)
48 52
 	assert.NoError(t, err)
49 53
 
50 54
 	// Check if everything was inserted correctly
@@ -67,7 +71,7 @@ func TestAddOrUpdateBook(t *testing.T) {
67 71
 	assert.Equal(t, book1.Authors[2].Forename, testauthor1.Forename)
68 72
 
69 73
 	// And anotherone
70
-	book2, err := AddOrUpdateBook(testbook)
74
+	book2, err := AddOrUpdateBook(testbook, &doer)
71 75
 	assert.NoError(t, err)
72 76
 	assert.Equal(t, testbook.Title, book2.Title) // If this works, the rest should work too so we don't need to recheck everythin again
73 77
 
@@ -94,14 +98,14 @@ func TestAddOrUpdateBook(t *testing.T) {
94 98
 	assert.Equal(t, testbook.Title, gotBook.Title)
95 99
 
96 100
 	// Pass an empty Book to see if it fails
97
-	_, err = AddOrUpdateBook(Book{})
101
+	_, err = AddOrUpdateBook(Book{}, &doer)
98 102
 	assert.Error(t, err)
99 103
 	assert.True(t, IsErrBookTitleCannotBeEmpty(err))
100 104
 
101 105
 	// Update the book
102 106
 	testbook.ID = book1.ID
103 107
 	testbook.Title = "LormIspmus"
104
-	book1updated, err := AddOrUpdateBook(testbook)
108
+	book1updated, err := AddOrUpdateBook(testbook, &doer)
105 109
 	assert.NoError(t, err)
106 110
 	assert.Equal(t, testbook.Title, book1updated.Title)
107 111
 
@@ -128,7 +132,7 @@ func TestAddOrUpdateBook(t *testing.T) {
128 132
 	assert.Equal(t, int64(99), qty2)
129 133
 
130 134
 	// Delete the book
131
-	err = DeleteBookByID(book1.ID)
135
+	err = DeleteBookByID(book1.ID, &doer)
132 136
 	assert.NoError(t, err)
133 137
 
134 138
 	// Check if its gone
@@ -137,7 +141,7 @@ func TestAddOrUpdateBook(t *testing.T) {
137 141
 	assert.False(t, exists)
138 142
 
139 143
 	// Try deleting one with ID = 0
140
-	err = DeleteBookByID(0)
144
+	err = DeleteBookByID(0, &doer)
141 145
 	assert.Error(t, err)
142 146
 	assert.True(t, IsErrIDCannotBeZero(err))
143 147
 }

+ 10
- 6
models/item_test.go View File

@@ -9,6 +9,10 @@ func TestAddOrUpdateItem(t *testing.T) {
9 9
 	// Create test database
10 10
 	assert.NoError(t, PrepareTestDatabase())
11 11
 
12
+	// Get our doer
13
+	doer, _, err := GetUserByID(1)
14
+	assert.NoError(t, err)
15
+
12 16
 	// Bootstrap our test item
13 17
 	testitem := Item{
14 18
 		Title:       "Testitem",
@@ -18,7 +22,7 @@ func TestAddOrUpdateItem(t *testing.T) {
18 22
 	}
19 23
 
20 24
 	// Create a new item
21
-	item1, err := AddOrUpdateItem(testitem)
25
+	item1, err := AddOrUpdateItem(testitem, &doer)
22 26
 	assert.NoError(t, err)
23 27
 	assert.Equal(t, testitem.Title, item1.Title)
24 28
 	assert.Equal(t, testitem.Price, item1.Price)
@@ -26,7 +30,7 @@ func TestAddOrUpdateItem(t *testing.T) {
26 30
 	assert.Equal(t, testitem.Other, item1.Other)
27 31
 
28 32
 	// And anotherone
29
-	item2, err := AddOrUpdateItem(testitem)
33
+	item2, err := AddOrUpdateItem(testitem, &doer)
30 34
 	assert.NoError(t, err)
31 35
 	assert.Equal(t, testitem.Title, item2.Title)
32 36
 
@@ -62,14 +66,14 @@ func TestAddOrUpdateItem(t *testing.T) {
62 66
 	assert.Equal(t, testitem.Other, gotitem.Other)
63 67
 
64 68
 	// Pass an empty item to see if it fails
65
-	_, err = AddOrUpdateItem(Item{})
69
+	_, err = AddOrUpdateItem(Item{}, &doer)
66 70
 	assert.Error(t, err)
67 71
 	assert.True(t, IsErrItemTitleCannotBeEmpty(err))
68 72
 
69 73
 	// Update the item
70 74
 	testitem.ID = item1.ID
71 75
 	testitem.Title = "Lorem Ipsum"
72
-	item1updated, err := AddOrUpdateItem(testitem)
76
+	item1updated, err := AddOrUpdateItem(testitem, &doer)
73 77
 	assert.NoError(t, err)
74 78
 	assert.Equal(t, testitem.Title, item1updated.Title)
75 79
 	assert.Equal(t, testitem.Price, item1updated.Price)
@@ -90,7 +94,7 @@ func TestAddOrUpdateItem(t *testing.T) {
90 94
 	assert.Equal(t, int64(99), qty2)
91 95
 
92 96
 	// Delete the item
93
-	err = DeleteItemByID(item1.ID)
97
+	err = DeleteItemByID(item1.ID, &doer)
94 98
 	assert.NoError(t, err)
95 99
 
96 100
 	// Check if it is gone
@@ -99,7 +103,7 @@ func TestAddOrUpdateItem(t *testing.T) {
99 103
 	assert.False(t, exists)
100 104
 
101 105
 	// Try deleting one with ID = 0
102
-	err = DeleteItemByID(0)
106
+	err = DeleteItemByID(0, &doer)
103 107
 	assert.Error(t, err)
104 108
 	assert.True(t, IsErrIDCannotBeZero(err))
105 109
 }

+ 10
- 6
models/publisher_test.go View File

@@ -9,6 +9,10 @@ func TestAddOrUpdatePublisher(t *testing.T) {
9 9
 	// Create test database
10 10
 	assert.NoError(t, PrepareTestDatabase())
11 11
 
12
+	// Get our doer
13
+	doer, _, err := GetUserByID(1)
14
+	assert.NoError(t, err)
15
+
12 16
 	// Bootstrap our test publisher
13 17
 	testpublisher := Publisher{
14 18
 		Name: "Testpublisher",
@@ -21,7 +25,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
21 25
 	for _, publisher := range allpublishers {
22 26
 
23 27
 		// Delete
24
-		err = DeletePublisherByID(publisher.ID)
28
+		err = DeletePublisherByID(publisher.ID, &doer)
25 29
 		assert.NoError(t, err)
26 30
 
27 31
 		// Check if it is gone
@@ -31,7 +35,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
31 35
 	}
32 36
 
33 37
 	// Create a new publisher
34
-	publisher1, err := AddOrUpdatePublisher(testpublisher)
38
+	publisher1, err := AddOrUpdatePublisher(testpublisher, &doer)
35 39
 	assert.NoError(t, err)
36 40
 	assert.Equal(t, testpublisher.Name, publisher1.Name)
37 41
 
@@ -42,14 +46,14 @@ func TestAddOrUpdatePublisher(t *testing.T) {
42 46
 	assert.Equal(t, testpublisher.Name, gotpublisher.Name)
43 47
 
44 48
 	// Pass an empty publisher to see if it fails
45
-	_, err = AddOrUpdatePublisher(Publisher{})
49
+	_, err = AddOrUpdatePublisher(Publisher{}, &doer)
46 50
 	assert.Error(t, err)
47 51
 	assert.True(t, IsErrNoPublisherName(err))
48 52
 
49 53
 	// Update the publisher
50 54
 	testpublisher.ID = publisher1.ID
51 55
 	testpublisher.Name = "Lorem Ipsum"
52
-	publisher1updated, err := AddOrUpdatePublisher(testpublisher)
56
+	publisher1updated, err := AddOrUpdatePublisher(testpublisher, &doer)
53 57
 	assert.NoError(t, err)
54 58
 	assert.Equal(t, testpublisher.Name, publisher1updated.Name)
55 59
 
@@ -59,7 +63,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
59 63
 	assert.NotNil(t, allpublishers[0])
60 64
 
61 65
 	// Delete the publisher
62
-	err = DeletePublisherByID(publisher1.ID)
66
+	err = DeletePublisherByID(publisher1.ID, &doer)
63 67
 	assert.NoError(t, err)
64 68
 
65 69
 	// Check if it is gone
@@ -68,7 +72,7 @@ func TestAddOrUpdatePublisher(t *testing.T) {
68 72
 	assert.False(t, exists)
69 73
 
70 74
 	// Try deleting one with ID = 0
71
-	err = DeletePublisherByID(0)
75
+	err = DeletePublisherByID(0, &doer)
72 76
 	assert.Error(t, err)
73 77
 	assert.True(t, IsErrIDCannotBeZero(err))
74 78
 }

+ 21
- 17
models/user_test.go View File

@@ -9,6 +9,10 @@ func TestCreateUser(t *testing.T) {
9 9
 	// Create test database
10 10
 	assert.NoError(t, PrepareTestDatabase())
11 11
 
12
+	// Get our doer
13
+	doer, _, err := GetUserByID(1)
14
+	assert.NoError(t, err)
15
+
12 16
 	// Our dummy user for testing
13 17
 	dummyuser := User{
14 18
 		Name:     "noooem, dief",
@@ -19,45 +23,45 @@ func TestCreateUser(t *testing.T) {
19 23
 	}
20 24
 
21 25
 	// Delete every preexisting user to have a fresh start
22
-	_, err := x.Where("1 = 1").Delete(&User{})
26
+	_, err = x.Where("1 = 1").Delete(&User{})
23 27
 	assert.NoError(t, err)
24 28
 
25 29
 	allusers, err := ListUsers("")
26 30
 	assert.NoError(t, err)
27 31
 	for _, user := range allusers {
28 32
 		// Delete it
29
-		err := DeleteUserByID(user.ID)
33
+		err := DeleteUserByID(user.ID, &doer)
30 34
 		assert.NoError(t, err)
31 35
 	}
32 36
 
33 37
 	// Create a new user
34
-	createdUser, err := CreateUser(dummyuser)
38
+	createdUser, err := CreateUser(dummyuser, &doer)
35 39
 	assert.NoError(t, err)
36 40
 
37 41
 	// Create a second new user
38
-	createdUser2, err := CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password})
42
+	createdUser2, err := CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password}, &doer)
39 43
 	assert.NoError(t, err)
40 44
 
41 45
 	// Check if it fails to create the same user again
42
-	_, err = CreateUser(dummyuser)
46
+	_, err = CreateUser(dummyuser, &doer)
43 47
 	assert.Error(t, err)
44 48
 
45 49
 	// Check if it fails to create a user with just the same username
46
-	_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"})
50
+	_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"}, &doer)
47 51
 	assert.Error(t, err)
48 52
 	assert.True(t, IsErrUsernameExists(err))
49 53
 
50 54
 	// Check if it fails to create one with the same email
51
-	_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email})
55
+	_, err = CreateUser(User{Username: "noone", Password: "1234", Email: dummyuser.Email}, &doer)
52 56
 	assert.Error(t, err)
53 57
 	assert.True(t, IsErrUserEmailExists(err))
54 58
 
55 59
 	// Check if it fails to create a user without password and username
56
-	_, err = CreateUser(User{})
60
+	_, err = CreateUser(User{}, &doer)
57 61
 	assert.Error(t, err)
58 62
 	assert.True(t, IsErrNoUsernamePassword(err))
59 63
 
60
-	_, err = CreateUser(User{Name: "blub"})
64
+	_, err = CreateUser(User{Name: "blub"}, &doer)
61 65
 	assert.Error(t, err)
62 66
 	assert.True(t, IsErrNoUsernamePassword(err))
63 67
 
@@ -92,20 +96,20 @@ func TestCreateUser(t *testing.T) {
92 96
 
93 97
 	// Update the user
94 98
 	newname := "Test_te"
95
-	uuser, err := UpdateUser(User{ID: theuser.ID, Name: newname, Password: "444444"})
99
+	uuser, err := UpdateUser(User{ID: theuser.ID, Name: newname, Password: "444444"}, &doer)
96 100
 	assert.NoError(t, err)
97 101
 	assert.Equal(t, newname, uuser.Name)
98 102
 	assert.Equal(t, theuser.Password, uuser.Password) // Password should not change
99 103
 	assert.Equal(t, theuser.Username, uuser.Username) // Username should not change either
100 104
 
101 105
 	// Try updating one which does not exist
102
-	_, err = UpdateUser(User{ID: 99999, Username: "dg"})
106
+	_, err = UpdateUser(User{ID: 99999, Username: "dg"}, &doer)
103 107
 	assert.Error(t, err)
104 108
 	assert.True(t, IsErrUserDoesNotExist(err))
105 109
 
106 110
 	// Update a users password
107 111
 	newpassword := "55555"
108
-	err = UpdateUserPassword(theuser.ID, newpassword)
112
+	err = UpdateUserPassword(theuser.ID, newpassword, &doer)
109 113
 	assert.NoError(t, err)
110 114
 
111 115
 	// Check if it was changed
@@ -123,26 +127,26 @@ func TestCreateUser(t *testing.T) {
123 127
 	assert.True(t, len(all) > 0)
124 128
 
125 129
 	// Try updating the password of a nonexistent user (should fail)
126
-	err = UpdateUserPassword(9999, newpassword)
130
+	err = UpdateUserPassword(9999, newpassword, &doer)
127 131
 	assert.Error(t, err)
128 132
 	assert.True(t, IsErrUserDoesNotExist(err))
129 133
 
130 134
 	// Delete it
131
-	err = DeleteUserByID(theuser.ID)
135
+	err = DeleteUserByID(theuser.ID, &doer)
132 136
 	assert.NoError(t, err)
133 137
 
134 138
 	// Try deleting one with ID = 0
135
-	err = DeleteUserByID(0)
139
+	err = DeleteUserByID(0, &doer)
136 140
 	assert.Error(t, err)
137 141
 	assert.True(t, IsErrIDCannotBeZero(err))
138 142
 
139 143
 	// Try delete the last user (Should fail)
140
-	err = DeleteUserByID(createdUser2.ID)
144
+	err = DeleteUserByID(createdUser2.ID, &doer)
141 145
 	assert.Error(t, err)
142 146
 	assert.True(t, IsErrCannotDeleteLastUser(err))
143 147
 
144 148
 	// Log some user action
145
-	err = logAction("test something", User{ID: 1}, 1)
149
+	err = logAction(ActionTypeUserUpdated, &doer, 1)
146 150
 	assert.NoError(t, err)
147 151
 
148 152
 }

Loading…
Cancel
Save