tests: improve repo package coverage (#3)

* refactor and add repo tests

* add CI name

* use atomic for test shutdown

* use go 1.19

* add timeout
This commit is contained in:
Hayden 2022-09-05 00:26:21 -08:00 committed by GitHub
parent 888ecfde34
commit 508e2e59bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 540 additions and 186 deletions

View file

@ -5,12 +5,9 @@ import (
"testing"
"github.com/hay-kot/content/backend/internal/types"
"github.com/hay-kot/content/backend/pkgs/faker"
"github.com/stretchr/testify/assert"
)
var fk = faker.NewFaker()
func locationFactory() types.LocationCreate {
return types.LocationCreate{
Name: fk.RandomString(10),
@ -18,28 +15,28 @@ func locationFactory() types.LocationCreate {
}
}
func Test_Locations_Get(t *testing.T) {
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
func TestLocationRepository_Get(t *testing.T) {
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
assert.NoError(t, err)
// Get by ID
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
assert.NoError(t, err)
assert.Equal(t, loc.ID, foundLoc.ID)
err = testRepos.Locations.Delete(context.Background(), loc.ID)
err = tRepos.Locations.Delete(context.Background(), loc.ID)
assert.NoError(t, err)
}
func Test_LocationsGetAllWithCount(t *testing.T) {
func TestLocationRepositoryGetAllWithCount(t *testing.T) {
ctx := context.Background()
result, err := testRepos.Locations.Create(ctx, testGroup.ID, types.LocationCreate{
result, err := tRepos.Locations.Create(ctx, tGroup.ID, types.LocationCreate{
Name: fk.RandomString(10),
Description: fk.RandomString(100),
})
assert.NoError(t, err)
_, err = testRepos.Items.Create(ctx, testGroup.ID, types.ItemCreate{
_, err = tRepos.Items.Create(ctx, tGroup.ID, types.ItemCreate{
Name: fk.RandomString(10),
Description: fk.RandomString(100),
LocationID: result.ID,
@ -47,7 +44,7 @@ func Test_LocationsGetAllWithCount(t *testing.T) {
assert.NoError(t, err)
results, err := testRepos.Locations.GetAll(context.Background(), testGroup.ID)
results, err := tRepos.Locations.GetAll(context.Background(), tGroup.ID)
assert.NoError(t, err)
for _, loc := range results {
@ -58,21 +55,21 @@ func Test_LocationsGetAllWithCount(t *testing.T) {
}
func Test_Locations_Create(t *testing.T) {
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
func TestLocationRepository_Create(t *testing.T) {
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
assert.NoError(t, err)
// Get by ID
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
assert.NoError(t, err)
assert.Equal(t, loc.ID, foundLoc.ID)
err = testRepos.Locations.Delete(context.Background(), loc.ID)
err = tRepos.Locations.Delete(context.Background(), loc.ID)
assert.NoError(t, err)
}
func Test_Locations_Update(t *testing.T) {
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
func TestLocationRepository_Update(t *testing.T) {
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
assert.NoError(t, err)
updateData := types.LocationUpdate{
@ -81,27 +78,27 @@ func Test_Locations_Update(t *testing.T) {
Description: fk.RandomString(100),
}
update, err := testRepos.Locations.Update(context.Background(), updateData)
update, err := tRepos.Locations.Update(context.Background(), updateData)
assert.NoError(t, err)
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
assert.NoError(t, err)
assert.Equal(t, update.ID, foundLoc.ID)
assert.Equal(t, update.Name, foundLoc.Name)
assert.Equal(t, update.Description, foundLoc.Description)
err = testRepos.Locations.Delete(context.Background(), loc.ID)
err = tRepos.Locations.Delete(context.Background(), loc.ID)
assert.NoError(t, err)
}
func Test_Locations_Delete(t *testing.T) {
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
func TestLocationRepository_Delete(t *testing.T) {
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
assert.NoError(t, err)
err = testRepos.Locations.Delete(context.Background(), loc.ID)
err = tRepos.Locations.Delete(context.Background(), loc.ID)
assert.NoError(t, err)
_, err = testRepos.Locations.Get(context.Background(), loc.ID)
_, err = tRepos.Locations.Get(context.Background(), loc.ID)
assert.Error(t, err)
}