test: add lightweight unit coverage for EnrichmentPipeline #138#142
test: add lightweight unit coverage for EnrichmentPipeline #138#142davide954 wants to merge 4 commits intoDigitalPebble:mainfrom
Conversation
e301671 to
c33c78b
Compare
src/test/java/com/digitalpebble/spruce/EnrichmentPipelineTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/digitalpebble/spruce/EnrichmentPipelineTest.java
Outdated
Show resolved
Hide resolved
c33c78b to
2b2190a
Compare
Introduced parameterized tests for negative cases. Added assertions for enriched columns. Used CURColumn constants where available, keeping hardcoded strings for missing fields.
Added missing standard columns to CURColumn to avoid magic strings
|
Addressed the feedback from the previous review. I updated the test to use Regarding your comment about |
|
Thanks, will review more when I have more time
I think we should have in CURColumn only things that are used in the enrichment modules. The tests should also not use fields that are not needed by the modules, like |
|
Great work @davide954 🎆 |
|
Thanks @tejas-2232. |
|
|
||
| Object[] values = new Object[schema.length()]; | ||
| values[0] = "us-east-1"; // Matches CURColumn.PRODUCT_REGION_CODE | ||
| values[1] = lineItemType; // The dynamic parameter |
There was a problem hiding this comment.
line 55-57.
The hardcoded array indices depend on loop iteration order for enrichment module columns. This looks fragile. what do you think about adding something like this?
int regionIndex = schema.fieldIndex(CURColumn.PRODUCT_REGION_CODE.getLabel());
int lineItemIndex = schema.fieldIndex(CURColumn.LINE_ITEM_TYPE.getLabel());
values[regionIndex] = "us-east-1";
values[lineItemIndex] = lineItemType;There was a problem hiding this comment.
Great catch! That makes perfect sense I'll work on it.
| // Verify enrichment logic | ||
| for (EnrichmentModule module : config.getModules()) { | ||
| for (Column c : module.columnsAdded()) { | ||
| int fieldIndex = processedRow.fieldIndex(c.getLabel()); |
There was a problem hiding this comment.
Missing Null Check on fieldIndex()
fieldIndex() can throw IllegalArgumentException if field doesn't exist.
I would say -> Add try-catch or ensure field exists first
try {
int fieldIndex = processedRow.fieldIndex(c.getLabel());
// assertions...
} catch (IllegalArgumentException e) {
fail("Column " + c.getLabel() + " not found in schema: " + e.getMessage());
}There was a problem hiding this comment.
I thought about it, but I wonder if the explicit try-catch is necessary here. Since this is a unit test, if fieldIndex() throws an IllegalArgumentException, the test will automatically fail with the exception message (which already identifies the missing field).
I'd prefer to keep the loop clean and let JUnit handle the exception as a test failure, unless you have a specific case in mind where masking the exception is better? In this case enlighten me
There was a problem hiding this comment.
I thought about it, but I wonder if the explicit
try-catchis necessary here. Since this is a unit test, iffieldIndex()throws anIllegalArgumentException, the test will automatically fail with the exception message (which already identifies the missing field).I'd prefer to keep the loop clean and let
JUnithandle the exception as a test failure, unless you have a specific case in mind where masking the exception is better? In this case enlighten me
haha, right. I went too far in thinking. That works anytime.
| .add(CURColumn.PRODUCT_REGION_CODE.getLabel(), CURColumn.PRODUCT_REGION_CODE.getType(), true) | ||
| .add(CURColumn.LINE_ITEM_TYPE.getLabel(), CURColumn.LINE_ITEM_TYPE.getType(), true); | ||
|
|
||
| for (EnrichmentModule module : config.getModules()) { |
There was a problem hiding this comment.
No Assertion on Module Count:
If config.getModules() returns an empty list then the test passes without actually validating anything.
I would say - you can add below assertion right after pipeline is created.
Let me know what you think? 😃
List<EnrichmentModule> modules = config.getModules();
assertTrue(!modules.isEmpty(), "Config should have at least one enrichment module for this test");
// OR below one
assertTrue(modules.size() > 0, "Config should have at least one enrichment module for this test");There was a problem hiding this comment.
I'll push tomorrow! Now I'm signing off as it's almost midnight here in Italy. Thanks for the sharp review @tejas-2232, catch you later!
This PR adds a pure JUnit test (
EnrichmentPipelineTest.java) for theEnrichmentPipelineclass.Instead of spinning up a full SparkSession (which is slow and can have path issues on Windows), this test:
Rowobjects.Passes with
mvn clean test.