Skip to content

Commit

Permalink
collection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karngyan committed Dec 25, 2024
1 parent f5f276c commit 820b838
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ui_api/collections/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package collections_test

import (
"fmt"
"testing"

approvals "github.com/approvals/go-approval-tests"
"github.com/karngyan/maek/ui_api/testutil"
"github.com/stretchr/testify/assert"
)

func TestCreate(t *testing.T) {
defer testutil.TruncateTables()
cs := testutil.NewClientStateWithUser(t)

rr, err := cs.Post(fmt.Sprintf("/v1/workspaces/%d/collections", cs.Workspace.ID), nil)
assert.Nil(t, err)
assert.Equal(t, 201, rr.Code)

approvals.VerifyJSONBytes(t, rr.Body.Bytes())
}
68 changes: 68 additions & 0 deletions ui_api/collections/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package collections_test

import (
"context"
"fmt"
"testing"

approvals "github.com/approvals/go-approval-tests"
"github.com/karngyan/maek/domains/collections"
"github.com/karngyan/maek/ui_api/testutil"
"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
defer testutil.TruncateTables()

cs := testutil.NewClientStateWithUser(t)
cs2 := testutil.NewClientStateWithUserEmail(t, "[email protected]")

n, err := collections.CreateCollection(context.Background(), cs.Workspace.ID, cs.User.ID)
assert.Nil(t, err)

n2, err := collections.CreateCollection(context.Background(), cs2.Workspace.ID, cs2.User.ID)

assert.Nil(t, err)

var testCases = []struct {
name string
id int64
workspaceId int64
expectedStatus int
}{
{
name: "valid collection id",
id: n.ID,
workspaceId: cs.Workspace.ID,
expectedStatus: 200,
},
{
name: "invalid collection id",
id: -1,
workspaceId: cs.Workspace.ID,
expectedStatus: 400,
},
{
name: "not found collection id",
id: 5,
workspaceId: cs.Workspace.ID,
expectedStatus: 404,
},
{
name: "collection id from different workspace",
id: n2.ID,
workspaceId: cs.Workspace.ID,
expectedStatus: 404,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rr, err := cs.Get(fmt.Sprintf("/v1/workspaces/%d/collections/%d", tc.workspaceId, tc.id))
assert.Nil(t, err)
assert.Equal(t, tc.expectedStatus, rr.Code)

approvals.VerifyJSONBytes(t, rr.Body.Bytes())
})
}
}
36 changes: 36 additions & 0 deletions ui_api/collections/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package collections_test

import (
"log"
"os"
"testing"

"github.com/approvals/go-approval-tests/reporters"

approvals "github.com/approvals/go-approval-tests"

"github.com/karngyan/maek/ui_api/testutil"
)

func TestMain(m *testing.M) {
testutil.FreezeTime()
os.Exit(runTests(m))
}

func runTests(m *testing.M) int {
testApp, err := testutil.StartTestApp()
if err != nil {
log.Println("Error initializing test app: ", err)
return 1
}
defer func() {
if err := testApp.Stop(); err != nil {
log.Println("Error stopping test app: ", err)
}
}()

approvals.UseFolder("./testdata")
approvals.UseReporter(reporters.NewQuietReporter())

return m.Run()
}
13 changes: 13 additions & 0 deletions ui_api/collections/testdata/create_test.TestCreate.approved.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"collection": {
"created": 1234567890,
"createdById": 1,
"description": "",
"id": 1,
"name": "",
"trashed": false,
"updated": 1234567890,
"updatedById": 1,
"workspaceId": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"error": "Collection not found"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"note_uuid": "collection_id is required"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"error": "Collection not found"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"collection": {
"created": 1234567890,
"createdById": 1,
"description": "",
"id": 1,
"name": "",
"trashed": false,
"updated": 1234567890,
"updatedById": 1,
"workspaceId": 1
}
}

0 comments on commit 820b838

Please sign in to comment.