|
| 1 | +// Copyright (c) 2024 Z5Labs and Contributors |
| 2 | +// |
| 3 | +// This software is released under the MIT License. |
| 4 | +// https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +package petstore |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/z5labs/humus/example/internal/petstorepb" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestInMemory_Add(t *testing.T) { |
| 18 | + t.Run("will add pet to store", func(t *testing.T) { |
| 19 | + t.Run("always", func(t *testing.T) { |
| 20 | + store := NewInMemory() |
| 21 | + |
| 22 | + store.Add(context.Background(), &petstorepb.Pet{ |
| 23 | + Id: 1, |
| 24 | + }) |
| 25 | + |
| 26 | + if !assert.Contains(t, store.pets, int64(1)) { |
| 27 | + return |
| 28 | + } |
| 29 | + }) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestInMemory_Get(t *testing.T) { |
| 34 | + t.Run("will not return pet", func(t *testing.T) { |
| 35 | + t.Run("if there are no pets in the store", func(t *testing.T) { |
| 36 | + store := NewInMemory() |
| 37 | + |
| 38 | + pet, found := store.Get(context.Background(), 1) |
| 39 | + if !assert.False(t, found) { |
| 40 | + return |
| 41 | + } |
| 42 | + if !assert.Nil(t, pet) { |
| 43 | + return |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("if the pet id is not found", func(t *testing.T) { |
| 48 | + store := NewInMemory() |
| 49 | + store.pets[1] = &petstorepb.Pet{} |
| 50 | + |
| 51 | + pet, found := store.Get(context.Background(), 2) |
| 52 | + if !assert.False(t, found) { |
| 53 | + return |
| 54 | + } |
| 55 | + if !assert.Nil(t, pet) { |
| 56 | + return |
| 57 | + } |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("will return pet", func(t *testing.T) { |
| 62 | + t.Run("if pet id is found", func(t *testing.T) { |
| 63 | + store := NewInMemory() |
| 64 | + store.pets[1] = &petstorepb.Pet{} |
| 65 | + |
| 66 | + pet, found := store.Get(context.Background(), 1) |
| 67 | + if !assert.True(t, found) { |
| 68 | + return |
| 69 | + } |
| 70 | + if !assert.NotNil(t, pet) { |
| 71 | + return |
| 72 | + } |
| 73 | + }) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +func TestInMemory_Delete(t *testing.T) { |
| 78 | + t.Run("will delete pet", func(t *testing.T) { |
| 79 | + t.Run("if the pet id is found", func(t *testing.T) { |
| 80 | + store := NewInMemory() |
| 81 | + store.pets[1] = &petstorepb.Pet{} |
| 82 | + |
| 83 | + store.Delete(context.Background(), 1) |
| 84 | + if !assert.Empty(t, store.pets) { |
| 85 | + return |
| 86 | + } |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("will be a no-op", func(t *testing.T) { |
| 91 | + t.Run("if the pet id is not found", func(t *testing.T) { |
| 92 | + store := NewInMemory() |
| 93 | + |
| 94 | + store.Delete(context.Background(), 1) |
| 95 | + if !assert.Empty(t, store.pets) { |
| 96 | + return |
| 97 | + } |
| 98 | + }) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func TestInMemory_Pets(t *testing.T) { |
| 103 | + t.Run("will return pets", func(t *testing.T) { |
| 104 | + t.Run("if there are pets in the store", func(t *testing.T) { |
| 105 | + store := NewInMemory() |
| 106 | + |
| 107 | + store.pets[1] = &petstorepb.Pet{Id: 1} |
| 108 | + store.pets[2] = &petstorepb.Pet{Id: 2} |
| 109 | + |
| 110 | + pets := store.Pets(context.Background()) |
| 111 | + if !assert.Len(t, pets, 2) { |
| 112 | + return |
| 113 | + } |
| 114 | + }) |
| 115 | + }) |
| 116 | +} |
0 commit comments