Skip to content

Commit

Permalink
grel: Prepare tests for modularization (#6571)
Browse files Browse the repository at this point in the history
* Introduce dedicated base class for GREL tests

* Move tests from EvalErrorTests to the functions they actually test
  • Loading branch information
wetneb committed May 16, 2024
1 parent ef5c171 commit e493b61
Show file tree
Hide file tree
Showing 51 changed files with 350 additions and 315 deletions.
36 changes: 0 additions & 36 deletions main/tests/server/src/com/google/refine/RefineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

import com.google.refine.expr.Evaluable;
import com.google.refine.expr.MetaParser;
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.grel.Parser;
Expand Down Expand Up @@ -381,39 +378,6 @@ protected static Object invoke(String name, Object... args) {
}
}

/**
* Parse and evaluate a GREL expression and compare the result to the expect value
*
* @param bindings
* @param test
* @throws ParsingException
*/
protected void parseEval(Properties bindings, String[] test)
throws ParsingException {
Evaluable eval = MetaParser.parse("grel:" + test[0]);
Object result = eval.evaluate(bindings);
if (test[1] != null) {
Assert.assertNotNull(result, "Expected " + test[1] + " for test " + test[0]);
Assert.assertEquals(result.toString(), test[1], "Wrong result for expression: " + test[0]);
} else {
Assert.assertNull(result, "Wrong result for expression: " + test[0]);
}
}

/**
* Parse and evaluate a GREL expression and compare the result an expected type using instanceof
*
* @param bindings
* @param test
* @throws ParsingException
*/
protected void parseEvalType(Properties bindings, String test, @SuppressWarnings("rawtypes") Class clazz)
throws ParsingException {
Evaluable eval = MetaParser.parse("grel:" + test);
Object result = eval.evaluate(bindings);
Assert.assertTrue(clazz.isInstance(result), "Wrong result type for expression: " + test);
}

