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

fixing junits #524

Merged
merged 6 commits into from
Mar 10, 2023
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
60 changes: 0 additions & 60 deletions common/client/src/test/java/zingg/client/TestClientOption.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zingg.client;
package zingg.common.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -7,7 +7,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,9 +15,6 @@
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;

import zingg.common.client.pipe.Pipe;
import zingg.common.client.pipe.SparkPipe;

public class TestArguments {

private static final String KEY_HEADER = "header";
Expand All @@ -36,7 +32,7 @@ public void testSubstituteVariablesWithAllEnvVarSet() {
env.put(KEY_MODEL_ID, "400");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));
String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Arguments args = Arguments.createArgumentsFromJSONString(json, "");
Expand All @@ -57,7 +53,7 @@ public void testSubstituteVariablesWithMissingEnvVar() {
env.put(KEY_MODEL_ID, "400");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -77,7 +73,7 @@ public void testSubstituteVariablesWithBlankEnvVar() {
env.put(KEY_MODEL_ID, "400");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -99,7 +95,7 @@ public void testInvalidEnvVarBooleanType() {
env.put(KEY_MODEL_ID, "400");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -120,7 +116,7 @@ public void testBooleanType() {
env.put(KEY_MODEL_ID, "400");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -142,7 +138,7 @@ public void testInvalidEnvVarNumericType() {
env.put(KEY_MODEL_ID, "ONEHUNDRED");

byte[] encoded = Files
.readAllBytes(Paths.get(getClass().getResource("../../testArguments/testConfigTemplate.json.env").getFile()));
.readAllBytes(Paths.get(getClass().getResource("../../../testArguments/testConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -164,7 +160,7 @@ public void testNumericWithinQuotes() {
env.put(KEY_MODEL_ID, "500");

byte[] encoded = Files.readAllBytes(
Paths.get(getClass().getResource("../../testArguments/testNumericWithinQuotesTemplate.json.env").getFile()));
Paths.get(getClass().getResource("../../../testArguments/testNumericWithinQuotesTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -187,7 +183,7 @@ public void testMalformedVariable() {
env.put(KEY_MODEL_ID, "500");

byte[] encoded = Files.readAllBytes(
Paths.get(getClass().getResource("../../testArguments/testMalformedConfigTemplate.json.env").getFile()));
Paths.get(getClass().getResource("../../../testArguments/testMalformedConfigTemplate.json.env").getFile()));

String template = new String(encoded, StandardCharsets.UTF_8);
String json = Arguments.substituteVariables(template, env);
Expand All @@ -214,7 +210,7 @@ public void testInvalidFilePath() {
public void testMatchTypeMultiple() {
Arguments args;
try {
args = Arguments.createArgumentsFromJSON(getClass().getResource("../../testArguments/configWithMultipleMatchTypes.json").getFile(), "test");
args = Arguments.createArgumentsFromJSON(getClass().getResource("../../../testArguments/configWithMultipleMatchTypes.json").getFile(), "test");
List<MatchType> fNameMatchType = args.getFieldDefinition().get(0).getMatchType();
assertEquals(2, fNameMatchType.size());
assertEquals(MatchType.FUZZY, fNameMatchType.get(0));
Expand All @@ -233,62 +229,16 @@ public void testMatchTypeMultiple() {
public void testMatchTypeWrong() {
Arguments args;
try {
args = Arguments.createArgumentsFromJSON(getClass().getResource("../../testArguments/configWithMultipleMatchTypesUnsupported.json").getFile(), "test");
args = Arguments.createArgumentsFromJSON(getClass().getResource("../../../testArguments/configWithMultipleMatchTypesUnsupported.json").getFile(), "test");
//List<MatchType> fNameMatchType = args.getFieldDefinition().get(0).getMatchType();
fail("config had error, should have flagged");

} catch (Exception | ZinggClientException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// e.printStackTrace();
}



}

@Test
public void testWriteArgumentObjectToJSONFile() {
Arguments args = new Arguments();
try {
FieldDefinition fname = new FieldDefinition();
fname.setFieldName("fname");
fname.setDataType("\"string\"");
fname.setMatchType(Arrays.asList(MatchType.EXACT, MatchType.FUZZY, MatchType.PINCODE));
//fname.setMatchType(Arrays.asList(MatchType.EXACT));
fname.setFields("fname");
FieldDefinition lname = new FieldDefinition();
lname.setFieldName("lname");
lname.setDataType("\"string\"");
lname.setMatchType(Arrays.asList(MatchType.FUZZY));
lname.setFields("lname");
args.setFieldDefinition(Arrays.asList(fname, lname));

Pipe inputPipe = new SparkPipe();
inputPipe.setName("test");
inputPipe.setFormat(Pipe.FORMAT_CSV);
inputPipe.setProp("location", "examples/febrl/test.csv");
args.setData(new Pipe[] {inputPipe});

Pipe outputPipe = new SparkPipe();
outputPipe.setName("output");
outputPipe.setFormat(Pipe.FORMAT_CSV);
outputPipe.setProp("location", "examples/febrl/output.csv");
args.setOutput(new Pipe[] {outputPipe});

args.setBlockSize(400L);
args.setCollectMetrics(true);
args.setModelId("500");
Arguments.writeArgumentsToJSON("/tmp/configFromArgObject.json", args);

//reload the same config file to check if deserialization is successful
Arguments newArgs = Arguments.createArgumentsFromJSON("/tmp/configFromArgObject.json", "test");
assertEquals(newArgs.getModelId(), "500", "Model id is different");
assertEquals(newArgs.getBlockSize(), 400L, "Block size is different");
assertEquals(newArgs.getFieldDefinition().get(0).getFieldName(), "fname", "Field Definition[0]'s name is different");
String expectedMatchType = "[EXACT, FUZZY, PINCODE]";
assertEquals(newArgs.getFieldDefinition().get(0).getMatchType().toString(), expectedMatchType);
} catch (Exception | ZinggClientException e) {
e.printStackTrace();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package zingg.client;
package zingg.common.client;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

public class TestClient {
Expand All @@ -33,15 +30,5 @@ public void testInvalidPhase() {
}
}

/*
@Test
public void testSetColumnOptionThroughBuildAndSetArguments() {
Arguments arguments = new Arguments();
String[] args = {ClientOptions.CONF, "configFile", ClientOptions.PHASE, "train", ClientOptions.COLUMN, "columnName", ClientOptions.SHOW_CONCISE, "true", ClientOptions.LICENSE, "licenseFile"};
ClientOptions options = new ClientOptions(args);
Client client = new Client();
client.buildAndSetArguments(arguments, options);

assertEquals("columnName", client.getArguments().getColumn());
}*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package zingg.common.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

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

import org.junit.jupiter.api.Test;

public class TestClientOption {


@Test
public void testParseArguments() {
String[] args = {"--phase", "train",
"--conf", "conf.json",
"--zinggDir", "/tmp/z_main",
"--email", "zingg@zingg.ai",
"--license", "zinggLicense.txt"};

ClientOptions co = new ClientOptions(args);
assertEquals("conf.json", co.get(ClientOptions.CONF).value);
assertEquals("train", co.get(ClientOptions.PHASE).value);
assertEquals("/tmp/z_main", co.get(ClientOptions.ZINGG_DIR).value);
assertNull(co.get(ClientOptions.HELP));
}

@Test
public void testParseUnsupportedArgumentsConf() {
try {
String[] args = {"--phase", "train",
"--conf1", "conf.json",
"--zinggDir", "/tmp/z_main",
"--email", "zingg@zingg.ai",
"--license", "zinggLicense.txt"};

ClientOptions co = new ClientOptions(args);
fail("exception should have been raised due to invalid conf option");
} catch (Exception e) {
assertTrue(true);
}
}

@Test
public void testParseUnsupportedArgumentsPhase() {
try {
String[] args = {"--phase1", "train",
"--conf1", "conf.json",
"--zinggDir", "/tmp/z_main",
"--email", "zingg@zingg.ai",
"--license", "zinggLicense.txt"};

ClientOptions co = new ClientOptions(args);
fail("exception should have been raised due to invalid phase option");
} catch (Exception e) {
assertTrue(true);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zingg.client;
package zingg.common.client;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zingg.client.input;
package zingg.common.client;

import java.io.File;

Expand Down
Loading