@Test(expected=ParseException.class) @SuppressWarnings("unchecked") public void multisetInitTest_wrongType() throws Exception { Map<Object, Integer> s = (Map<Object, Integer>) StellarProcessorUtils.run("MULTISET_INIT({ 'foo' : 'bar'})", new HashMap<>()); }
@Test public void testIsNaN() { Assert.assertTrue(runPredicate("IS_NAN(NaN)", new HashMap<>())); Assert.assertFalse(runPredicate("IS_NAN(1.0)", new HashMap<>())); Assert.assertTrue(runPredicate("IS_NAN(0.0/0.0)",new HashMap<>())); }
public static void runWithArguments(String function, Object argument, Object expected) { runWithArguments(function, ImmutableList.of(argument), expected); }
/** * Execute and validate a Stellar expression. * * <p>This is intended for use while unit testing Stellar expressions. This ensures that the expression * validates successfully and produces a result that can be serialized correctly. * * @param expression The expression to execute. * @param varResolver The variable resolver to use * @param context The execution context. * @return The result of executing the expression. */ public static Object run(String expression, VariableResolver varResolver, Context context) { validate(expression, context); Object result = execute(expression, varResolver, context); ensureKryoSerializable(result, expression); ensureJavaSerializable(result, expression); return result; }
private void checkFalsey(String falseyExpr) { VariableResolver resolver = new MapVariableResolver(new HashMap<>()); Assert.assertTrue(runPredicate(String.format(" %s || true", falseyExpr), resolver)); Assert.assertFalse(runPredicate(String.format("%s && EXCEPTION('blah')", falseyExpr), resolver)); Assert.assertTrue(runPredicate(String.format("NOT(%s)", falseyExpr), resolver)); Assert.assertFalse(runPredicate(String.format("if %s then true else false", falseyExpr), resolver)); Assert.assertFalse(runPredicate(String.format("if %s then true || %s else false", falseyExpr, falseyExpr), resolver)); Assert.assertFalse(runPredicate(String.format("if %s then true || %s else false && %s", falseyExpr, falseyExpr, falseyExpr), resolver)); Assert.assertFalse(runPredicate(String.format("if %s then true || %s else false && (%s || true)", falseyExpr, falseyExpr, falseyExpr), resolver)); //make sure that nulls aren't replaced by false everywhere, only in boolean expressions. Assert.assertNull(run(String.format("MAP_GET(%s, {false : 'blah'})", falseyExpr), resolver)); }
public static void validate(String rule) { validate(rule, Context.EMPTY_CONTEXT()); }
@Test public void testValidateDoesNotThrow(){ String query = "someVar"; validate(query); }
/** * FORMAT - Not passing a format string will throw an exception */ @Test(expected = ParseException.class) public void testFormatWithNoArguments() throws Exception { run("FORMAT()", Collections.emptyMap()); }
@Test(expected = ParseException.class) public void testEmptyArgs() throws Exception { runPredicate("0 == FUZZY_SCORE(empty,empty,empty)", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))); }
@Test public void toTldTest_tld_square() { runWithArguments("DOMAIN_TO_TLD", "com.com", "com"); runWithArguments("DOMAIN_TO_TLD", "net.net", "net"); runWithArguments("DOMAIN_TO_TLD", "co.uk.co.uk", "co.uk"); runWithArguments("DOMAIN_TO_TLD", "www.subdomain.com.com", "com"); }
@Test public void ableToDivideByZero() throws Exception { assertEquals(0F/0F, run("0F/0F", ImmutableMap.of())); assertEquals(0D/0D, run("0D/0D", ImmutableMap.of())); assertEquals(0D/0F, run("0D/0F", ImmutableMap.of())); assertEquals(0F/0D, run("0F/0D", ImmutableMap.of())); assertEquals(0F/0, run("0F/0", ImmutableMap.of())); assertEquals(0D/0, run("0D/0", ImmutableMap.of())); assertEquals(0/0D, run("0/0D", ImmutableMap.of())); assertEquals(0/0F, run("0/0F", ImmutableMap.of())); } }
@Test(expected = ParseException.class) public void testMissingLanguage() throws Exception { runPredicate("0 == FUZZY_SCORE(metron,'z',klingon)", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))); }
@Test public void removeSubdomainsTest_tld_square() { runWithArguments("DOMAIN_REMOVE_SUBDOMAINS", "com.com", "com.com"); runWithArguments("DOMAIN_REMOVE_SUBDOMAINS", "net.net", "net.net"); runWithArguments("DOMAIN_REMOVE_SUBDOMAINS", "co.uk.co.uk", "uk.co.uk"); runWithArguments("DOMAIN_REMOVE_SUBDOMAINS", "www.subdomain.com.com", "com.com"); }
@SuppressWarnings("ConstantConditions") @Test public void checkComparisonOperationsWithFunctions() throws Exception { assertEquals(1f >= 2, run("TO_FLOAT(1) >= 2", ImmutableMap.of())); assertEquals(1f <= 2, run("TO_FLOAT(1) <= TO_FLOAT(2)", ImmutableMap.of())); assertEquals(1f == 2, run("TO_FLOAT(1) == TO_LONG(2)", ImmutableMap.of())); assertEquals(12.31f == 10.2f, run("TO_FLOAT(12.31) < 10.2f", ImmutableMap.of())); }
@Test public void testShortCircuit_mixedBoolOps() throws Exception { final Map<String, String> variableMap = new HashMap<String, String>(); Assert.assertTrue(runPredicate("(false && true) || true" , new DefaultVariableResolver(v -> variableMap.get(v),v -> variableMap.containsKey(v)))); Assert.assertTrue(runPredicate("(false && false) || true" , new DefaultVariableResolver(v -> variableMap.get(v),v -> variableMap.containsKey(v)))); Assert.assertFalse(runPredicate("(true || true) && false" , new DefaultVariableResolver(v -> variableMap.get(v),v -> variableMap.containsKey(v)))); }
@Test public void removeTldTest() { runWithArguments("DOMAIN_REMOVE_TLD", "google.com", "google"); runWithArguments("DOMAIN_REMOVE_TLD", "www.google.co.uk", "www.google"); runWithArguments("DOMAIN_REMOVE_TLD", "www.google.com", "www.google"); runWithArguments("DOMAIN_REMOVE_TLD", "com", ""); }
@SuppressWarnings("PointlessArithmeticExpression") @Test public void happyPathLongArithmetic() throws Exception { assertEquals(0L * 1L, run("0L * 1L", ImmutableMap.of())); assertEquals(0l / 1L, run("0l / 1L", ImmutableMap.of())); assertEquals(1L - 1l, run("1L - 1l", ImmutableMap.of())); assertEquals(2147483648L + 1L, run("2147483648L + 1L", ImmutableMap.of())); }
@Test(expected = ParseException.class) public void differentTypesShouldThrowErrorWhenUsingLT() throws Exception { final Map<String, Object> variableMap = new HashMap<>(); runPredicate("1 < '1'", new DefaultVariableResolver(variableMap::get,variableMap::containsKey)); }
@Test public void removeTldTest_tld_square() { runWithArguments("DOMAIN_REMOVE_TLD", "com.com", "com"); runWithArguments("DOMAIN_REMOVE_TLD", "net.net", "net"); runWithArguments("DOMAIN_REMOVE_TLD", "co.uk.co.uk", "co.uk"); runWithArguments("DOMAIN_REMOVE_TLD", "www.subdomain.com.com", "www.subdomain.com"); }