@AfterMethod
public void TearDown() throws Exception {
bindings = null;
Expand Down
38 changes: 0 additions & 38 deletions main/tests/server/src/com/google/refine/expr/EvalErrorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,17 @@

package com.google.refine.expr;

import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;

public class EvalErrorTests extends RefineTest {

@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}

@Test
public void serializeEvalError() {
EvalError e = new EvalError("This is a critical error");
TestUtils.isSerializedTo(e, "{\"type\":\"error\",\"message\":\"This is a critical error\"}");
}

@Test
public void testInnerHtml() {
Assert.assertTrue(invoke("innerHtml") instanceof EvalError);
Assert.assertTrue(invoke("innerHtml", "test") instanceof EvalError);

EvalError evalError = (EvalError) invoke("innerHtml", "test");
Assert.assertEquals(evalError.toString(),
"innerHtml() cannot work with this \'string\'. The first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function");
}

@Test
public void testWholeText() {
Assert.assertTrue(invoke("wholeText") instanceof EvalError);
Assert.assertTrue(invoke("wholeText", "test") instanceof EvalError);

EvalError evalError = (EvalError) invoke("wholeText", "test");
Assert.assertEquals(evalError.toString(),
"wholeText() cannot work with this \'string\' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}

@Test
public void testXmlText() {
Assert.assertTrue(invoke("xmlText") instanceof EvalError);
Assert.assertTrue(invoke("xmlText", "test") instanceof EvalError);

EvalError evalError = (EvalError) invoke("xmlText", "test");
Assert.assertEquals(evalError.toString(),
"xmlText() cannot work with this \'string\' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,16 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

package com.google.refine.expr.functions;

import java.util.Properties;

import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.GrelTestBase;

public class CoalesceTests extends RefineTest {
public class CoalesceTests extends GrelTestBase {

private static final Integer[] ZERO_TO_TWO = new Integer[] { 0, 1, 2 };

@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}

@BeforeMethod
public void setUp() {
bindings = new Properties();
}

@AfterMethod
public void tearDown() {
bindings = null;
}

@Test
public void testCoalesceInvalidParams() {
Assert.assertTrue(invoke("coalesce") instanceof EvalError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,28 @@
import java.util.List;
import java.util.Properties;

import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.HasFieldsListImpl;
import com.google.refine.expr.WrappedCell;
import com.google.refine.expr.WrappedRow;
import com.google.refine.grel.GrelTestBase;
import com.google.refine.model.Cell;
import com.google.refine.model.Project;
import com.google.refine.model.Row;

/**
* Test cases for cross function.
*/
public class CrossTests extends RefineTest {
public class CrossTests extends GrelTestBase {

private static final String ERROR_MSG = "cross expects a cell or value, a project name to look up (optional), and a column name in that project (optional)";
private static final OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00",
DateTimeFormatter.ISO_OFFSET_DATE_TIME);

@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}

private HasFieldsListImpl emptyList;

// dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,15 @@

package com.google.refine.expr.functions;

import java.util.Properties;

import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.Evaluable;
import com.google.refine.expr.ExpressionUtils;
import com.google.refine.expr.MetaParser;
import com.google.refine.expr.ParsingException;
import com.google.refine.model.Project;

public class GetTests extends RefineTest {

Project project;
Properties bindings;

@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}

@BeforeMethod
public void SetUp() {
project = new Project();
bindings = ExpressionUtils.createBindings(project);
}
import com.google.refine.grel.GrelTestBase;

@AfterMethod
public void TearDown() {
project = null;
bindings = null;
}
public class GetTests extends GrelTestBase {

@Test
public void testGetJsonFieldExists() throws ParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.util.CalendarParserException;
import com.google.refine.grel.GrelTestBase;

public class JsonizeTests extends RefineTest {
public class JsonizeTests extends GrelTestBase {

@Test
public void testToString() throws CalendarParserException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.util.CalendarParser;
import com.google.refine.expr.util.CalendarParserException;
import com.google.refine.grel.GrelTestBase;

public class ToDateTests extends RefineTest {
public class ToDateTests extends GrelTestBase {

@Test
public void testToDate() throws CalendarParserException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.Function;
import com.google.refine.grel.GrelTestBase;

public class ToNumberTests extends RefineTest {
public class ToNumberTests extends GrelTestBase {

private static final Double EPSILON = 0.000001;
static Properties bindings = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.util.CalendarParser;
import com.google.refine.expr.util.CalendarParserException;
import com.google.refine.grel.GrelTestBase;

public class ToStringTests extends RefineTest {
public class ToStringTests extends GrelTestBase {

@Test
public void testToString() throws CalendarParserException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,18 @@
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.GrelTestBase;

public class TypeTests extends RefineTest {
public class TypeTests extends GrelTestBase {

static final List<String> listArray = Arrays.asList("v1", "v2", "v3");
private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);

@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}

@BeforeMethod
public void setUp() {
bindings = new Properties();
}

@AfterMethod
public void tearDown() {
bindings = null;
}

@Test
public void testTypeInvalidParams() {
Assert.assertTrue(invoke("type") instanceof EvalError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.GrelTestBase;

public class InArrayTests extends RefineTest {
public class InArrayTests extends GrelTestBase {

static Properties bindings;
static final List<String> listArray = Arrays.asList("v1", "v2", "v3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.GrelTestBase;

public class JoinTests extends RefineTest {
public class JoinTests extends GrelTestBase {

@Test
public void joinArray() throws ParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.GrelTestBase;

public class ReverseTests extends RefineTest {
public class ReverseTests extends GrelTestBase {

@Test
public void reverseJsonArray() throws ParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.GrelTestBase;

public class SortTests extends RefineTest {
public class SortTests extends GrelTestBase {

@Test
public void sortJsonArray() throws ParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.GrelTestBase;

public class UniquesTests extends RefineTest {
public class UniquesTests extends GrelTestBase {

@Test
public void uniquesJsonArray() throws ParsingException {
Expand Down
Loading

0 comments on commit e493b61

Please sign in to comment.