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

[BT-4418] FaunaParser Double values #19

Merged
merged 11 commits into from
Jan 31, 2024
14 changes: 13 additions & 1 deletion faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fauna.serialization;

import static com.fauna.common.enums.FaunaTokenType.DATE;
import static com.fauna.common.enums.FaunaTokenType.DOUBLE;
import static com.fauna.common.enums.FaunaTokenType.END_ARRAY;
import static com.fauna.common.enums.FaunaTokenType.END_DOCUMENT;
import static com.fauna.common.enums.FaunaTokenType.END_OBJECT;
Expand Down Expand Up @@ -129,8 +130,10 @@ private void handleStartObject() throws IOException {
case TIME_TAG:
handleTaggedString(FaunaTokenType.TIME);
break;
case DOC_TAG:
case DOUBLE_TAG:
handleTaggedString(FaunaTokenType.DOUBLE);
break;
case DOC_TAG:
case LONG_TAG:
case MOD_TAG:
case OBJECT_TAG:
Expand Down Expand Up @@ -226,4 +229,13 @@ public Instant getValueAsTime() {
throw new RuntimeException("Error getting the current token as LocalDateTime", e);
}
}

public Double getValueAsDouble() {
validateTaggedType(DOUBLE);
try {
return Double.parseDouble(taggedTokenValue);
} catch (NumberFormatException e) {
throw new RuntimeException("Error getting the current token as Double", e);
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.fauna.query.template;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class FaunaTemplateTest {

Expand Down Expand Up @@ -63,16 +62,4 @@ void testTemplates_WithEscapes() {
assertEquals(TemplatePartType.LITERAL, expanded.get(1).getType());
}

@Test
void testTemplates_WithUnsupportedIdentifiers() {
FaunaTemplate template = new FaunaTemplate("let x = ${かわいい}");
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
List<FaunaTemplate.TemplatePart> expanded = new ArrayList<>();
template.forEach(expanded::add);
});
String expectedMessage = "Invalid placeholder in template: line 1, col 9";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ public void testeGetValueAsTime() throws IOException {
assertThrows(RuntimeException.class, invalidReader::getValueAsLocalDate);
}

@Test
public void testGetValueAsDouble() throws IOException {
String s = "{\"@double\": \"1.23\"}";
InputStream inputStream = new ByteArrayInputStream(s.getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.DOUBLE, 1.23D)
);

assertReader(reader, expectedTokens);

String invalidJson = "{\"@double\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(RuntimeException.class, invalidReader::getValueAsDouble);
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand All @@ -151,6 +170,9 @@ private static void assertReader(FaunaParser reader,
case TIME:
assertEquals(entry.getValue(), reader.getValueAsTime());
break;
case DOUBLE:
assertEquals(entry.getValue(), reader.getValueAsDouble());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down
Loading