Commit 51a6994a by Tomas Dvorak

Output valid JSON results

parent 5e9035c1
...@@ -2,7 +2,7 @@ package com.github.variety.test; ...@@ -2,7 +2,7 @@ package com.github.variety.test;
import com.github.variety.Variety; import com.github.variety.Variety;
import com.github.variety.VarietyAnalysis; import com.github.variety.VarietyAnalysis;
import com.mongodb.DBObject; import com.mongodb.BasicDBList;
import com.mongodb.util.JSON; import com.mongodb.util.JSON;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
...@@ -30,20 +30,16 @@ public class OutputFormatTest { ...@@ -30,20 +30,16 @@ public class OutputFormatTest {
@Test @Test
public void verifyJsonEntries() throws Exception { public void verifyJsonEntries() throws Exception {
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_JSON).runAnalysis(); final VarietyAnalysis analysis = variety
.withQuiet(true) // do not output any other metadata, only results
.withFormat(Variety.FORMAT_JSON)
.runAnalysis();
// TODO: output itself is not valid JSON. It contains mongo shell output (can be removed with --quiet) and variety execution info. // Verify, that output is parse-able json by transforming stdout to json
// At the end of output, there are printed records from result collection, every record on new line. final BasicDBList parsed = (BasicDBList) JSON.parse(analysis.getStdOut());
// Valid json output is requested in issue https://github.com/variety/variety/issues/23
// Verify, that every object is parse-able json by transforming strings to json stream // there should be seven different json results
// Results are detected by line starting with character '{'. Assert.assertEquals(7, parsed.size());
final Stream<DBObject> objects = Stream.of(analysis.getStdOut().split("\n"))
.filter(line -> line.startsWith("{"))
.map(str -> (DBObject)JSON.parse(str));
// there should be seven different json results in the stdout
Assert.assertEquals(7, objects.count());
} }
@Test @Test
......
...@@ -227,9 +227,7 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) { ...@@ -227,9 +227,7 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) {
var sortedKeys = resultsDB[resultsCollectionName].find({}).sort({totalOccurrences: -1}); var sortedKeys = resultsDB[resultsCollectionName].find({}).sort({totalOccurrences: -1});
if(outputFormat === 'json') { if(outputFormat === 'json') {
sortedKeys.forEach(function(key) { printjson(sortedKeys.toArray()); // valid formatted json output, compressed variant is printjsononeline()
print(tojson(key, '', true));
});
} else { // output nice ascii table with results } else { // output nice ascii table with results
var table = [["key", "types", "occurrences", "percents"], ["", "", "", ""]]; // header + delimiter rows var table = [["key", "types", "occurrences", "percents"], ["", "", "", ""]]; // header + delimiter rows
sortedKeys.forEach(function(key) { sortedKeys.forEach(function(key) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment