forked from katzien/go-structure-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move validation for duplicate beers out of storage and to the service - change how we generate beer IDs, and from int to string - add a test for adding beers (with known limitations)
- Loading branch information
Showing
16 changed files
with
169 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package adding | ||
|
||
import ( | ||
"github.com/katzien/go-structure-examples/domain-hex/pkg/listing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestAddBeers(t *testing.T) { | ||
b1 := Beer{ | ||
Name: "Test Beer 1", | ||
Brewery: "Brewery One", | ||
Abv: 3.6, | ||
ShortDesc: "Lorem Ipsum", | ||
} | ||
|
||
b2 := Beer{ | ||
Name: "Test Beer 2", | ||
Brewery: "Brewery Two", | ||
Abv: 4.8, | ||
ShortDesc: "Bacon Ipsum", | ||
} | ||
|
||
mR := new(mockStorage) | ||
|
||
s := NewService(mR) | ||
|
||
err := s.AddBeer(b1, b2) | ||
require.NoError(t, err) | ||
|
||
beers := mR.GetAllBeers() | ||
assert.Len(t, beers, 2) | ||
} | ||
|
||
type mockStorage struct { | ||
beers []Beer | ||
} | ||
|
||
func (m *mockStorage) AddBeer(b Beer) error { | ||
m.beers = append(m.beers, b) | ||
|
||
return nil | ||
} | ||
|
||
func (m *mockStorage) GetAllBeers() []listing.Beer { | ||
beers := []listing.Beer{} | ||
|
||
for _, bb := range m.beers { | ||
b := listing.Beer{ | ||
Name: bb.Name, | ||
Brewery: bb.Brewery, | ||
Abv: bb.Abv, | ||
ShortDesc: bb.ShortDesc, | ||
} | ||
beers = append(beers, b) | ||
} | ||
|
||
return beers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package storage | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
) | ||
|
||
// GetID returns a random ID string of the format prefix_random16chars, e.g. beer_ts72nf6ak8dts73g. | ||
// This is a simple (naive) implementation using the rand package, | ||
// just to avoid importing external UUID packages in this demo app. | ||
// This implementation in no way guarantees uniqueness, so please don't use it for any production purposes! | ||
func GetID(prefix string) (string, error) { | ||
b := make([]byte, 8) | ||
_, err := rand.Read(b) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fmt.Sprintf("%s_%x", prefix, b), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetID(t *testing.T) { | ||
id, err := GetID("testing") | ||
|
||
require.NoError(t, err) | ||
|
||
assert.True(t, strings.HasPrefix(id, "testing_")) | ||
assert.Len(t, id, 24) | ||
} | ||
|
||
func TestGetIDEmptyPrefix(t *testing.T) { | ||
id, err := GetID("") | ||
|
||
require.NoError(t, err) | ||
|
||
assert.True(t, strings.HasPrefix(id, "_")) | ||
assert.Len(t, id, 17) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.