Skip to content

Commit

Permalink
Add JSON5 upsert GitRepository test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ks-yim committed Dec 10, 2021
1 parent aea4511 commit 7e79ea2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,14 @@ static <T> Entry<T> applyQuery(Entry<T> entry, Query<T> query) {
if (queryType == IDENTITY || queryType == IDENTITY_TEXT || queryType == IDENTITY_JSON) {
return entry;
} else if (queryType == JSON_PATH) {
if (entryType == EntryType.JSON5) {
try {
return Entry.of(entry.revision(), query.path(), EntryType.JSON,
query.apply(unsafeCast(Jackson.readTree(entry.contentAsText()))));
} catch (JsonParseException e) {
throw new QueryExecutionException("Invalid JSON5 entry content: " + entry.content() +
" (query: " + query + ')');
}
try {
// Entry whose type is EntryType.JSON5 is overridden to EntryType.JSON for JsonPath query.
return Entry.of(entry.revision(), query.path(), EntryType.JSON,
query.apply(unsafeCast(entry.contentAsJson())));
} catch (JsonParseException e) {
throw new QueryExecutionException("Invalid JSON content: " + entry.content() +
" (query: " + query + ')');
}
return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content()));
} else {
throw new QueryExecutionException("Unsupported entry type: " + entryType +
" (query: " + query + ')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ static void destroy() {
private String prefix;
private String allPattern;
private final String[] jsonPaths = new String[NUM_ITERATIONS];
private final String[] json5Paths = new String[NUM_ITERATIONS];
private final String[] textPaths = new String[NUM_ITERATIONS];
private final Change<JsonNode>[] jsonUpserts = Util.unsafeCast(new Change[NUM_ITERATIONS]);
private final Change<String>[] json5Upserts = Util.unsafeCast(new Change[NUM_ITERATIONS]);
private final Change<String>[] textUpserts = Util.unsafeCast(new Change[NUM_ITERATIONS]);
private final Change<JsonNode>[] jsonPatches = Util.unsafeCast(new Change[NUM_ITERATIONS]);
private final Change<String>[] textPatches = Util.unsafeCast(new Change[NUM_ITERATIONS]);
Expand All @@ -140,11 +142,14 @@ void setUp(TestInfo testInfo) {

for (int i = 0; i < NUM_ITERATIONS; i++) {
final String jsonPath = prefix + i + ".json";
final String json5Path = prefix + i + ".json5";
final String textPath = prefix + i + ".txt";

jsonPaths[i] = jsonPath;
json5Paths[i] = json5Path;
textPaths[i] = textPath;
jsonUpserts[i] = Change.ofJsonUpsert(jsonPath, "{ \"" + i + "\": " + i + " }");
json5Upserts[i] = Change.ofJson5Upsert(json5Path, "{ \"" + i + "\": '" + i + "' }");
textUpserts[i] = Change.ofTextUpsert(textPath, "value:\n" + i);
}

Expand All @@ -166,6 +171,11 @@ void testJsonUpsert() {
testUpsert(jsonUpserts, EntryType.JSON);
}

@Test
void testJson5Upsert() {
testUpsert(json5Upserts, EntryType.JSON5);
}

@Test
void testTextUpsert() {
testUpsert(textUpserts, EntryType.TEXT);
Expand Down Expand Up @@ -196,10 +206,9 @@ private void testUpsert(Change<?>[] upserts, EntryType entryType) {
final Map<String, Entry<?>> entries = Util.unsafeCast(repo.find(headRev, allPattern).join());
for (Change<?> c : upserts) {
final String path = c.path();
if (entryType == EntryType.TEXT) {
if (entryType == EntryType.TEXT || entryType == EntryType.JSON5) {
// Text must be sanitized so that the last line ends with \n.
assertThat(entries).containsEntry(path, Entry.of(headRev, path, EntryType.TEXT,
c.content() + "\n"));
assertThat(entries).containsEntry(path, Entry.of(headRev, path, entryType, c.content() + "\n"));
} else {
assertThat(entries).containsEntry(path, Entry.of(headRev, path, entryType, c.content()));
}
Expand Down

0 comments on commit 7e79ea2

Please sign in to comment.