use of com.torodb.core.dsl.backend.BackendTransactionJob in project torodb by torodb.
the class DefaultToBackendFunctionTest method testApply_noChanges.
@Test
public void testApply_noChanges() {
MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
});
BatchMetaDocPart allCreatedDocPart = mock(BatchMetaDocPart.class, settings);
doReturn(false).when(allCreatedDocPart).isCreatedOnCurrentBatch();
doReturn(Collections.emptyList()).when(allCreatedDocPart).getOnBatchModifiedMetaFields();
doReturn(Collections.emptyList()).when(allCreatedDocPart).getOnBatchModifiedMetaScalars();
DocPartData allCreatedData = mock(DocPartData.class);
given(allCreatedData.getMetaDocPart()).willReturn(allCreatedDocPart);
CollectionData collectionData = mock(CollectionData.class);
given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allCreatedData));
//when
Iterable<BackendTransactionJob> result = fun.apply(collectionData);
ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
//then
assertEquals("Expected 1 jobs to do, but " + resultList.size() + " were recived", 1, resultList.size());
{
Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allCreatedData)).findAny();
assertTrue(insertJob.isPresent());
}
}
use of com.torodb.core.dsl.backend.BackendTransactionJob in project torodb by torodb.
the class DefaultToBackendFunctionTest method testApply_newScalar.
@Test
public void testApply_newScalar() {
MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
});
BatchMetaDocPart withNewScalarDocPart = mock(BatchMetaDocPart.class, settings);
doReturn(false).when(withNewScalarDocPart).isCreatedOnCurrentBatch();
doReturn(Collections.emptyList()).when(withNewScalarDocPart).getOnBatchModifiedMetaFields();
doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.INTEGER))).when(withNewScalarDocPart).getOnBatchModifiedMetaScalars();
DocPartData withNewScalar = mock(DocPartData.class);
given(withNewScalar.getMetaDocPart()).willReturn(withNewScalarDocPart);
CollectionData collectionData = mock(CollectionData.class);
given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(withNewScalar));
//when
Iterable<BackendTransactionJob> result = fun.apply(collectionData);
ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
//then
assertEquals("Expected 2 jobs to do, but " + resultList.size() + " were recived", 2, resultList.size());
{
Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(withNewScalar)).findAny();
assertTrue(insertJob.isPresent());
Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddScalarDddlJob)) {
return false;
}
AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
return castedJob.getDocPart().equals(withNewScalarDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.INTEGER);
}).findAny();
assertTrue(addScalarJob.isPresent());
int addScalarIndex = resultList.indexOf(addScalarJob.get());
int insertIndex = resultList.indexOf(insertJob.get());
assert addScalarIndex >= 0;
assert insertIndex >= 0;
assertTrue("For a given doc part, all related add scalar jobs must be executed before insert " + "jobs, but in this case the add scalr job has index " + addScalarIndex + " and the insert job has index " + insertIndex, addScalarIndex < insertIndex);
}
}
Aggregations