use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class SelectStatementHandlerTest method handle_ScanOperationWithPartialMultipleClusteringKeysRangeInclusively_ShouldCallQueryItemsWithProperQuery.
@Test
public void handle_ScanOperationWithPartialMultipleClusteringKeysRangeInclusively_ShouldCallQueryItemsWithProperQuery() {
// Arrange
when(metadata.getPartitionKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_1)));
when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Arrays.asList(ANY_NAME_2, ANY_NAME_3)));
when(metadata.getClusteringOrder(any())).thenReturn(Order.ASC);
when(metadata.getClusteringOrders()).thenReturn(ImmutableMap.of(ANY_NAME_2, Order.ASC, ANY_NAME_3, Order.ASC));
when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
Scan scan = prepareScan().withStart(new Key(ANY_NAME_2, ANY_TEXT_2), true).withEnd(new Key(ANY_NAME_2, ANY_TEXT_3), true);
String expectedCondition = DynamoOperation.PARTITION_KEY + " = " + DynamoOperation.PARTITION_KEY_ALIAS + " AND " + DynamoOperation.CLUSTERING_KEY + " BETWEEN " + DynamoOperation.START_CLUSTERING_KEY_ALIAS + " AND " + DynamoOperation.END_CLUSTERING_KEY_ALIAS;
DynamoOperation dynamoOperation = new DynamoOperation(scan, metadata);
ByteBuffer partitionKey = dynamoOperation.getConcatenatedPartitionKey();
Map<String, AttributeValue> expectedBindMap = new HashMap<>();
expectedBindMap.put(DynamoOperation.PARTITION_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(partitionKey)).build());
expectedBindMap.put(DynamoOperation.START_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2), metadata.getClusteringOrders()))).build());
expectedBindMap.put(DynamoOperation.END_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(BytesUtils.getClosestNextBytes(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_3), metadata.getClusteringOrders())).get())).build());
// Act Assert
assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
// Assert
ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
verify(client).query(captor.capture());
QueryRequest actualRequest = captor.getValue();
assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedCondition);
assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingScanOperationWithIndexedColumnAsPartitionKey_shouldNotThrowAnyException.
@Test
public void whenCheckingScanOperationWithIndexedColumnAsPartitionKey_shouldNotThrowAnyException() {
// Arrange
Key partitionKey = new Key(COL1, 1);
Key startClusteringKey = null;
Key endClusteringKey = null;
List<String> projections = Arrays.asList(COL1, COL2, COL3);
int limit = 10;
Scan scan = new Scan(partitionKey).withStart(startClusteringKey).withStart(endClusteringKey).withProjections(projections).withLimit(limit).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatCode(() -> operationChecker.check(scan)).doesNotThrowAnyException();
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingScanOperationWithInvalidPartialOrdering_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingScanOperationWithInvalidPartialOrdering_shouldThrowIllegalArgumentException() {
// Arrange
Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
Key startClusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
Key endClusteringKey = new Key(CKEY1, 2, CKEY2, "val9");
List<String> projections = Arrays.asList(COL1, COL2, COL3);
int limit = -10;
Scan scan = new Scan(partitionKey).withStart(startClusteringKey).withEnd(endClusteringKey).withProjections(projections).withLimit(limit).withOrdering(new Scan.Ordering(CKEY2, Scan.Ordering.Order.ASC)).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(scan)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingScanOperationWithNonIndexedColumnAsPartitionKey_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingScanOperationWithNonIndexedColumnAsPartitionKey_shouldThrowIllegalArgumentException() {
// Arrange
Key partitionKey = new Key(COL2, 0.1d);
Key startClusteringKey = null;
Key endClusteringKey = null;
List<String> projections = Arrays.asList(COL1, COL2, COL3);
int limit = 10;
Scan scan = new Scan(partitionKey).withStart(startClusteringKey).withStart(endClusteringKey).withProjections(projections).withLimit(limit).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(scan)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class JdbcServiceTest method whenGetScannerExecuted_withScanAll_shouldCallQueryBuilder.
@Test
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION")
public void whenGetScannerExecuted_withScanAll_shouldCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.select(any())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.from(any(), any(), any())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.limit(anyInt())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.build()).thenReturn(selectQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(false);
// Act
Scan scan = new ScanAll().forNamespace(NAMESPACE).forTable(TABLE);
jdbcService.getScanner(scan, connection);
// Assert
verify(operationChecker).check(any(ScanAll.class));
verify(queryBuilder).select(any());
}
Aggregations