DATAMONGO-978 - Derived delete query should pass on type information.

We now pass on type information for derived delete queries to the according delete operation. This propagates the information correctly to the according Before and After events.

Before this change the type would have been set to null in case of non collection like method return type.

Original pull request: #199.
This commit is contained in:
Christoph Strobl
2014-07-03 08:52:51 +02:00
committed by Thomas Darimont
parent cd68a8db54
commit 998bb09a92
2 changed files with 8 additions and 5 deletions

View File

@@ -408,7 +408,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
return operations.findAllAndRemove(query, metadata.getJavaType()); return operations.findAllAndRemove(query, metadata.getJavaType());
} }
WriteResult writeResult = operations.remove(query, metadata.getCollectionName()); WriteResult writeResult = operations.remove(query, metadata.getJavaType(), metadata.getCollectionName());
return writeResult != null ? writeResult.getN() : 0L; return writeResult != null ? writeResult.getN() : 0L;
} }
} }

View File

@@ -54,7 +54,7 @@ import com.mongodb.WriteResult;
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class AbstracMongoQueryUnitTests { public class AbstractMongoQueryUnitTests {
@Mock RepositoryMetadata metadataMock; @Mock RepositoryMetadata metadataMock;
@Mock MongoOperations mongoOperationsMock; @Mock MongoOperations mongoOperationsMock;
@@ -88,7 +88,8 @@ public class AbstracMongoQueryUnitTests {
createQueryForMethod("deletePersonByLastname", String.class).setDeleteQuery(true).execute(new Object[] { "booh" }); createQueryForMethod("deletePersonByLastname", String.class).setDeleteQuery(true).execute(new Object[] { "booh" });
verify(this.mongoOperationsMock, times(1)).remove(Matchers.any(Query.class), Matchers.eq("persons")); verify(this.mongoOperationsMock, times(1)).remove(Matchers.any(Query.class), Matchers.eq(Person.class),
Matchers.eq("persons"));
verify(this.mongoOperationsMock, times(0)).find(Matchers.any(Query.class), Matchers.any(Class.class), verify(this.mongoOperationsMock, times(0)).find(Matchers.any(Query.class), Matchers.any(Class.class),
Matchers.anyString()); Matchers.anyString());
} }
@@ -122,19 +123,21 @@ public class AbstracMongoQueryUnitTests {
/** /**
* @see DATAMONGO-566 * @see DATAMONGO-566
* @see DATAMONGO-978
*/ */
@Test @Test
public void testDeleteExecutionReturnsNrDocumentsDeletedFromWriteResult() { public void testDeleteExecutionReturnsNrDocumentsDeletedFromWriteResult() {
when(writeResultMock.getN()).thenReturn(100); when(writeResultMock.getN()).thenReturn(100);
when(this.mongoOperationsMock.remove(Matchers.any(Query.class), Matchers.eq("persons"))) when(this.mongoOperationsMock.remove(Matchers.any(Query.class), Matchers.eq(Person.class), Matchers.eq("persons")))
.thenReturn(writeResultMock); .thenReturn(writeResultMock);
MongoQueryFake query = createQueryForMethod("deletePersonByLastname", String.class); MongoQueryFake query = createQueryForMethod("deletePersonByLastname", String.class);
query.setDeleteQuery(true); query.setDeleteQuery(true);
assertThat(query.execute(new Object[] { "fake" }), is((Object) 100L)); assertThat(query.execute(new Object[] { "fake" }), is((Object) 100L));
verify(this.mongoOperationsMock, times(1)).remove(Matchers.any(Query.class), Matchers.eq("persons")); verify(this.mongoOperationsMock, times(1)).remove(Matchers.any(Query.class), Matchers.eq(Person.class),
Matchers.eq("persons"));
} }
private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) { private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) {