Refactor CrossFunctionTests' project creating logic into RefineTest
This commit is contained in:
parent
197dc47f37
commit
5278e929a2
@ -33,30 +33,129 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.tests;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeSuite;
|
||||
|
||||
import com.google.refine.ProjectManager;
|
||||
import com.google.refine.ProjectMetadata;
|
||||
import com.google.refine.RefineServlet;
|
||||
import com.google.refine.importers.SeparatorBasedImporter;
|
||||
import com.google.refine.importing.ImportingJob;
|
||||
import com.google.refine.importing.ImportingManager;
|
||||
import com.google.refine.model.Cell;
|
||||
import com.google.refine.model.Column;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.util.JSONUtilities;
|
||||
|
||||
/**
|
||||
* A base class containing various utilities to help testing Refine.
|
||||
*/
|
||||
public class RefineTest {
|
||||
|
||||
protected Logger logger;
|
||||
|
||||
protected RefineServlet servlet;
|
||||
private List<Project> projects = new ArrayList<Project>();
|
||||
private List<ImportingJob> importingJobs = new ArrayList<ImportingJob>();
|
||||
|
||||
@BeforeSuite
|
||||
public void init() {
|
||||
System.setProperty("log4j.configuration", "tests.log4j.properties");
|
||||
ProjectManager.singleton = new ProjectManagerStub();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to create a project from a CSV encoded as a file. Not much
|
||||
* control is given on the import options, because this method is intended
|
||||
* to be a quick way to create a project for a test. For more control over
|
||||
* the import, just call the importer directly.
|
||||
*
|
||||
* The projects created via this method and their importing jobs will be disposed of
|
||||
* at the end of each test.
|
||||
*
|
||||
* @param projectName
|
||||
* the name of the project to create
|
||||
* @param input
|
||||
* the content of the file, encoded as a CSV (with "," as a separator)
|
||||
* @return
|
||||
*/
|
||||
protected Project createCSVProject(String projectName, String input) {
|
||||
Project project = new Project();
|
||||
|
||||
ProjectMetadata metadata = new ProjectMetadata();
|
||||
metadata.setName(projectName);
|
||||
|
||||
JSONObject options = mock(JSONObject.class);
|
||||
prepareImportOptions(options, ",", -1, 0, 0, 1, false, false);
|
||||
|
||||
servlet = new RefineServletStub();
|
||||
ImportingManager.initialize(servlet);
|
||||
ImportingJob job = ImportingManager.createJob();
|
||||
|
||||
SeparatorBasedImporter importer = new SeparatorBasedImporter();
|
||||
|
||||
List<Exception> exceptions = new ArrayList<Exception>();
|
||||
importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
|
||||
project.update();
|
||||
ProjectManager.singleton.registerProject(project, metadata);
|
||||
|
||||
projects.add(project);
|
||||
importingJobs.add(job);
|
||||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the importing options for the CSV importer.
|
||||
* @param options
|
||||
* @param sep
|
||||
* @param limit
|
||||
* @param skip
|
||||
* @param ignoreLines
|
||||
* @param headerLines
|
||||
* @param guessValueType
|
||||
* @param ignoreQuotes
|
||||
*/
|
||||
private void prepareImportOptions(JSONObject options,
|
||||
String sep, int limit, int skip, int ignoreLines,
|
||||
int headerLines, boolean guessValueType, boolean ignoreQuotes) {
|
||||
|
||||
whenGetStringOption("separator", options, sep);
|
||||
whenGetIntegerOption("limit", options, limit);
|
||||
whenGetIntegerOption("skipDataLines", options, skip);
|
||||
whenGetIntegerOption("ignoreLines", options, ignoreLines);
|
||||
whenGetIntegerOption("headerLines", options, headerLines);
|
||||
whenGetBooleanOption("guessCellValueTypes", options, guessValueType);
|
||||
whenGetBooleanOption("processQuotes", options, !ignoreQuotes);
|
||||
whenGetBooleanOption("storeBlankCellsAsNulls", options, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up the projects and jobs created with createCSVProject
|
||||
*/
|
||||
@AfterMethod
|
||||
private void cleanupProjectsAndJobs() {
|
||||
for(ImportingJob job : importingJobs) {
|
||||
ImportingManager.disposeJob(job.id);
|
||||
}
|
||||
for(Project project: projects) {
|
||||
ProjectManager.singleton.deleteProject(project.id);
|
||||
}
|
||||
servlet = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,37 +1,22 @@
|
||||
|
||||
package com.google.refine.tests.expr.functions;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONObject;
|
||||
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.ProjectManager;
|
||||
import com.google.refine.ProjectMetadata;
|
||||
import com.google.refine.RefineServlet;
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.HasFieldsListImpl;
|
||||
import com.google.refine.expr.WrappedRow;
|
||||
import com.google.refine.grel.ControlFunctionRegistry;
|
||||
import com.google.refine.grel.Function;
|
||||
import com.google.refine.importers.SeparatorBasedImporter;
|
||||
import com.google.refine.importing.ImportingJob;
|
||||
import com.google.refine.importing.ImportingManager;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.tests.ProjectManagerStub;
|
||||
import com.google.refine.tests.RefineServletStub;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
|
||||
/**
|
||||
@ -47,82 +32,33 @@ public class CrossFunctionTests extends RefineTest {
|
||||
}
|
||||
|
||||
// dependencies
|
||||
RefineServlet servlet;
|
||||
Project projectGift;
|
||||
Project projectAddress;
|
||||
ProjectMetadata metadata;
|
||||
ImportingJob job;
|
||||
JSONObject options;
|
||||
SeparatorBasedImporter importer;
|
||||
|
||||
|
||||
// data from: https://github.com/OpenRefine/OpenRefine/wiki/GREL-Other-Functions
|
||||
@BeforeMethod
|
||||
public void SetUp() {
|
||||
bindings = new Properties();
|
||||
|
||||
servlet = new RefineServletStub();
|
||||
ProjectManager.singleton = new ProjectManagerStub();
|
||||
ImportingManager.initialize(servlet);
|
||||
projectAddress = new Project();
|
||||
|
||||
job = ImportingManager.createJob();
|
||||
options = mock(JSONObject.class);
|
||||
importer = new SeparatorBasedImporter();
|
||||
String projectName = "My Address Book";
|
||||
String input = "friend,address\n"
|
||||
+ "john,120 Main St.\n"
|
||||
+ "mary,50 Broadway Ave.\n"
|
||||
+ "john,999 XXXXXX St.\n" // john's 2nd address
|
||||
+ "anne,17 Morning Crescent\n";
|
||||
projectAddress = createCSVProject(projectName, input);
|
||||
|
||||
projectName = "Christmas Gifts";
|
||||
input = "gift,recipient\n"
|
||||
+ "lamp,mary\n"
|
||||
+ "clock,john\n";
|
||||
projectGift = createCSVProject(projectName, input);
|
||||
|
||||
createMyAddressBook();
|
||||
projectGift = createChristmasGifts();
|
||||
bindings.put("project", projectGift);
|
||||
|
||||
// add a column address based on column recipient
|
||||
bindings.put("columnName", "recipient");
|
||||
}
|
||||
|
||||
// data from: https://github.com/OpenRefine/OpenRefine/wiki/GREL-Other-Functions
|
||||
private Project createMyAddressBook() {
|
||||
String projectName = "My Address Book";
|
||||
String input = "friend;address\n"
|
||||
+ "john;120 Main St.\n"
|
||||
+ "mary;50 Broadway Ave.\n"
|
||||
+ "john;999 XXXXXX St.\n" // john's 2nd address
|
||||
+ "anne;17 Morning Crescent\n";
|
||||
return createProject(projectName, input);
|
||||
}
|
||||
|
||||
private Project createChristmasGifts() {
|
||||
String projectName = "Christmas Gifts";
|
||||
String input = "gift;recipient\n"
|
||||
+ "lamp;mary\n"
|
||||
+ "clock;john\n";
|
||||
return createProject(projectName, input);
|
||||
}
|
||||
|
||||
private Project createProject(String projectName, String input) {
|
||||
Project project = new Project();
|
||||
ProjectMetadata metadata = new ProjectMetadata();
|
||||
|
||||
metadata.setName(projectName);
|
||||
prepareOptions(";", -1, 0, 0, 1, false, false);
|
||||
List<Exception> exceptions = new ArrayList<Exception>();
|
||||
importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
|
||||
project.update();
|
||||
ProjectManager.singleton.registerProject(project, metadata);
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void TearDown() {
|
||||
ImportingManager.disposeJob(job.id);
|
||||
ProjectManager.singleton.deleteProject(projectGift.id);
|
||||
ProjectManager.singleton.deleteProject(projectAddress.id);
|
||||
job = null;
|
||||
metadata = null;
|
||||
projectGift = null;
|
||||
projectAddress = null;
|
||||
options = null;
|
||||
importer = null;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void crossFunctionOneToOneTest() throws Exception {
|
||||
Row row = ((Row)((WrappedRow) ((HasFieldsListImpl) invoke("cross", "mary", "My Address Book", "friend")).get(0)).row);
|
||||
@ -189,20 +125,4 @@ public class CrossFunctionTests extends RefineTest {
|
||||
return function.call(bindings,args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void prepareOptions(
|
||||
String sep, int limit, int skip, int ignoreLines,
|
||||
int headerLines, boolean guessValueType, boolean ignoreQuotes) {
|
||||
|
||||
whenGetStringOption("separator", options, sep);
|
||||
whenGetIntegerOption("limit", options, limit);
|
||||
whenGetIntegerOption("skipDataLines", options, skip);
|
||||
whenGetIntegerOption("ignoreLines", options, ignoreLines);
|
||||
whenGetIntegerOption("headerLines", options, headerLines);
|
||||
whenGetBooleanOption("guessCellValueTypes", options, guessValueType);
|
||||
whenGetBooleanOption("processQuotes", options, !ignoreQuotes);
|
||||
whenGetBooleanOption("storeBlankCellsAsNulls", options, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user