From 2834f3f641b520e17ad1217c7f9e9008b862c361 Mon Sep 17 00:00:00 2001 From: erikzeneco <97616465+erikzeneco@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:11:59 +0100 Subject: [PATCH] check isUpsert header in POST document request (#5) * check isUpsert header in POST document request * Verify response code on "CreateItem that already exists" test --------- Co-authored-by: Pijus Kamandulis --- api/handlers/documents.go | 6 +++ api/tests/documents_test.go | 88 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/api/handlers/documents.go b/api/handlers/documents.go index a68e2c8..964ee92 100644 --- a/api/handlers/documents.go +++ b/api/handlers/documents.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" jsonpatch "github.com/evanphx/json-patch/v5" "github.com/gin-gonic/gin" @@ -223,6 +224,11 @@ func DocumentsPost(c *gin.Context) { return } + isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert")) + if isUpsert { + repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string)) + } + createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody) if status == repositorymodels.Conflict { c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) diff --git a/api/tests/documents_test.go b/api/tests/documents_test.go index beb0f5b..33492ad 100644 --- a/api/tests/documents_test.go +++ b/api/tests/documents_test.go @@ -220,4 +220,92 @@ func Test_Documents_Patch(t *testing.T) { panic(err) } }) + + t.Run("CreateItem", func(t *testing.T) { + context := context.TODO() + + item := map[string]interface{}{ + "Id": "6789011", + "pk": "456", + "newField": "newValue2", + } + bytes, err := json.Marshal(item) + assert.Nil(t, err) + + r, err2 := collectionClient.CreateItem( + context, + azcosmos.PartitionKey{}, + bytes, + &azcosmos.ItemOptions{ + EnableContentResponseOnWrite: false, + }, + ) + assert.NotNil(t, r) + assert.Nil(t, err2) + }) + + t.Run("CreateItem that already exists", func(t *testing.T) { + context := context.TODO() + + item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}} + bytes, err := json.Marshal(item) + assert.Nil(t, err) + + r, err := collectionClient.CreateItem( + context, + azcosmos.PartitionKey{}, + bytes, + &azcosmos.ItemOptions{ + EnableContentResponseOnWrite: false, + }, + ) + assert.NotNil(t, r) + assert.NotNil(t, err) + + var respErr *azcore.ResponseError + if errors.As(err, &respErr) { + assert.Equal(t, http.StatusConflict, respErr.StatusCode) + } else { + panic(err) + } + }) + + t.Run("UpsertItem new", func(t *testing.T) { + context := context.TODO() + + item := map[string]interface{}{"id": "123456", "pk": "1234", "isCool": false, "arr": []int{1, 2, 3}} + bytes, err := json.Marshal(item) + assert.Nil(t, err) + + r, err2 := collectionClient.UpsertItem( + context, + azcosmos.PartitionKey{}, + bytes, + &azcosmos.ItemOptions{ + EnableContentResponseOnWrite: false, + }, + ) + assert.NotNil(t, r) + assert.Nil(t, err2) + }) + + t.Run("UpsertItem that already exists", func(t *testing.T) { + context := context.TODO() + + item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3, 4}} + bytes, err := json.Marshal(item) + assert.Nil(t, err) + + r, err2 := collectionClient.UpsertItem( + context, + azcosmos.PartitionKey{}, + bytes, + &azcosmos.ItemOptions{ + EnableContentResponseOnWrite: false, + }, + ) + assert.NotNil(t, r) + assert.Nil(t, err2) + }) + }