2021-04-23 23:07:18 +02:00
|
|
|
package models_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/photoview/photoview/api/graphql/models"
|
|
|
|
"github.com/photoview/photoview/api/test_utils"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-08-30 21:36:45 +02:00
|
|
|
func TestAlbumGetChildrenAndParents(t *testing.T) {
|
2021-04-23 23:07:18 +02:00
|
|
|
db := test_utils.DatabaseTest(t)
|
|
|
|
|
|
|
|
rootAlbum := models.Album{
|
|
|
|
Title: "root",
|
|
|
|
Path: "/photos",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !assert.NoError(t, db.Save(&rootAlbum).Error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
children := []models.Album{
|
|
|
|
{
|
|
|
|
Title: "child1",
|
|
|
|
Path: "/photos/child1",
|
|
|
|
ParentAlbumID: &rootAlbum.ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "child2",
|
|
|
|
Path: "/photos/child2",
|
|
|
|
ParentAlbumID: &rootAlbum.ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "not_child",
|
|
|
|
Path: "/videos",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !assert.NoError(t, db.Save(&children).Error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_child := models.Album{
|
|
|
|
Title: "subchild",
|
|
|
|
Path: "/photos/child1/subchild",
|
|
|
|
ParentAlbumID: &children[0].ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !assert.NoError(t, db.Save(&sub_child).Error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:36:45 +02:00
|
|
|
verifyResult := func(t *testing.T, expected_albums []*models.Album, result []*models.Album) {
|
|
|
|
assert.Equal(t, len(expected_albums), len(result))
|
2021-04-23 23:07:18 +02:00
|
|
|
|
2021-08-30 21:36:45 +02:00
|
|
|
for _, expected := range expected_albums {
|
|
|
|
found_expected := false
|
|
|
|
for _, item := range result {
|
|
|
|
if item.Title == expected.Title && item.Path == expected.Path {
|
|
|
|
found_expected = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_expected {
|
|
|
|
assert.Failf(t, "albums did not match", "expected to find item: %v", expected)
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 23:07:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 21:36:45 +02:00
|
|
|
t.Run("Album get children", func(t *testing.T) {
|
|
|
|
root_children, err := rootAlbum.GetChildren(db, nil)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-23 23:07:18 +02:00
|
|
|
|
2021-08-30 21:36:45 +02:00
|
|
|
expected_children := []*models.Album{
|
|
|
|
{
|
|
|
|
Title: "root",
|
|
|
|
Path: "/photos",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "child1",
|
|
|
|
Path: "/photos/child1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "child2",
|
|
|
|
Path: "/photos/child2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "subchild",
|
|
|
|
Path: "/photos/child1/subchild",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyResult(t, expected_children, root_children)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Album get parents", func(t *testing.T) {
|
|
|
|
parents, err := sub_child.GetParents(db, nil)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
2021-04-23 23:07:18 +02:00
|
|
|
}
|
2021-08-30 21:36:45 +02:00
|
|
|
|
|
|
|
expected_parents := []*models.Album{
|
|
|
|
{
|
|
|
|
Title: "root",
|
|
|
|
Path: "/photos",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "child1",
|
|
|
|
Path: "/photos/child1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "subchild",
|
|
|
|
Path: "/photos/child1/subchild",
|
|
|
|
},
|
2021-04-23 23:07:18 +02:00
|
|
|
}
|
2021-08-30 21:36:45 +02:00
|
|
|
|
|
|
|
verifyResult(t, expected_parents, parents)
|
|
|
|
})
|
2021-04-23 23:07:18 +02:00
|
|
|
|
|
|
|
}
|