Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1429: add ITs to verify handling of "fractional ints" (allowed/non-allowed) #1432

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.stargate.sgv2.jsonapi.api.v1.util.DataApiCommandSenders;
import io.stargate.sgv2.jsonapi.exception.DocumentException;
import io.stargate.sgv2.jsonapi.exception.ErrorCodeV1;
import io.stargate.sgv2.jsonapi.testresource.DseTestResource;
import java.util.Map;
Expand All @@ -19,7 +18,6 @@
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
public class FindOneTableIntegrationTest extends AbstractTableIntegrationTestBase {
static final String TABLE_WITH_STRING_ID_AGE_NAME = "findOneSingleStringKeyTable";
static final String TABLE_WITH_TEXT_COLUMNS = "findOneTextColumnsTable";

@BeforeAll
public final void createDefaultTables() {
Expand All @@ -33,16 +31,6 @@ public final void createDefaultTables() {
"name",
Map.of("type", "text")),
"id");
createTableWithColumns(
TABLE_WITH_TEXT_COLUMNS,
Map.of(
"idText",
Map.of("type", "text"),
"asciiText",
Map.of("type", "ascii"),
"varcharText",
Map.of("type", "text")),
"idText");
}

// On-empty tests to be run before ones that populate tables
Expand Down Expand Up @@ -205,52 +193,6 @@ public void findOneDocIdKey() {

@Nested
@Order(3)
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this test to new, refactored InsertOneTableIntegrationTests (see below).

class FindOneTextColumns {
public final String STRING_UTF8_WITH_2BYTE_CHAR = "utf8-2-byte-\u00a2"; // cent symbol
public final String STRING_UTF8_WITH_3BYTE_CHAR = "utf8-3-byte-\u20ac"; // euro symbol

@Test
void insertWithTextColumnsAndFind() {
final String DOC_JSON =
"""
{
"idText": "abc",
"asciiText": "safe value",
"varcharText": "%s/%s"
}
"""
.formatted(STRING_UTF8_WITH_2BYTE_CHAR, STRING_UTF8_WITH_3BYTE_CHAR);
insertOneInTable(TABLE_WITH_TEXT_COLUMNS, DOC_JSON);

DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_TEXT_COLUMNS)
.postFindOne("{ \"filter\": { \"idText\": \"abc\" } }")
.hasNoErrors()
.hasJSONField("data.document", DOC_JSON);
}

@Test
void failTryingToInsertNonAscii() {
final String DOC_JSON =
"""
{
"idText": "def",
"asciiText": "%s",
"varcharText": "safe value"
}
"""
.formatted(STRING_UTF8_WITH_2BYTE_CHAR);
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_TEXT_COLUMNS)
.postInsertOne(DOC_JSON)
.hasSingleApiError(
DocumentException.Code.INVALID_COLUMN_VALUES,
DocumentException.class,
"String contains non-ASCII character");
}
}

