From b480bbefcc598c4fcd316873ab8d645acb46411f Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Fri, 10 Jul 2020 10:29:56 +0200 Subject: [PATCH] Backend: Add unit tests for internal/entity --- internal/entity/location_fixtures_test.go | 34 +++++++++++++++++++++++ internal/entity/location_test.go | 28 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 internal/entity/location_fixtures_test.go diff --git a/internal/entity/location_fixtures_test.go b/internal/entity/location_fixtures_test.go new file mode 100644 index 000000000..1bd0bebbc --- /dev/null +++ b/internal/entity/location_fixtures_test.go @@ -0,0 +1,34 @@ +package entity + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestLocationMap_Get(t *testing.T) { + t.Run("get existing location", func(t *testing.T) { + r := LocationFixtures.Get("mexico") + assert.Equal(t, "Adosada Platform", r.LocName) + assert.Equal(t, "s2:85d1ea7d382c", r.ID) + assert.IsType(t, Location{}, r) + }) + t.Run("get not existing location", func(t *testing.T) { + r := LocationFixtures.Get("Fusion 3333") + assert.Equal(t, "zz", r.ID) + assert.IsType(t, Location{}, r) + }) +} + +func TestLocationMap_Pointer(t *testing.T) { + t.Run("get existing location pointer", func(t *testing.T) { + r := LocationFixtures.Pointer("mexico") + assert.Equal(t, "Adosada Platform", r.LocName) + assert.Equal(t, "s2:85d1ea7d382c", r.ID) + assert.IsType(t, &Location{}, r) + }) + t.Run("get not existing location pointer", func(t *testing.T) { + r := LocationFixtures.Pointer("Fusion 444") + assert.Equal(t, "zz", r.ID) + assert.IsType(t, &Location{}, r) + }) +} diff --git a/internal/entity/location_test.go b/internal/entity/location_test.go index 02e56f1e7..871355e23 100644 --- a/internal/entity/location_test.go +++ b/internal/entity/location_test.go @@ -44,6 +44,11 @@ func TestLocation_Keywords(t *testing.T) { r := m.Keywords() assert.Equal(t, []string{"camping", "caravan", "kwazulu-natal", "lobotes", "mandeni", "park", "south-africa"}, r) }) + t.Run("place id empty", func(t *testing.T) { + m := &Location{} + r := m.Keywords() + assert.Empty(t, r) + }) } func TestLocation_Find(t *testing.T) { @@ -63,3 +68,26 @@ func TestLocation_Find(t *testing.T) { assert.Equal(t, "maps: reverse lookup disabled", err.Error()) }) } + +func TestFirstOrCreateLocation(t *testing.T) { + t.Run("id empty", func(t *testing.T) { + loc := &Location{} + + assert.Nil(t, FirstOrCreateLocation(loc)) + }) + t.Run("place id empty", func(t *testing.T) { + loc := &Location{ID: "1234jhy"} + + assert.Nil(t, FirstOrCreateLocation(loc)) + }) + t.Run("success", func(t *testing.T) { + loc := LocationFixtures.Pointer("caravan park") + + result := FirstOrCreateLocation(loc) + + if result == nil { + t.Fatal("result should not be nil") + } + assert.NotEmpty(t, result.ID) + }) +}