|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | +) |
| 9 | + |
| 10 | +type StoreSuite struct { |
| 11 | + suite.Suite |
| 12 | + store Storer |
| 13 | +} |
| 14 | + |
| 15 | +func (suite *StoreSuite) TestCreate() { |
| 16 | + a := assert.New(suite.T()) |
| 17 | + |
| 18 | + id, error := suite.store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: 450}) |
| 19 | + a.Equal(id, 0) |
| 20 | + a.Equal(error, nil) |
| 21 | +} |
| 22 | + |
| 23 | +func (suite *StoreSuite) TestCreateSecond() { |
| 24 | + a := assert.New(suite.T()) |
| 25 | + |
| 26 | + id, error := suite.store.Create(Puppy{Breed: "Boxer", Color: "Brown", Value: 300}) |
| 27 | + a.Equal(id, 1) |
| 28 | + a.Equal(error, nil) |
| 29 | +} |
| 30 | + |
| 31 | +func (suite *StoreSuite) TestCreateNegativeNumber() { |
| 32 | + a := assert.New(suite.T()) |
| 33 | + |
| 34 | + id, error := suite.store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: -100}) |
| 35 | + a.Equal(id, -1) |
| 36 | + a.Equal(error, NewError(NegativeValue)) |
| 37 | +} |
| 38 | + |
| 39 | +func (suite *StoreSuite) TestRead() { |
| 40 | + a := assert.New(suite.T()) |
| 41 | + |
| 42 | + data, error := suite.store.Read(0) |
| 43 | + a.Equal(data, Puppy{ID: 0, Breed: "Wolf", Color: "Grey", Value: 450}) |
| 44 | + a.Equal(error, nil) |
| 45 | +} |
| 46 | + |
| 47 | +func (suite *StoreSuite) TestReadNonExistent() { |
| 48 | + a := assert.New(suite.T()) |
| 49 | + |
| 50 | + success, error := suite.store.Read(100) |
| 51 | + a.Equal(success, Puppy{}) |
| 52 | + a.Equal(error, NewError(IDNotFound)) |
| 53 | +} |
| 54 | + |
| 55 | +func (suite *StoreSuite) TestUpdate() { |
| 56 | + a := assert.New(suite.T()) |
| 57 | + |
| 58 | + success, error := suite.store.Update(0, Puppy{Breed: "Doberman", Color: "Black", Value: 500}) |
| 59 | + a.Equal(success, true) |
| 60 | + a.Equal(error, nil) |
| 61 | + |
| 62 | + data, error := suite.store.Read(0) |
| 63 | + a.Equal(data, Puppy{ID: 0, Breed: "Doberman", Color: "Black", Value: 500}) |
| 64 | + a.Equal(error, nil) |
| 65 | +} |
| 66 | + |
| 67 | +func (suite *StoreSuite) TestUpdateNonExistent() { |
| 68 | + a := assert.New(suite.T()) |
| 69 | + |
| 70 | + success, error := suite.store.Update(100, Puppy{Breed: "Doberman", Color: "Black", Value: 500}) |
| 71 | + a.Equal(success, false) |
| 72 | + a.Equal(error, NewError(IDNotFound)) |
| 73 | +} |
| 74 | + |
| 75 | +func (suite *StoreSuite) TestUpdateNegativeNumber() { |
| 76 | + a := assert.New(suite.T()) |
| 77 | + |
| 78 | + success, error := suite.store.Update(0, Puppy{Breed: "Doberman", Color: "Black", Value: -500}) |
| 79 | + a.Equal(success, false) |
| 80 | + a.Equal(error, NewError(NegativeValue)) |
| 81 | +} |
| 82 | + |
| 83 | +func (suite *StoreSuite) TestDestroy() { |
| 84 | + a := assert.New(suite.T()) |
| 85 | + |
| 86 | + success, error := suite.store.Destroy(1) |
| 87 | + a.Equal(success, true) |
| 88 | + a.Equal(error, nil) |
| 89 | + |
| 90 | + data, error := suite.store.Read(1) |
| 91 | + a.Equal(data, Puppy{}) |
| 92 | + a.Equal(error, NewError(IDNotFound)) |
| 93 | +} |
| 94 | + |
| 95 | +func (suite *StoreSuite) TestDestroyNonExistent() { |
| 96 | + a := assert.New(suite.T()) |
| 97 | + |
| 98 | + success, error := suite.store.Destroy(100) |
| 99 | + a.Equal(success, false) |
| 100 | + a.Equal(error, NewError(IDNotFound)) |
| 101 | +} |
| 102 | + |
| 103 | +func (suite *StoreSuite) TestIdIncrementOnDelete() { |
| 104 | + a := assert.New(suite.T()) |
| 105 | + id, _ := suite.store.Create(Puppy{Breed: "Greyhound", Color: "Light Brown", Value: 700}) |
| 106 | + success, _ := suite.store.Destroy(id) |
| 107 | + a.Equal(success, true) |
| 108 | + |
| 109 | + newID, _ := suite.store.Create(Puppy{Breed: "Greyhound", Color: "Light Brown", Value: 700}) |
| 110 | + a.Equal(newID, 3) |
| 111 | +} |
| 112 | + |
| 113 | +func TestStore(t *testing.T) { |
| 114 | + suite.Run(t, &StoreSuite{store: NewMapStore()}) |
| 115 | + suite.Run(t, &StoreSuite{store: NewSyncStore()}) |
| 116 | +} |
0 commit comments