Skip to content

Commit

Permalink
check isUpsert header in POST document request (#5)
Browse files Browse the repository at this point in the history
* check isUpsert header in POST document request

* Verify response code on "CreateItem that already exists" test

---------

Co-authored-by: Pijus Kamandulis <[email protected]>
  • Loading branch information
erikzeneco and pikami authored Nov 1, 2024
1 parent a6b5d32 commit 2834f3f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/handlers/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"

jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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"})
Expand Down
88 changes: 88 additions & 0 deletions api/tests/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

}

0 comments on commit 2834f3f

Please sign in to comment.