-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea151e
commit e136342
Showing
6 changed files
with
101 additions
and
19 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,38 @@ | ||
package database | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/josephspurrier/gocleanarchitecture/domain/user" | ||
) | ||
|
||
// MockService represents a service for managing users. | ||
type MockService struct { | ||
records []user.Item | ||
mutex sync.RWMutex | ||
} | ||
|
||
// Reads reads database. | ||
func (c *MockService) Read() error { | ||
return nil | ||
} | ||
|
||
// Write saves the database. | ||
func (c *MockService) Write() error { | ||
return nil | ||
} | ||
|
||
// AddRecord adds a record to the database. | ||
func (c *MockService) AddRecord(rec user.Item) { | ||
c.mutex.Lock() | ||
c.records = append(c.records, rec) | ||
c.mutex.Unlock() | ||
} | ||
|
||
// Records retrieves all records from the database. | ||
func (c *MockService) Records() []user.Item { | ||
c.mutex.RLock() | ||
r := c.records | ||
c.mutex.RUnlock() | ||
return r | ||
} |
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,23 @@ | ||
package database_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/josephspurrier/gocleanarchitecture/database" | ||
"github.com/josephspurrier/gocleanarchitecture/domain/user" | ||
) | ||
|
||
// TestMockService ensures the mock service works correctly. | ||
func TestMockService(t *testing.T) { | ||
// Test the reading and writing. | ||
s := new(database.MockService) | ||
AssertEqual(t, s.Read(), nil) | ||
AssertEqual(t, s.Write(), nil) | ||
|
||
// Test adding a record and reading it. | ||
u := new(user.Item) | ||
u.Email = "jdoe@example.com" | ||
u.Password = "Pa$$w0rd" | ||
s.AddRecord(*u) | ||
AssertEqual(t, len(s.Records()), 1) | ||
} |
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,17 @@ | ||
package database_test | ||
|
||
import "testing" | ||
|
||
// AssertEqual throws an error if the two values are not equal. | ||
func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) { | ||
if actualValue != expectedValue { | ||
t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue) | ||
} | ||
} | ||
|
||
// AssertNotNil throws an error if the value is nil. | ||
func AssertNotNil(t *testing.T, actualValue interface{}) { | ||
if actualValue == nil { | ||
t.Errorf("\n got: %v\ndidn't want: %v", actualValue, nil) | ||
} | ||
} |