Last active
August 29, 2021 06:31
-
-
Save percybolmer/95a065dcdf990bc771589a5287690495 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package memory | |
import ( | |
"testing" | |
"github.com/google/uuid" | |
"github.com/percybolmer/ddd-go/aggregate" | |
"github.com/percybolmer/ddd-go/domain/customer" | |
) | |
func TestMemory_GetCustomer(t *testing.T) { | |
type testCase struct { | |
name string | |
id uuid.UUID | |
expectedErr error | |
} | |
// Create a fake customer to add to repository | |
cust, err := aggregate.NewCustomer("Percy") | |
if err != nil { | |
t.Fatal(err) | |
} | |
id := cust.GetID() | |
// Create the repo to use, and add some test Data to it for testing | |
// Skip Factory for this | |
repo := MemoryRepository{ | |
customers: map[uuid.UUID]aggregate.Customer{ | |
id: cust, | |
}, | |
} | |
testCases := []testCase{ | |
{ | |
name: "No Customer By ID", | |
id: uuid.MustParse("f47ac10b-58cc-0372-8567-0e02b2c3d479"), | |
expectedErr: customer.ErrCustomerNotFound, | |
}, { | |
name: "Customer By ID", | |
id: id, | |
expectedErr: nil, | |
}, | |
} | |
for _, tc := range testCases { | |
t.Run(tc.name, func(t *testing.T) { | |
_, err := repo.Get(tc.id) | |
if err != tc.expectedErr { | |
t.Errorf("Expected error %v, got %v", tc.expectedErr, err) | |
} | |
}) | |
} | |
} | |
func TestMemory_AddCustomer(t *testing.T) { | |
type testCase struct { | |
name string | |
cust string | |
expectedErr error | |
} | |
testCases := []testCase{ | |
{ | |
name: "Add Customer", | |
cust: "Percy", | |
expectedErr: nil, | |
}, | |
} | |
for _, tc := range testCases { | |
t.Run(tc.name, func(t *testing.T) { | |
repo := MemoryRepository{ | |
customers: map[uuid.UUID]aggregate.Customer{}, | |
} | |
cust, err := aggregate.NewCustomer(tc.cust) | |
if err != nil { | |
t.Fatal(err) | |
} | |
err = repo.Add(cust) | |
if err != tc.expectedErr { | |
t.Errorf("Expected error %v, got %v", tc.expectedErr, err) | |
} | |
found, err := repo.Get(cust.GetID()) | |
if err != nil { | |
t.Fatal(err) | |
} | |
if found.GetID() != cust.GetID() { | |
t.Errorf("Expected %v, got %v", cust.GetID(), found.GetID()) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment