### Question:
CSVSchemaCommand extends BaseCommand { @Override public int run() throws IOException { Preconditions.checkArgument(samplePaths != null && !samplePaths.isEmpty(), "Sample CSV path is required"); Preconditions.checkArgument(samplePaths.size() == 1, "Only one CSV sample can be given"); if (header != null) { noHeader = true; } CSVProperties props = new CSVProperties.Builder() .delimiter(delimiter) .escape(escape) .quote(quote) .header(header) .hasHeader(!noHeader) .linesToSkip(linesToSkip) .charset(charsetName) .build(); Set<String> required = ImmutableSet.of(); if (requiredFields != null) { required = ImmutableSet.copyOf(requiredFields); } String sampleSchema = CSVUtil .inferNullableSchema( recordName, open(samplePaths.get(0)), props, required) .toString(!minimize); output(sampleSchema, console, outputPath); return 0; } CSVSchemaCommand(Logger console); @Override int run(); @Override List<String> getExamples(); @Override void setConf(Configuration conf); @Override Configuration getConf();  }### Answer:
@Test public void testSchemaStdout() throws Exception { command.samplePaths = Lists.newArrayList("target/users.csv"); command.recordName = "User"; int rc = command.run(); Assert.assertEquals("Should return success code", 0, rc); verify(console).info(argThat(TestUtil.matchesSchema(schema))); verifyNoMoreInteractions(console); }
@Test public void testSchemaRequiredFields() throws Exception { command.samplePaths = Lists.newArrayList("target/users.csv"); command.recordName = "User"; command.requiredFields = Lists.newArrayList("id"); int rc = command.run(); Assert.assertEquals("Should return success code", 0, rc); verify(console).info(argThat(TestUtil.matchesSchema(requiredSchema))); verifyNoMoreInteractions(console); }
@Test public void testSchemaToFile() throws Exception { command.samplePaths = Lists.newArrayList("target/users.csv"); command.recordName = "User"; command.outputPath = "target/user.avsc"; int rc = command.run(); Assert.assertEquals("Should return success code", 0, rc); String fileContent = Files.toString( new File("target/user.avsc"), BaseCommand.UTF8); Assert.assertTrue("File should contain pretty printed schema", TestUtil.matchesSchema(schema).matches(fileContent)); verifyNoMoreInteractions(console); }
@Test public void testMultipleSamplesFail() throws Exception { command.samplePaths = Lists.newArrayList(sample, "target/sample2.csv"); TestHelpers.assertThrows("Should reject saving multiple schemas in a file", IllegalArgumentException.class, new Callable<Void>() { @Override public Void call() throws Exception { command.run(); return null; } } ); verifyNoMoreInteractions(console); }
@Test public void testMinimize() throws Exception { command.samplePaths = Lists.newArrayList("target/users.csv"); command.recordName = "User"; command.minimize = true; int rc = command.run(); Assert.assertEquals("Should return success code", 0, rc); verify(console).info(argThat(TestUtil.matchesMinimizedSchema(schema))); verifyNoMoreInteractions(console); }
@Test public void testMissingSamplePath() { TestHelpers.assertThrows("Should complain when no sample csv is given", IllegalArgumentException.class, new Callable<Void>() { @Override public Void call() throws Exception { command.run(); return null; } }); verifyZeroInteractions(console); }
@Test public void testInvalidCSVHeaderFail() throws Exception { command.samplePaths = Lists.newArrayList("target/users_failed.csv"); command.recordName = "User"; TestHelpers.assertThrows("Should fail when csv header doesn't follow alphanumeric standards", DatasetException.class, new Callable<Void>() { @Override public Void call() throws Exception { command.run(); return null; } }); verifyZeroInteractions(console); }