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

package mysqldriver
import (
"context"
"git.aiterp.net/stufflog/server/internal/slerrors"
"git.aiterp.net/stufflog/server/models"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var item1ImageURL = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
var item1 = models.Item{
Name: "Salmon Fillet - 500g",
Description: "Best fish",
Tags: []string{"Groceries"},
ImageURL: &item1ImageURL,
}
var item1Updated = models.Item{
Name: "Salmon Fillet - 450g",
Description: "Do not handle under suspicious circumstances.",
Tags: []string{"Groceries"},
ImageURL: nil,
}
var item2 = models.Item{
Name: "Tape - Basic",
Description: "",
Tags: []string{"Groceries", "Hardware", "Office Supplies"},
ImageURL: nil,
}
var item3 = models.Item{
Name: "Flour - Wheat - 1kg",
Description: "For bread and stuff",
Tags: []string{"Groceries"},
ImageURL: nil,
}
var item4 = models.Item{
Name: "Flour - Wheat - 2kg",
Description: "For more bread and stuff",
Tags: []string{"Groceries"},
ImageURL: nil,
}
func TestItemRepository(t *testing.T) {
items := testDB.items
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
assert.NoError(t, clearTable("item"))
assert.NoError(t, clearTable("item_tag"))
// INSERT
// IDs are random, so test data needs to be changed before comparison.
result, err := items.Insert(ctx, item1)
assert.NoError(t, err)
if result != nil {
item1.ID = result.ID
item1Updated.ID = result.ID
}
assert.Equal(t, item1, *result)
result, err = items.Insert(ctx, item2)
assert.NoError(t, err)
if result != nil {
item2.ID = result.ID
}
assert.Equal(t, item2, *result)
result, err = items.Insert(ctx, item3)
assert.NoError(t, err)
if result != nil {
item3.ID = result.ID
}
assert.Equal(t, item3, *result)
result, err = items.Insert(ctx, item4)
assert.NoError(t, err)
if result != nil {
item4.ID = result.ID
}
assert.Equal(t, item4, *result)
if t.Failed() {
return
}
// FIND
result, err = items.Find(ctx, item1.ID)
assert.NoError(t, err)
assert.Equal(t, &item1, result)
result, err = items.Find(ctx, item2.ID)
assert.NoError(t, err)
assert.Equal(t, &item2, result)
// FIND't
result, err = items.Find(ctx, "Iobviouslyinvalidid")
assert.Error(t, err)
assert.True(t, slerrors.IsNotFound(err))
assert.Nil(t, result)
// LIST
results, err := items.List(ctx, models.ItemFilter{})
assert.NoError(t, err)
assert.Equal(t, []*models.Item{&item3, &item4, &item1, &item2}, results)
results, err = items.List(ctx, models.ItemFilter{
Tags: []string{"Hardware"},
})
assert.NoError(t, err)
assert.Equal(t, []*models.Item{&item2}, results)
results, err = items.List(ctx, models.ItemFilter{
ItemIDs: []string{item1.ID, item2.ID},
})
assert.NoError(t, err)
assert.Equal(t, []*models.Item{&item1, &item2}, results)
// UPDATE
err = items.Save(ctx, item1Updated)
assert.NoError(t, err)
// FIND after UPDATE
result, err = items.Find(ctx, item1.ID)
assert.NoError(t, err)
assert.NotEqual(t, &item1, result)
assert.Equal(t, &item1Updated, result)
// TAGS
allTags, err := items.GetTags(ctx)
assert.NoError(t, err)
assert.Equal(t, item2.Tags, allTags)
// DELETE
err = items.Delete(ctx, item2)
assert.NoError(t, err)
// LIST after DELETE
results, err = items.List(ctx, models.ItemFilter{})
assert.NoError(t, err)
assert.Equal(t, []*models.Item{&item3, &item4, &item1Updated}, results)
// TAGS after DELETE
allTags, err = items.GetTags(ctx)
assert.NoError(t, err)
assert.Equal(t, item1.Tags, allTags)
}