@Nested
@Order(4)
class FindOneFail {
@Test
@Order(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package io.stargate.sgv2.jsonapi.api.v1.tables;

import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.stargate.sgv2.jsonapi.api.v1.util.DataApiCommandSenders;
import io.stargate.sgv2.jsonapi.exception.DocumentException;
import io.stargate.sgv2.jsonapi.testresource.DseTestResource;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestClassOrder;

@QuarkusIntegrationTest
@WithTestResource(value = DseTestResource.class, restrictToAnnotatedClass = false)
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
public class InsertOneTableIntegrationTest extends AbstractTableIntegrationTestBase {
static final String TABLE_WITH_TEXT_COLUMNS = "findOneTextColumnsTable";
static final String TABLE_WITH_INT_COLUMNS = "findOneIntColumnsTable";

@BeforeAll
public final void createDefaultTables() {
createTableWithColumns(
TABLE_WITH_TEXT_COLUMNS,
Map.of(
"idText",
Map.of("type", "text"),
"asciiText",
Map.of("type", "ascii"),
"varcharText",
Map.of("type", "text")),
"idText");
createTableWithColumns(
TABLE_WITH_INT_COLUMNS,
Map.of(
"id",
Map.of("type", "text"),
"intValue",
Map.of("type", "int"),
"longValue",
Map.of("type", "bigint"),
"shortValue",
Map.of("type", "smallint"),
"byteValue",
Map.of("type", "tinyint"),
"bigIntegerValue",
Map.of("type", "varint")),
"id");
}

@Nested
@Order(1)
class InsertTextColumns {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from test above since it really exercises InsertOne, not FindOne.

public final String STRING_UTF8_WITH_2BYTE_CHAR = "utf8-2-byte-\u00a2"; // cent symbol
public final String STRING_UTF8_WITH_3BYTE_CHAR = "utf8-3-byte-\u20ac"; // euro symbol

@Test
void insertWithTextColumns() {
final String DOC_JSON =
"""
{
"idText": "abc",
"asciiText": "safe value",
"varcharText": "%s/%s"
}
"""
.formatted(STRING_UTF8_WITH_2BYTE_CHAR, STRING_UTF8_WITH_3BYTE_CHAR);
insertOneInTable(TABLE_WITH_TEXT_COLUMNS, DOC_JSON);

// And verify that we can read it back
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_TEXT_COLUMNS)
.postFindOne("{ \"filter\": { \"idText\": \"abc\" } }")
.hasNoErrors()
.hasJSONField("data.document", DOC_JSON);
}

@Test
void failTryingToInsertNonAscii() {
final String DOC_JSON =
"""
{
"idText": "def",
"asciiText": "%s",
"varcharText": "safe value"
}
"""
.formatted(STRING_UTF8_WITH_2BYTE_CHAR);
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_TEXT_COLUMNS)
.postInsertOne(DOC_JSON)
.hasSingleApiError(
DocumentException.Code.INVALID_COLUMN_VALUES,
DocumentException.class,
"String contains non-ASCII character");
}
}

@Nested
@Order(2)
class InsertIntColumns {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And these are the new tests for #1429 .

// [data-api#1429]: Test to verify that all-zero fractional parts are ok for int types
@Test
void insertWithIntColumnsZeroFractional() {
// In goes 5.00
insertOneInTable(TABLE_WITH_INT_COLUMNS, numDoc("zero-fraction", "5.00"));
// and out comes 5
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_INT_COLUMNS)
.postFindOne("{ \"filter\": { \"id\": \"zero-fraction\" } }")
.hasNoErrors()
.hasJSONField("data.document", numDoc("zero-fraction", "5"));
}

// [data-api#1429]: Test to verify that scientific is allowed for int types if (and only if)
// the fractional part is zero
@Test
void insertWithIntColumnsScientificNotation() {
// In goes 1.23E+02
insertOneInTable(TABLE_WITH_INT_COLUMNS, numDoc("scientific-but-int", "1.23E+02"));
// and out comes 123
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_INT_COLUMNS)
.postFindOne("{ \"filter\": { \"id\": \"scientific-but-int\" } }")
.hasNoErrors()
.hasJSONField("data.document", numDoc("scientific-but-int", "123"));
}

// [data-api#1429]: Test to verify that should there be real fraction, insert fails
@Test
void failWithNonZeroFractionPlain() {
// Try with 12.5, should fail
DataApiCommandSenders.assertTableCommand(keyspaceName, TABLE_WITH_INT_COLUMNS)
.postInsertOne(numDoc("non-zero-fraction", "12.5"))
.hasSingleApiError(
DocumentException.Code.INVALID_COLUMN_VALUES,
DocumentException.class,
"Root cause: Rounding necessary");
}

private String numDoc(String id, String num) {
return
"""
{
"id": "%s",
"byteValue": %s,
"shortValue": %s,
"intValue": %s,
"longValue": %s,
"bigIntegerValue": %s
}
"""
.formatted(id, num, num, num, num, num);
}
}
}