Skip to content

Commit be7513a

Browse files
authored
Merge pull request #367 from marklogic/feature/maybe-bug
Added tests for transform errors
2 parents fc83ea0 + 0b91cd1 commit be7513a

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

marklogic-spark-connector/src/main/java/com/marklogic/spark/reader/document/ForestReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.marklogic.spark.ContextSupport;
1818
import com.marklogic.spark.Options;
1919
import com.marklogic.spark.ReadProgressLogger;
20+
import org.apache.commons.io.IOUtils;
2021
import org.apache.spark.sql.catalyst.InternalRow;
2122
import org.apache.spark.sql.connector.read.PartitionReader;
2223
import org.slf4j.Logger;
@@ -160,7 +161,7 @@ private DocumentPage readPage(List<String> uris) {
160161

161162
private void closeCurrentDocumentPage() {
162163
if (currentDocumentPage != null) {
163-
currentDocumentPage.close();
164+
IOUtils.closeQuietly(currentDocumentPage);
164165
currentDocumentPage = null;
165166
}
166167
}

marklogic-spark-connector/src/test/java/com/marklogic/spark/reader/document/ReadDocumentRowsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ void withTransform() {
308308
assertEquals("value2", params.get("param2").asText());
309309
}
310310

311+
@Test
312+
void transformThrowsError() {
313+
Dataset<Row> dataset = startRead()
314+
.option(Options.READ_DOCUMENTS_STRING_QUERY, "Vivianne")
315+
.option(Options.READ_DOCUMENTS_TRANSFORM, "throwError")
316+
.load();
317+
318+
SparkException ex = assertThrows(SparkException.class, () -> dataset.count());
319+
assertTrue(ex.getMessage().contains("This is an intentional error for testing purposes."),
320+
"When the transform throws an error, our connector throws a ConnectorException, but Spark seems to wrap " +
321+
"its stacktrace into a SparkException, such that we can't access the original ConnectorException " +
322+
"object. But the transform error should be in the error message. " +
323+
"Actual message: " + ex.getMessage());
324+
}
325+
311326
private DataFrameReader startRead() {
312327
return newSparkSession().read()
313328
.format(CONNECTOR_IDENTIFIER)

marklogic-spark-connector/src/test/java/com/marklogic/spark/writer/WriteRowsWithTransformTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,23 @@ void withParamsAndCustomDelimiter() {
7676
assertEquals("value,2", doc.get("params").get("param2").asText());
7777
}
7878

79+
@Test
80+
void transformThrowsError() {
81+
ConnectorException ex = assertThrowsConnectorException(() -> newWriterForSingleRow()
82+
.option(Options.WRITE_TRANSFORM_NAME, "throwError")
83+
.save());
84+
85+
assertTrue(ex.getMessage().contains("This is an intentional error for testing purposes."),
86+
"The transform is expected to throw an error which should be caught by " +
87+
"WriteBatcherDataWriter and then thrown as a ConnectorException. " +
88+
"Actual error: " + ex.getMessage());
89+
}
90+
7991
@Test
8092
void invalidTransform() {
8193
ConnectorException ex = assertThrowsConnectorException(() -> newWriterForSingleRow()
82-
.option(Options.WRITE_TRANSFORM_NAME, "this-doesnt-exist")
83-
.save());
94+
.option(Options.WRITE_TRANSFORM_NAME, "this-doesnt-exist")
95+
.save());
8496

8597
assertTrue(ex.getMessage().contains("Extension this-doesnt-exist or a dependency does not exist"),
8698
"The connector can't easily validate that a REST transform is valid, but the expectation is that the " +
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function transform(context, params, content) {
2+
fn.error(xs.QName("ERROR"), "This is an intentional error for testing purposes.");
3+
return content;
4+
};
5+
6+
exports.transform = transform;

0 commit comments

Comments
 (0)