(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestCreateBook(t *testing.T) { |
| 30 | testCases := []struct { |
| 31 | userUSN int |
| 32 | expectedUSN int |
| 33 | label string |
| 34 | }{ |
| 35 | { |
| 36 | userUSN: 0, |
| 37 | expectedUSN: 1, |
| 38 | label: "js", |
| 39 | }, |
| 40 | { |
| 41 | userUSN: 3, |
| 42 | expectedUSN: 4, |
| 43 | label: "js", |
| 44 | }, |
| 45 | { |
| 46 | userUSN: 15, |
| 47 | expectedUSN: 16, |
| 48 | label: "css", |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for idx, tc := range testCases { |
| 53 | func() { |
| 54 | db := testutils.InitMemoryDB(t) |
| 55 | |
| 56 | user := testutils.SetupUserData(db, "user@test.com", "password123") |
| 57 | testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) |
| 58 | |
| 59 | anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") |
| 60 | testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) |
| 61 | |
| 62 | a := NewTest() |
| 63 | a.DB = db |
| 64 | a.Clock = clock.NewMock() |
| 65 | |
| 66 | book, err := a.CreateBook(user, tc.label) |
| 67 | if err != nil { |
| 68 | t.Fatal(errors.Wrap(err, "creating book")) |
| 69 | } |
| 70 | |
| 71 | var bookCount int64 |
| 72 | var bookRecord database.Book |
| 73 | var userRecord database.User |
| 74 | |
| 75 | if err := db.Model(&database.Book{}).Count(&bookCount).Error; err != nil { |
| 76 | t.Fatal(errors.Wrap(err, "counting books")) |
| 77 | } |
| 78 | if err := db.First(&bookRecord).Error; err != nil { |
| 79 | t.Fatal(errors.Wrap(err, "finding book")) |
| 80 | } |
| 81 | if err := db.Where("id = ?", user.ID).First(&userRecord).Error; err != nil { |
| 82 | t.Fatal(errors.Wrap(err, "finding user")) |
| 83 | } |
| 84 | |
| 85 | assert.Equal(t, bookCount, int64(1), "book count mismatch") |
| 86 | assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") |
nothing calls this directly
no test coverage detected