-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a very basic gin test example where we get the index page and check it contains a particular post in the mock database. To complete this, I had to mock the database by turning the previous declaration into an interface, and moving the concrete types into `SqlDatabase`. The mock is done in `DatabaseMock`.
- Loading branch information
1 parent
31a88c9
commit 082e970
Showing
15 changed files
with
288 additions
and
68 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,24 @@ | ||
name: Tests | ||
|
||
on: [workflow_call] | ||
|
||
jobs: | ||
linters: | ||
name: Tests 🧪 | ||
|
||
runs-on: ubuntu-latest | ||
container: | ||
image: mattgomes28/urchin-golang:0.2 | ||
options: --user 1001 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Generating templ files | ||
run: | | ||
templ generate | ||
shell: bash | ||
|
||
- name: Running Go Tests 🧪 | ||
run: | | ||
go test ./... -v |
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,95 @@ | ||
package app | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
shardedmap "github.com/zutto/shardedmap" | ||
) | ||
|
||
type TrueTimeMockValidator struct{} | ||
|
||
func (validator *TrueTimeMockValidator) IsValid(cache *EndpointCache) bool { | ||
return true | ||
} | ||
|
||
type FalseTimeMockValidator struct{} | ||
|
||
func (validator *FalseTimeMockValidator) IsValid(cache *EndpointCache) bool { | ||
return false | ||
} | ||
|
||
func makeTrueCacheMock() Cache { | ||
return &TimedCache{ | ||
cacheMap: shardedmap.NewShardMap(2), | ||
cacheTimeout: 10 * time.Second, | ||
estimatedSize: atomic.Uint64{}, | ||
validator: &TrueTimeMockValidator{}, | ||
} | ||
} | ||
|
||
func makeFalseCacheMock() Cache { | ||
return &TimedCache{ | ||
cacheMap: shardedmap.NewShardMap(2), | ||
cacheTimeout: 10 * time.Second, | ||
estimatedSize: atomic.Uint64{}, | ||
validator: &FalseTimeMockValidator{}, | ||
} | ||
} | ||
|
||
func TestCacheAddition(t *testing.T) { | ||
|
||
test_data := []struct { | ||
name string | ||
contents []byte | ||
}{ | ||
{"first", []byte("hello")}, | ||
{"second", []byte("the quick brown fox does some weird stuff")}, | ||
{"veryLonNameThatIsProbablyTooLong", []byte("Hello there my friends")}, | ||
{"nPe9Rkff6ER6EzAxPUIpxc8UBBLm71hhq2MO9hkQWisrfihUqv", []byte("oA7Hv1A7vOuZSKrPT4ZN5DGKNSHZqpLEvUA5hu54CMyIt8c78u")}, | ||
} | ||
|
||
cache := makeTrueCacheMock() | ||
|
||
rolling_size := uint64(0) | ||
for _, test_case := range test_data { | ||
|
||
rolling_size += uint64(len(test_case.contents)) | ||
err := cache.Store(test_case.name, test_case.contents) | ||
assert.Nil(t, err) | ||
assert.Equal(t, cache.Size(), rolling_size) | ||
|
||
endpoint_cache, err := cache.Get(test_case.name) | ||
assert.Nil(t, err) | ||
assert.Equal(t, endpoint_cache.contents, test_case.contents) | ||
} | ||
} | ||
|
||
func TestCacheFailure(t *testing.T) { | ||
|
||
test_data := []struct { | ||
name string | ||
contents []byte | ||
}{ | ||
{"first", []byte("hello")}, | ||
{"second", []byte("the quick brown fox does some weird stuff")}, | ||
{"veryLonNameThatIsProbablyTooLong", []byte("Hello there my friends")}, | ||
{"nPe9Rkff6ER6EzAxPUIpxc8UBBLm71hhq2MO9hkQWisrfihUqv", []byte("oA7Hv1A7vOuZSKrPT4ZN5DGKNSHZqpLEvUA5hu54CMyIt8c78u")}, | ||
} | ||
|
||
cache := makeFalseCacheMock() | ||
|
||
rolling_size := uint64(0) | ||
for _, test_case := range test_data { | ||
|
||
rolling_size += uint64(len(test_case.contents)) | ||
err := cache.Store(test_case.name, test_case.contents) | ||
assert.Nil(t, err) | ||
assert.Equal(t, cache.Size(), rolling_size) | ||
|
||
_, err = cache.Get(test_case.name) | ||
assert.NotNil(t, err) | ||
} | ||
} |
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.