-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate WriteResult(s) (like update and delete counts) from Mongo b…
…ackend to Repository API. Fixes #1376
- Loading branch information
1 parent
29bca65
commit 729f330
Showing
4 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
criteria/common/test/org/immutables/criteria/backend/WriteResultsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2022 Immutables Authors and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.immutables.criteria.backend; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.immutables.check.Checkers.check; | ||
|
||
class WriteResultsTest { | ||
@Test | ||
void empty() { | ||
WriteResult empty = WriteResult.empty(); | ||
check(empty.insertedCount().getAsLong()).is(0L); | ||
check(empty.updatedCount().getAsLong()).is(0L); | ||
check(empty.deletedCount().getAsLong()).is(0L); | ||
check(empty.totalCount().getAsLong()).is(0L); | ||
} | ||
|
||
@Test | ||
void unknown() { | ||
ImmutableWriteResult unknown = WriteResult.unknown(); | ||
check(!unknown.insertedCount().isPresent()); | ||
check(!unknown.updatedCount().isPresent()); | ||
check(!unknown.deletedCount().isPresent()); | ||
check(!unknown.totalCount().isPresent()); | ||
check(unknown.withUpdatedCount(1).updatedCount().getAsLong()).is(1L); | ||
} | ||
|
||
@Test | ||
void totalCount() { | ||
ImmutableWriteResult empty = WriteResult.empty(); | ||
check(empty.totalCount().getAsLong()).is(0L); | ||
check(empty.withInsertedCount(1).totalCount().getAsLong()).is(1L); | ||
check(empty.withDeletedCount(1).withInsertedCount(1) | ||
.totalCount().getAsLong()).is(2L); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
criteria/mongo/test/org/immutables/criteria/mongo/MongoWriteResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2022 Immutables Authors and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.immutables.criteria.mongo; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.mongodb.reactivestreams.client.MongoDatabase; | ||
import org.immutables.criteria.backend.WriteResult; | ||
import org.immutables.criteria.personmodel.ImmutablePerson; | ||
import org.immutables.criteria.personmodel.Person; | ||
import org.immutables.criteria.personmodel.PersonCriteria; | ||
import org.immutables.criteria.personmodel.PersonGenerator; | ||
import org.immutables.criteria.personmodel.PersonRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.immutables.check.Checkers.check; | ||
|
||
/** | ||
* Validate results of a mongo write operation with {@link WriteResult}. | ||
*/ | ||
@ExtendWith(MongoExtension.class) | ||
class MongoWriteResultTest { | ||
private final PersonRepository repository; | ||
private final PersonCriteria person; | ||
|
||
private final PersonGenerator generator; | ||
|
||
MongoWriteResultTest(MongoDatabase database) { | ||
BackendResource backend = new BackendResource(database); | ||
this.repository = new PersonRepository(backend.backend()); | ||
this.person = PersonCriteria.person; | ||
this.generator = new PersonGenerator(); | ||
} | ||
|
||
@Test | ||
void insert() { | ||
WriteResult result = repository.insert(generator.next()); | ||
check(result.insertedCount().getAsLong()).is(1L); | ||
check(result.deletedCount().getAsLong()).is(0L); | ||
check(result.updatedCount().getAsLong()).is(0L); | ||
|
||
WriteResult result2 = repository.insertAll(ImmutableList.of(generator.next(), generator.next())); | ||
check(result2.insertedCount().getAsLong()).is(2L); | ||
check(result2.deletedCount().getAsLong()).is(0L); | ||
check(result2.updatedCount().getAsLong()).is(0L); | ||
} | ||
|
||
@Test | ||
void delete() { | ||
WriteResult result = repository.delete(person.id.is("__missing__")); | ||
check(result.insertedCount().getAsLong()).is(0L); | ||
check(result.deletedCount().getAsLong()).is(0L); | ||
check(result.updatedCount().getAsLong()).is(0L); | ||
check(result.totalCount().getAsLong()).is(0L); | ||
|
||
Person person1 = generator.next(); | ||
repository.insert(person1); | ||
WriteResult result2 = repository.delete(person.id.is(person1.id())); | ||
check(result2.deletedCount().getAsLong()).is(1L); | ||
} | ||
|
||
@Test | ||
void update() { | ||
WriteResult result1 = repository.update(person.id.is("aaa")).set(person.fullName, "name1").execute(); | ||
check(result1.updatedCount().getAsLong()).is(0L); | ||
check(result1.insertedCount().getAsLong()).is(0L); | ||
check(result1.deletedCount().getAsLong()).is(0L); | ||
|
||
ImmutablePerson person1 = generator.next(); | ||
repository.insert(person1); | ||
|
||
WriteResult result2 = repository.update(person.id.is(person1.id())).set(person.fullName, "name2").execute(); | ||
check(result2.updatedCount().getAsLong()).is(1L); | ||
check(result2.insertedCount().getAsLong()).is(0L); | ||
check(result2.deletedCount().getAsLong()).is(0L); | ||
} | ||
} |