From 820b838b78fb196141a7c052fcb9778990d768ad Mon Sep 17 00:00:00 2001 From: karn Date: Wed, 25 Dec 2024 19:46:31 +0530 Subject: [PATCH] collection tests --- ui_api/collections/create_test.go | 21 ++++++ ui_api/collections/get_test.go | 68 +++++++++++++++++++ ui_api/collections/setup_test.go | 36 ++++++++++ .../create_test.TestCreate.approved.json | 13 ++++ ..._id_from_different_workspace.approved.json | 3 + ...estGet.invalid_collection_id.approved.json | 3 + ...tGet.not_found_collection_id.approved.json | 3 + ....TestGet.valid_collection_id.approved.json | 13 ++++ 8 files changed, 160 insertions(+) create mode 100644 ui_api/collections/create_test.go create mode 100644 ui_api/collections/get_test.go create mode 100644 ui_api/collections/setup_test.go create mode 100644 ui_api/collections/testdata/create_test.TestCreate.approved.json create mode 100644 ui_api/collections/testdata/get_test.TestGet.collection_id_from_different_workspace.approved.json create mode 100644 ui_api/collections/testdata/get_test.TestGet.invalid_collection_id.approved.json create mode 100644 ui_api/collections/testdata/get_test.TestGet.not_found_collection_id.approved.json create mode 100644 ui_api/collections/testdata/get_test.TestGet.valid_collection_id.approved.json diff --git a/ui_api/collections/create_test.go b/ui_api/collections/create_test.go new file mode 100644 index 0000000..20ef6fa --- /dev/null +++ b/ui_api/collections/create_test.go @@ -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()) +} diff --git a/ui_api/collections/get_test.go b/ui_api/collections/get_test.go new file mode 100644 index 0000000..dbc49f7 --- /dev/null +++ b/ui_api/collections/get_test.go @@ -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, "john@maek.ai") + + 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()) + }) + } +} diff --git a/ui_api/collections/setup_test.go b/ui_api/collections/setup_test.go new file mode 100644 index 0000000..c8504f2 --- /dev/null +++ b/ui_api/collections/setup_test.go @@ -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() +} diff --git a/ui_api/collections/testdata/create_test.TestCreate.approved.json b/ui_api/collections/testdata/create_test.TestCreate.approved.json new file mode 100644 index 0000000..5d4264f --- /dev/null +++ b/ui_api/collections/testdata/create_test.TestCreate.approved.json @@ -0,0 +1,13 @@ +{ + "collection": { + "created": 1234567890, + "createdById": 1, + "description": "", + "id": 1, + "name": "", + "trashed": false, + "updated": 1234567890, + "updatedById": 1, + "workspaceId": 1 + } +} \ No newline at end of file diff --git a/ui_api/collections/testdata/get_test.TestGet.collection_id_from_different_workspace.approved.json b/ui_api/collections/testdata/get_test.TestGet.collection_id_from_different_workspace.approved.json new file mode 100644 index 0000000..f5e59c2 --- /dev/null +++ b/ui_api/collections/testdata/get_test.TestGet.collection_id_from_different_workspace.approved.json @@ -0,0 +1,3 @@ +{ + "error": "Collection not found" +} \ No newline at end of file diff --git a/ui_api/collections/testdata/get_test.TestGet.invalid_collection_id.approved.json b/ui_api/collections/testdata/get_test.TestGet.invalid_collection_id.approved.json new file mode 100644 index 0000000..e5376c2 --- /dev/null +++ b/ui_api/collections/testdata/get_test.TestGet.invalid_collection_id.approved.json @@ -0,0 +1,3 @@ +{ + "note_uuid": "collection_id is required" +} \ No newline at end of file diff --git a/ui_api/collections/testdata/get_test.TestGet.not_found_collection_id.approved.json b/ui_api/collections/testdata/get_test.TestGet.not_found_collection_id.approved.json new file mode 100644 index 0000000..f5e59c2 --- /dev/null +++ b/ui_api/collections/testdata/get_test.TestGet.not_found_collection_id.approved.json @@ -0,0 +1,3 @@ +{ + "error": "Collection not found" +} \ No newline at end of file diff --git a/ui_api/collections/testdata/get_test.TestGet.valid_collection_id.approved.json b/ui_api/collections/testdata/get_test.TestGet.valid_collection_id.approved.json new file mode 100644 index 0000000..5d4264f --- /dev/null +++ b/ui_api/collections/testdata/get_test.TestGet.valid_collection_id.approved.json @@ -0,0 +1,13 @@ +{ + "collection": { + "created": 1234567890, + "createdById": 1, + "description": "", + "id": 1, + "name": "", + "trashed": false, + "updated": 1234567890, + "updatedById": 1, + "workspaceId": 1 + } +} \ No newline at end of file