Commit 59c2ec0b by Tomas Dvorak

tests adapted to #104

parent 18ec4c82
...@@ -5,6 +5,7 @@ import com.mongodb.*; ...@@ -5,6 +5,7 @@ import com.mongodb.*;
import org.junit.Assert; import org.junit.Assert;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set;
public class DbResultsValidator implements ResultsValidator { public class DbResultsValidator implements ResultsValidator {
...@@ -53,7 +54,8 @@ public class DbResultsValidator implements ResultsValidator { ...@@ -53,7 +54,8 @@ public class DbResultsValidator implements ResultsValidator {
} }
private void verifyKeyTypes(final String key, final DBObject result, final String[] expectedTypes) { private void verifyKeyTypes(final String key, final DBObject result, final String[] expectedTypes) {
final BasicDBList types = (BasicDBList)((DBObject) result.get("value")).get("types"); final BasicDBObject typesObj = (BasicDBObject)((BasicDBObject)result.get("value")).get("types");
final Set<String> types = typesObj.keySet();
Assert.assertEquals( Assert.assertEquals(
"Incorrect count of expected(" + Arrays.toString(expectedTypes) + ") and real types(" + Arrays.toString(types.toArray()) "Incorrect count of expected(" + Arrays.toString(expectedTypes) + ") and real types(" + Arrays.toString(types.toArray())
......
...@@ -25,8 +25,8 @@ public class JsonResultsValidator implements ResultsValidator { ...@@ -25,8 +25,8 @@ public class JsonResultsValidator implements ResultsValidator {
final String key = ((BasicDBObject)obj.get("_id")).getString("key"); final String key = ((BasicDBObject)obj.get("_id")).getString("key");
final long totalOccurrences = obj.getLong("totalOccurrences"); final long totalOccurrences = obj.getLong("totalOccurrences");
final double percentContaining = obj.getDouble("percentContaining"); final double percentContaining = obj.getDouble("percentContaining");
final BasicDBList typesList = (BasicDBList) ((BasicDBObject)obj.get("value")).get("types"); final BasicDBObject typesObj = (BasicDBObject) ((BasicDBObject)obj.get("value")).get("types");
final HashSet<String> types = new HashSet<>(Arrays.asList(typesList.toArray(new String[typesList.size()]))); final Set<String> types = typesObj.keySet();
entries.add(new VarietyEntry(key, totalOccurrences, percentContaining, types)); entries.add(new VarietyEntry(key, totalOccurrences, percentContaining, types));
} }
return entries; return entries;
......
...@@ -67,7 +67,7 @@ public class LimitedAccessTest { ...@@ -67,7 +67,7 @@ public class LimitedAccessTest {
@Test @Test
public void verifyBasicResultsAscii() throws Exception { public void verifyBasicResultsAscii() throws Exception {
final String stdout = variety.withPersistResults(false).withQuiet(true).runAnalysis(); final String stdout = variety.withPersistResults(false).withQuiet(true).runAnalysis();
Assert.assertEquals(SampleData.EXPECTED_DATA_ASCII_TABLE, stdout); Assert.assertEquals(SampleData.getExpectedDataAsciiTable(), stdout);
} }
......
...@@ -46,7 +46,7 @@ public class OutputFormatTest { ...@@ -46,7 +46,7 @@ public class OutputFormatTest {
.filter(line -> line.startsWith("|") || line.startsWith("+")) .filter(line -> line.startsWith("|") || line.startsWith("+"))
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
Assert.assertEquals(SampleData.EXPECTED_DATA_ASCII_TABLE, actual); Assert.assertEquals(SampleData.getExpectedDataAsciiTable(), actual);
} }
} }
...@@ -2,12 +2,14 @@ package com.github.variety.test; ...@@ -2,12 +2,14 @@ package com.github.variety.test;
import com.github.variety.Variety; import com.github.variety.Variety;
import com.github.variety.validator.ResultsValidator; import com.github.variety.validator.ResultsValidator;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.net.URL; import java.net.URL;
import java.util.regex.Pattern;
public class PluginsTest { public class PluginsTest {
...@@ -49,7 +51,7 @@ public class PluginsTest { ...@@ -49,7 +51,7 @@ public class PluginsTest {
public void verifyPluginParamParsing() throws Exception { public void verifyPluginParamParsing() throws Exception {
final String path = getPluginPath("/csvplugin.js"); final String path = getPluginPath("/csvplugin.js");
final ResultsValidator analysis = variety.withPlugins(path + "|delimiter=;").runDatabaseAnalysis(); final ResultsValidator analysis = variety.withPlugins(path + "|delimiter=;").runDatabaseAnalysis();
Assert.assertTrue(analysis.getStdOut().contains("Using plugins of [ \"" + path + "\" ]")); Assert.assertTrue(analysis.getStdOut().contains(path));
} }
private String getPluginPath(final String name) { private String getPluginPath(final String name) {
......
...@@ -34,6 +34,6 @@ public class QuietOptionTest { ...@@ -34,6 +34,6 @@ public class QuietOptionTest {
@Test @Test
public void testQuietLogs() throws Exception { public void testQuietLogs() throws Exception {
final ResultsValidator varietyAnalysis = variety.withQuiet(true).runDatabaseAnalysis(); final ResultsValidator varietyAnalysis = variety.withQuiet(true).runDatabaseAnalysis();
Assert.assertEquals(SampleData.EXPECTED_DATA_ASCII_TABLE, varietyAnalysis.getStdOut()); Assert.assertEquals(SampleData.getExpectedDataAsciiTable(), varietyAnalysis.getStdOut());
} }
} }
...@@ -4,6 +4,9 @@ import com.mongodb.BasicDBObjectBuilder; ...@@ -4,6 +4,9 @@ import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import org.bson.types.Binary; import org.bson.types.Binary;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -12,23 +15,6 @@ import java.util.List; ...@@ -12,23 +15,6 @@ import java.util.List;
class SampleData { class SampleData {
/** /**
* Ascii table representation of sample data results. It should be possible to verify actual output of Variety
* against this table, to check correct formatting.
*/
public static final String EXPECTED_DATA_ASCII_TABLE =
"+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100.0 |\n" +
"| name | String | 5 | 100.0 |\n" +
"| bio | String | 3 | 60.0 |\n" +
"| birthday | String | 2 | 40.0 |\n" +
"| pets | Array,String | 2 | 40.0 |\n" +
"| someBinData | BinData-old | 1 | 20.0 |\n" +
"| someWeirdLegacyKey | String | 1 | 20.0 |\n" +
"+------------------------------------------------------------+";
/**
* Java representation of sample collection provided in variety README:<p> * Java representation of sample collection provided in variety README:<p>
* *
* {name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"}<p> * {name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"}<p>
...@@ -76,4 +62,16 @@ class SampleData { ...@@ -76,4 +62,16 @@ class SampleData {
return examples; return examples;
} }
/**
* Ascii table representation of sample data results. It should be possible to verify actual output of Variety
* against this table, to check correct formatting.
*/
public static String getExpectedDataAsciiTable() {
try {
return new String(Files.readAllBytes(Paths.get(SampleData.class.getResource("/expected_ascii_table.txt").getFile())));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }
...@@ -3,7 +3,7 @@ var getCsv = function(varietyResults) { ...@@ -3,7 +3,7 @@ var getCsv = function(varietyResults) {
var headers = ['key', 'types', 'occurrences', 'percents']; var headers = ['key', 'types', 'occurrences', 'percents'];
var table = [headers.join(delimiter)]; var table = [headers.join(delimiter)];
var rows = varietyResults.map(function(key) { var rows = varietyResults.map(function(key) {
return [key._id.key, key.value.types, key.totalOccurrences, key.percentContaining].join(delimiter); return [key._id.key, Object.keys(key.value.types).sort(), key.totalOccurrences, key.percentContaining].join(delimiter);
}, this); }, this);
return table.concat(rows).join('\n'); return table.concat(rows).join('\n');
}; };
......
+--------------------------------------------------------------------+
| key | types | occurrences | percents |
| ------------------ | -------------------- | ----------- | -------- |
| _id | ObjectId | 5 | 100.0 |
| name | String | 5 | 100.0 |
| bio | String | 3 | 60.0 |
| birthday | String | 2 | 40.0 |
| pets | String (1),Array (1) | 2 | 40.0 |
| someBinData | BinData-old | 1 | 20.0 |
| someWeirdLegacyKey | String | 1 | 20.0 |
+--------------------------------------------------------------------+
\ No newline at end of file
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