stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.6 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/internal/slerrors"
  5. "git.aiterp.net/stufflog/server/models"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. )
  10. var item1ImageURL = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
  11. var item1 = models.Item{
  12. Name: "Salmon Fillet - 500g",
  13. Description: "Best fish",
  14. Tags: []string{"Groceries"},
  15. ImageURL: &item1ImageURL,
  16. }
  17. var item1Updated = models.Item{
  18. Name: "Salmon Fillet - 450g",
  19. Description: "Do not handle under suspicious circumstances.",
  20. Tags: []string{"Groceries"},
  21. ImageURL: nil,
  22. }
  23. var item2 = models.Item{
  24. Name: "Tape - Basic",
  25. Description: "",
  26. Tags: []string{"Groceries", "Hardware", "Office Supplies"},
  27. ImageURL: nil,
  28. }
  29. var item3 = models.Item{
  30. Name: "Flour - Wheat - 1kg",
  31. Description: "For bread and stuff",
  32. Tags: []string{"Groceries"},
  33. ImageURL: nil,
  34. }
  35. var item4 = models.Item{
  36. Name: "Flour - Wheat - 2kg",
  37. Description: "For more bread and stuff",
  38. Tags: []string{"Groceries"},
  39. ImageURL: nil,
  40. }
  41. func TestItemRepository(t *testing.T) {
  42. items := testDB.items
  43. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  44. defer cancel()
  45. assert.NoError(t, clearTable("item"))
  46. assert.NoError(t, clearTable("item_tag"))
  47. // INSERT
  48. // IDs are random, so test data needs to be changed before comparison.
  49. result, err := items.Insert(ctx, item1)
  50. assert.NoError(t, err)
  51. if result != nil {
  52. item1.ID = result.ID
  53. item1Updated.ID = result.ID
  54. }
  55. assert.Equal(t, item1, *result)
  56. result, err = items.Insert(ctx, item2)
  57. assert.NoError(t, err)
  58. if result != nil {
  59. item2.ID = result.ID
  60. }
  61. assert.Equal(t, item2, *result)
  62. result, err = items.Insert(ctx, item3)
  63. assert.NoError(t, err)
  64. if result != nil {
  65. item3.ID = result.ID
  66. }
  67. assert.Equal(t, item3, *result)
  68. result, err = items.Insert(ctx, item4)
  69. assert.NoError(t, err)
  70. if result != nil {
  71. item4.ID = result.ID
  72. }
  73. assert.Equal(t, item4, *result)
  74. if t.Failed() {
  75. return
  76. }
  77. // FIND
  78. result, err = items.Find(ctx, item1.ID)
  79. assert.NoError(t, err)
  80. assert.Equal(t, &item1, result)
  81. result, err = items.Find(ctx, item2.ID)
  82. assert.NoError(t, err)
  83. assert.Equal(t, &item2, result)
  84. // FIND't
  85. result, err = items.Find(ctx, "Iobviouslyinvalidid")
  86. assert.Error(t, err)
  87. assert.True(t, slerrors.IsNotFound(err))
  88. assert.Nil(t, result)
  89. // LIST
  90. results, err := items.List(ctx, models.ItemFilter{})
  91. assert.NoError(t, err)
  92. assert.Equal(t, []*models.Item{&item3, &item4, &item1, &item2}, results)
  93. results, err = items.List(ctx, models.ItemFilter{
  94. Tags: []string{"Hardware"},
  95. })
  96. assert.NoError(t, err)
  97. assert.Equal(t, []*models.Item{&item2}, results)
  98. results, err = items.List(ctx, models.ItemFilter{
  99. ItemIDs: []string{item1.ID, item2.ID},
  100. })
  101. assert.NoError(t, err)
  102. assert.Equal(t, []*models.Item{&item1, &item2}, results)
  103. // UPDATE
  104. err = items.Save(ctx, item1Updated)
  105. assert.NoError(t, err)
  106. // FIND after UPDATE
  107. result, err = items.Find(ctx, item1.ID)
  108. assert.NoError(t, err)
  109. assert.NotEqual(t, &item1, result)
  110. assert.Equal(t, &item1Updated, result)
  111. // TAGS
  112. allTags, err := items.GetTags(ctx)
  113. assert.NoError(t, err)
  114. assert.Equal(t, item2.Tags, allTags)
  115. // DELETE
  116. err = items.Delete(ctx, item2)
  117. assert.NoError(t, err)
  118. // LIST after DELETE
  119. results, err = items.List(ctx, models.ItemFilter{})
  120. assert.NoError(t, err)
  121. assert.Equal(t, []*models.Item{&item3, &item4, &item1Updated}, results)
  122. // TAGS after DELETE
  123. allTags, err = items.GetTags(ctx)
  124. assert.NoError(t, err)
  125. assert.Equal(t, item1.Tags, allTags)
  126. }