Skip to content

Commit

Permalink
Add failing test for #285
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 24, 2021
1 parent e00bab9 commit 340bd18
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ static class ABC {
public String b = "b";
public String c = "c";
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

final CsvMapper MAPPER = mapperForCsv();
private final CsvMapper MAPPER = mapperForCsv();

final CsvSchema SCHEMA = MAPPER.schemaFor(ABC.class);
private final CsvSchema SCHEMA_ABC = MAPPER.schemaFor(ABC.class);

// by default, just... ignore
public void testDefaultMissingHandling() throws Exception
{
ObjectReader r = MAPPER.readerFor(ABC.class).with(SCHEMA);
ObjectReader r = MAPPER.readerFor(ABC.class).with(SCHEMA_ABC);
final ABC DEFAULT = new ABC();

ABC result = r.readValue("first,second,third\n");
Expand Down Expand Up @@ -65,7 +65,7 @@ public void testDefaultMissingHandling() throws Exception
public void testInjectMissingAsNulls() throws Exception
{
ObjectReader r = MAPPER.readerFor(ABC.class)
.with(SCHEMA)
.with(SCHEMA_ABC)
.with(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS);

// check with various number of missing; but first with no missing
Expand Down Expand Up @@ -95,7 +95,7 @@ public void testInjectMissingAsNulls() throws Exception
public void testFailOnMissingColumns() throws Exception
{
ObjectReader r = MAPPER.readerFor(ABC.class)
.with(SCHEMA)
.with(SCHEMA_ABC)
.with(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS);

// check with various number of missing, as well as recovery
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fasterxml.jackson.dataformat.csv.failing;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.databind.MappingIterator;

import com.fasterxml.jackson.dataformat.csv.*;

/**
* Tests for cases where one more of schema-declared columns is
* missing; various handling choices include "null-injection"
* as well as failure (throw exception) and just skipping (default).
*/
public class MissingColumns285Test extends ModuleTestBase
{
@JsonPropertyOrder({ "name", "age" })
static class Person {
public String name;
public int age;
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final CsvMapper MAPPER = mapperForCsv();

// [dataformats-text#285]
public void testMissingWithReorder() throws Exception
{
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).setReorderColumns(true)
.addColumn("name").addColumn("age").build();
final String CSV = "name\nRoger\n";
MappingIterator<Person> it = MAPPER
.readerFor(Person.class)
.with(csvSchema)
.readValues(CSV);
try {
it.nextValue();
fail("Should not pass with missing columns");
} catch (CsvReadException e) {
verifyException(e, "Not enough column values");
verifyException(e, "expected 2, found 1");
}
}
}

0 comments on commit 340bd18

Please sign in to comment.