Skip to content

Commit

Permalink
Fix #372: add tests to verify $date-value updates work as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
tatu-at-datastax committed Apr 28, 2023
1 parent e7e591e commit 6e18851
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,26 @@ public void mustHandleSubdoc() throws Exception {
UpdateClause updateClause = objectMapper.readValue(json, UpdateClause.class);
assertThat(updateClause.buildOperations()).hasSize(1).contains(operation);
}

@Test
public void mustHandleDate() throws Exception {
String json =
"""
{"$set" : {"dateType": {"$date": 123456}}}
""";
final UpdateOperation operation =
SetOperation.constructSet(
(ObjectNode)
objectMapper.readTree(
"""
{
"dateType": {
"$date": 123456
}
}
"""));
UpdateClause updateClause = objectMapper.readValue(json, UpdateClause.class);
assertThat(updateClause.buildOperations()).hasSize(1).contains(operation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,126 @@ public void projectionBeforeUpdate() {
}
}

@Nested
class FindOneAndUpdateWithDate {
@Test
public void setWithDateField() {
final String document =
"""
{
"_id": "doc1",
"username": "user1"
}
""";
insertDoc(document);
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"findOneAndUpdate": {
"filter" : {"_id" : "doc1"},
"update" : {"$set" : {"date": { "$date": 1234567890 }}},
"options": {"upsert": true}
}
}
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("data.document", jsonEquals(document))
.body("status.matchedCount", is(1))
.body("status.modifiedCount", is(1))
.body("errors", is(nullValue()));

// assert state after update
String expected =
"""
{
"_id": "doc1",
"username": "user1",
"date": { "$date": 1234567890 }
}
""";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"find": {
"filter" : {"_id" : "doc1"}
}
}
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("data.documents[0]", jsonEquals(expected));
}

@Test
public void unsetWithDateField() {
final String document =
"""
{
"_id": "doc1",
"createdAt": {
"$date": 1234567
}
}
""";
insertDoc(document);
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"findOneAndUpdate": {
"filter" : {"_id" : "doc1"},
"update" : {"$unset" : {"createdAt": 1}}
}
}
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("data.document", jsonEquals(document))
.body("status.matchedCount", is(1))
.body("status.modifiedCount", is(1))
.body("errors", is(nullValue()));

// assert state after update
String expected =
"""
{
"_id": "doc1"
}
""";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"find": {
"filter" : {"_id" : "doc1"}
}
}
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("data.documents[0]", jsonEquals(expected));
}
}

@AfterEach
public void cleanUpData() {
deleteAllDocuments();
Expand Down

0 comments on commit 6e18851

Please sign in to comment.