use of com.torodb.core.dsl.backend.AddFieldDdlJob in project torodb by torodb.
the class DefaultToBackendFunctionTest method testApply_newField.
@Test
public void testApply_newField() {
MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
});
BatchMetaDocPart withNewFieldsDocPart = mock(BatchMetaDocPart.class, settings);
doReturn(false).when(withNewFieldsDocPart).isCreatedOnCurrentBatch();
doReturn(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.INTEGER))).when(withNewFieldsDocPart).getOnBatchModifiedMetaFields();
doReturn(Collections.emptyList()).when(withNewFieldsDocPart).getOnBatchModifiedMetaScalars();
DocPartData withNewData = mock(DocPartData.class);
given(withNewData.getMetaDocPart()).willReturn(withNewFieldsDocPart);
CollectionData collectionData = mock(CollectionData.class);
given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(withNewData));
//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(withNewData)).findAny();
assertTrue(insertJob.isPresent());
Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddFieldDdlJob)) {
return false;
}
AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
return castedJob.getDocPart().equals(withNewFieldsDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
}).findAny();
assertTrue(addFieldJob.isPresent());
int addFieldIndex = resultList.indexOf(addFieldJob.get());
int insertIndex = resultList.indexOf(insertJob.get());
assert addFieldIndex >= 0;
assert insertIndex >= 0;
assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
}
}
use of com.torodb.core.dsl.backend.AddFieldDdlJob in project torodb by torodb.
the class DefaultToBackendFunctionTest method testApply_newDocPart.
@Test
public void testApply_newDocPart() {
MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
});
BatchMetaDocPart allNewDocPart = mock(BatchMetaDocPart.class, settings);
doReturn(true).when(allNewDocPart).isCreatedOnCurrentBatch();
doReturn(Lists.newArrayList(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.BOOLEAN))).stream()).when(allNewDocPart).streamFields();
doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.BOOLEAN)).stream()).when(allNewDocPart).streamScalars();
DocPartData allNewData = mock(DocPartData.class);
given(allNewData.getMetaDocPart()).willReturn(allNewDocPart);
CollectionData collectionData = mock(CollectionData.class);
given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allNewData));
//when
Iterable<BackendTransactionJob> result = fun.apply(collectionData);
ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
//then
assertEquals("Expected 4 jobs to do, but " + resultList.size() + " were recived", 4, resultList.size());
{
Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allNewData)).findAny();
assertTrue(insertJob.isPresent());
Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddFieldDdlJob)) {
return false;
}
AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
}).findAny();
assertTrue(addFieldJob.isPresent());
Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddScalarDddlJob)) {
return false;
}
AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.BOOLEAN);
}).findAny();
assertTrue(addScalarJob.isPresent());
Optional<BackendTransactionJob> createDocPartJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddDocPartDdlJob)) {
return false;
}
AddDocPartDdlJob castedJob = (AddDocPartDdlJob) job;
return castedJob.getDocPart().equals(allNewDocPart);
}).findAny();
assertTrue(createDocPartJob.isPresent());
int createDocPartIndex = resultList.indexOf(createDocPartJob.get());
int addFieldIndex = resultList.indexOf(addFieldJob.get());
int addScalarIndex = resultList.indexOf(addScalarJob.get());
int insertIndex = resultList.indexOf(insertJob.get());
assert createDocPartIndex >= 0;
assert addFieldIndex >= 0;
assert addScalarIndex >= 0;
assert insertIndex >= 0;
assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
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);
assertTrue("For a given doc part, all related create doc part jobs must be executed " + "before add field jobs, but in this case the create doc part job has index " + createDocPartIndex + " and " + "the add field job has index " + addFieldIndex, createDocPartIndex < addFieldIndex);
}
}
Aggregations