github.com/jamiefdhurst/journal@v0.9.2/internal/app/model/journal_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 6 "github.com/jamiefdhurst/journal/internal/app" 7 pkgDb "github.com/jamiefdhurst/journal/pkg/database" 8 "github.com/jamiefdhurst/journal/test/mocks/database" 9 ) 10 11 func TestJournal_GetDate(t *testing.T) { 12 tables := []struct { 13 input string 14 output string 15 }{ 16 {"2018-05-10", "Thursday May 10, 2018"}, 17 {"200-00-00", ""}, 18 {"", ""}, 19 {"0000-00-00", ""}, 20 } 21 22 for _, table := range tables { 23 j := Journal{Date: table.input} 24 actual := j.GetDate() 25 if actual != table.output { 26 t.Errorf("Expected GetDate() to produce result of '%s', got '%s'", table.output, actual) 27 } 28 } 29 } 30 31 func TestJournal_GetEditableDate(t *testing.T) { 32 tables := []struct { 33 input string 34 output string 35 }{ 36 {"2018-05-10", "2018-05-10"}, 37 {"2018-05-10EXTRATHINGS", "2018-05-10"}, 38 {"200-00-00", ""}, 39 {"", ""}, 40 {"0000-00-00", "0000-00-00"}, 41 } 42 43 for _, table := range tables { 44 j := Journal{Date: table.input} 45 actual := j.GetEditableDate() 46 if actual != table.output { 47 t.Errorf("Expected GetEditableDate() to produce result of '%s', got '%s'", table.output, actual) 48 } 49 } 50 } 51 52 func TestJournal_GetExcerpt(t *testing.T) { 53 tables := []struct { 54 input string 55 output string 56 }{ 57 {"<p>Some simple text</p>", "Some simple text"}, 58 {"<p>Multiple</p><p>paragraphs, some with</p><p>multiple words</p>", "Multiple paragraphs, some with multiple words"}, 59 {"", ""}, 60 {"<p></p><p></p>", " "}, 61 {"<p>a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z</p>", "a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x..."}, 62 } 63 64 for _, table := range tables { 65 j := Journal{Content: table.input} 66 actual := j.GetExcerpt() 67 if actual != table.output { 68 t.Errorf("Expected GetExcerpt() to produce result of '%s', got '%s'", table.output, actual) 69 } 70 } 71 } 72 73 func TestJournals_CreateTable(t *testing.T) { 74 db := &database.MockSqlite{} 75 container := &app.Container{Db: db} 76 js := Journals{Container: container} 77 js.CreateTable() 78 if db.Queries != 1 { 79 t.Errorf("Expected 1 query to have been run") 80 } 81 } 82 83 func TestJournals_EnsureUniqueSlug(t *testing.T) { 84 db := &database.MockSqlite{} 85 db.ErrorMode = false 86 container := &app.Container{Db: db} 87 js := Journals{Container: container} 88 89 // Test no result 90 db.Rows = &database.MockRowsEmpty{} 91 actual := js.EnsureUniqueSlug("test", 0) 92 if actual != "test" { 93 t.Errorf("Expected EnsureUniqueSlug() to produce result of '%s', got '%s'", "test", actual) 94 } 95 96 // Test simple 97 db.Rows = &database.MockJournal_SingleRow{} 98 db.ExpectedArgument = "test" 99 actual = js.EnsureUniqueSlug("test", 0) 100 if actual != "test-1" { 101 t.Errorf("Expected EnsureUniqueSlug() to produce result of '%s', got '%s'", "test-1", actual) 102 } 103 104 db.Rows = &database.MockJournal_SingleRow{} 105 db.ExpectedArgument = "test-2" 106 actual = js.EnsureUniqueSlug("test", 2) 107 if actual != "test-3" { 108 t.Errorf("Expected EnsureUniqueSlug() to produce result of '%s', got '%s'", "test-3", actual) 109 } 110 } 111 112 func TestJournals_FetchAll(t *testing.T) { 113 114 // Test error 115 db := &database.MockSqlite{} 116 db.ErrorMode = true 117 container := &app.Container{Db: db} 118 js := Journals{Container: container} 119 journals := js.FetchAll() 120 if len(journals) > 0 { 121 t.Errorf("Expected empty result set returned when error received") 122 } 123 124 // Test empty result 125 db.ErrorMode = false 126 db.Rows = &database.MockRowsEmpty{} 127 journals = js.FetchAll() 128 if len(journals) > 0 { 129 t.Errorf("Expected empty result set returned") 130 } 131 132 // Test successful result 133 db.Rows = &database.MockJournal_MultipleRows{} 134 journals = js.FetchAll() 135 if len(journals) < 2 || journals[0].ID != 1 || journals[1].Content != "Content 2" { 136 t.Errorf("Expected 2 rows returned and with correct data") 137 } 138 } 139 140 func TestJournals_FetchPaginated(t *testing.T) { 141 142 // Test error 143 db := &database.MockSqlite{} 144 db.ErrorMode = true 145 container := &app.Container{Db: db} 146 js := Journals{Container: container} 147 journals, pagination := js.FetchPaginated(pkgDb.PaginationQuery{Page: 1, ResultsPerPage: 2}) 148 if len(journals) > 0 || pagination.TotalPages > 0 { 149 t.Error("Expected empty result set returned when error received") 150 } 151 152 // Test empty result 153 db.ErrorMode = false 154 db.Rows = &database.MockPagination_Result{TotalResults: 0} 155 journals, pagination = js.FetchPaginated(pkgDb.PaginationQuery{Page: 1, ResultsPerPage: 2}) 156 if len(journals) > 0 || pagination.TotalPages > 0 { 157 t.Error("Expected empty result set returned when no pages received") 158 } 159 160 // Test pages out of bounds 161 db.Rows = &database.MockPagination_Result{TotalResults: 2} 162 journals, pagination = js.FetchPaginated(pkgDb.PaginationQuery{Page: 4, ResultsPerPage: 2}) 163 if len(journals) > 0 || pagination.TotalPages != 1 { 164 t.Errorf("Expected empty result set with correct pages returned, instead received +%v", pagination) 165 } 166 167 // Test successful result 168 db.EnableMultiMode() 169 db.AppendResult(&database.MockPagination_Result{TotalResults: 4}) 170 db.AppendResult(&database.MockJournal_MultipleRows{}) 171 journals, pagination = js.FetchPaginated(pkgDb.PaginationQuery{Page: 1, ResultsPerPage: 2}) 172 if len(journals) != 2 || journals[0].ID != 1 || journals[1].Content != "Content 2" || pagination.TotalPages != 2 || pagination.TotalResults != 4 { 173 t.Errorf("Expected 2 rows returned and with correct data") 174 } 175 } 176 177 func TestJournals_FindBySlug(t *testing.T) { 178 // Test error 179 db := &database.MockSqlite{} 180 db.ErrorMode = true 181 container := &app.Container{Db: db} 182 js := Journals{Container: container} 183 journal := js.FindBySlug("example") 184 if journal.ID > 0 { 185 t.Errorf("Expected empty result set returned when error received") 186 } 187 188 // Test empty result 189 db.ErrorMode = false 190 db.Rows = &database.MockRowsEmpty{} 191 journal = js.FindBySlug("example") 192 if journal.ID > 0 { 193 t.Errorf("Expected empty result set returned") 194 } 195 196 // Test successful result 197 db.Rows = &database.MockJournal_SingleRow{} 198 db.ExpectedArgument = "slug" 199 journal = js.FindBySlug("slug") 200 if journal.ID != 1 || journal.Content != "Content" { 201 t.Errorf("Expected 1 row returned and with correct data") 202 } 203 204 // Test unexpected amount of rows 205 db.Rows = &database.MockJournal_MultipleRows{} 206 journal = js.FindBySlug("slug") 207 if journal.ID > 0 { 208 t.Errorf("Expected no rows when query returns more than one result") 209 } 210 } 211 212 func TestJournals_FindNext(t *testing.T) { 213 // Test error 214 db := &database.MockSqlite{} 215 db.ErrorMode = true 216 container := &app.Container{Db: db} 217 js := Journals{Container: container} 218 journal := js.FindNext(100) 219 if journal.ID > 0 { 220 t.Error("Expected empty result set returned when error received") 221 } 222 223 // Test empty result 224 db.ErrorMode = false 225 db.Rows = &database.MockRowsEmpty{} 226 journal = js.FindNext(100) 227 if journal.ID > 0 { 228 t.Error("Expected empty result set returned") 229 } 230 231 // Test successful result 232 db.Rows = &database.MockJournal_SingleRow{} 233 db.ExpectedArgument = "0" 234 journal = js.FindNext(0) 235 if journal.ID != 1 || journal.Content != "Content" { 236 t.Error("Expected 1 row returned and with correct data") 237 } 238 239 // Test unexpected amount of rows 240 db.Rows = &database.MockJournal_MultipleRows{} 241 journal = js.FindNext(0) 242 if journal.ID > 0 { 243 t.Error("Expected no rows when query returns more than one result") 244 } 245 } 246 247 func TestJournals_FindPrev(t *testing.T) { 248 // Test error 249 db := &database.MockSqlite{} 250 db.ErrorMode = true 251 container := &app.Container{Db: db} 252 js := Journals{Container: container} 253 journal := js.FindPrev(100) 254 if journal.ID > 0 { 255 t.Error("Expected empty result set returned when error received") 256 } 257 258 // Test empty result 259 db.ErrorMode = false 260 db.Rows = &database.MockRowsEmpty{} 261 journal = js.FindPrev(100) 262 if journal.ID > 0 { 263 t.Error("Expected empty result set returned") 264 } 265 266 // Test successful result 267 db.Rows = &database.MockJournal_SingleRow{} 268 db.ExpectedArgument = "2" 269 journal = js.FindPrev(2) 270 if journal.ID != 1 || journal.Content != "Content" { 271 t.Error("Expected 1 row returned and with correct data") 272 } 273 274 // Test unexpected amount of rows 275 db.Rows = &database.MockJournal_MultipleRows{} 276 journal = js.FindPrev(0) 277 if journal.ID > 0 { 278 t.Error("Expected no rows when query returns more than one result") 279 } 280 } 281 282 func TestJournals_Save(t *testing.T) { 283 db := &database.MockSqlite{Result: &database.MockResult{}} 284 db.Rows = &database.MockRowsEmpty{} 285 gs := &database.MockGiphyExtractor{} 286 container := &app.Container{Db: db} 287 js := Journals{Container: container, Gs: gs} 288 289 // Test with new Journal 290 journal := js.Save(Journal{ID: 0, Title: "Testing"}) 291 if journal.ID != 1 || journal.Title != "Testing" { 292 t.Error("Expected same Journal to have been returned with new ID") 293 } 294 295 // Test with same Journal 296 journal = js.Save(Journal{ID: 2, Title: "Testing 2"}) 297 if journal.ID != 2 || journal.Title != "Testing 2" { 298 t.Error("Expected same Journal to have been returned with new ID") 299 } 300 301 // Check Giphy calls 302 if gs.CalledTimes != 2 { 303 t.Error("Expected Giphy to have been called 2 times within test scope") 304 } 305 } 306 307 func TestSlugify(t *testing.T) { 308 tables := []struct { 309 input string 310 output string 311 }{ 312 {"A SIMPLE TITLE", "a-simple-title"}, 313 {"already-slugified", "already-slugified"}, 314 {" ", "---"}, 315 {"lower cased", "lower-cased"}, 316 {"Special!!!Characters@$%^&*(", "special---characters-------"}, 317 } 318 319 for _, table := range tables { 320 actual := Slugify(table.input) 321 if actual != table.output { 322 t.Errorf("Expected Slugify() to produce result of '%s', got '%s'", table.output, actual) 323 } 324 } 325 }