mirror of https://github.com/pikami/cosmium.git
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 <pikami@users.noreply.github.com>
This commit is contained in:
parent
a6b5d32ff7
commit
2834f3f641
|
@ -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"})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue