Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add csv parsing #11

Closed
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- checkout
- run: |
./gradlew clean test
./gradlew clean test -i
- store_test_results:
path: build/test-results
- store_artifacts:
Expand Down
8 changes: 6 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ repositories {
}

dependencies {
implementation("org.apache.commons:commons-csv:1.9.0")
implementation("org.apache.commons:commons-lang3:3.12.0")

testImplementation("org.assertj:assertj-core:3.21.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}

tasks.getByName<Test>("test") {
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/org/example/csv2tex/HelloWorld.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/org/example/csv2tex/data/SchoolReportData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.example.csv2tex.data;

import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;

/**
* Data class representing one school report (report card).
* <p></p>
* One such dataset contains data for
* <ul>
* <li>one student</li>
* <li>one term</li>
* <li>all subjects + respective school grade</li>
* <li>a number of boolean variables for parameters (e.g. "passed the term")</li>
* </ul>
*/
public class SchoolReportData {

// TODO:
// In general, names are a complex topic, a separation into surname and given name is not always possible
// like that.
// Please consider if a simple "name" field containing everything would be an option.
public String surname = "";
public String givenName = "";

/**
* Birthdate, represented as a String - we don't want to care about complex date formats etc
*/
public String birthday = "";

/**
* Ordered listing of subjects and respective grades.
* <p>
* Again, represented as a String, as we don't want to take care of
* different grade representations (A, B, C... vs 1, 2, 3, ... vs ?).
*/
public final List<Pair<String, String>> subjectToGrade = new ArrayList<>();

/**
* Boolean data that will be part of the report, e.g. "advances to the next grade" or "will stay down / repeat the grade"
*/
public final List<Boolean> booleanInformation = new ArrayList<>();

}
33 changes: 33 additions & 0 deletions src/main/java/org/example/csv2tex/data/SchoolReportDataParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.example.csv2tex.data;

import java.io.File;
import java.util.List;

public class SchoolReportDataParser {

private int numberOfSubjects;

/**
* Parses data from a given CSV file into {@link SchoolReportData} structures.
* <p></p>
* It assumes the following columns in the given CSV file, and thus only needs the number of subjects as input parameter:
* <ul>
* <li>last name</li>
* <li>given name</li>
* <li>date of birth</li>
* <li>a number of column pairs: subject, followed by grade</li>
* <li>remaining columns are assumed to represent boolean values</li>
* </ul>
* (Other approaches would be just as easily possible)
*
* @param numberOfSubjects number of subjects contained in the report data
*/
public SchoolReportDataParser(int numberOfSubjects) {
this.numberOfSubjects = numberOfSubjects;
}

public List<SchoolReportData> parseCsvFileToDataList(File csvFile) {
// FIXME DonMischo
throw new RuntimeException("implement me");
}
}
87 changes: 87 additions & 0 deletions src/test/java/org/example/csv2tex/CommonsCsvLearningTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.example.csv2tex;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Just to try out how commons CSV works
*/
public class CommonsCsvLearningTest {

public enum Header {
Name, Email, Comment
}

@Test
public void tryOutCommonsCsvParsing() throws IOException {
// arrange
File csvFile = getCsvFileFromClasspath();

// act
List<CSVRecord> recordList = new ArrayList<>();
try (Reader in = new FileReader(csvFile)) {
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
records.forEach(recordList::add);
}

// assert
assertThat(recordList)
.describedAs("should contain header, then 2 lines of content")
.hasSize(3);
CSVRecord record1 = recordList.get(1);
CSVRecord record2 = recordList.get(2);
assertThat(record1.get(0)).isEqualTo("robert");
assertThat(record1.get(1).trim()).isEqualTo("robert@example.com");
assertThat(record1.get(2).trim()).isEqualTo("test data set 1");
assertThat(record2.get(0)).isEqualTo("micha");
assertThat(record2.get(1).trim()).isEqualTo("micha@example.com");
assertThat(record2.get(2).trim()).isEqualTo("test data set 2");
}

@Test
public void tryOutCommonsCsvParsingWithOptions() throws IOException {
// arrange
File csvFile = getCsvFileFromClasspath();
CSVFormat parser = CSVFormat.DEFAULT.builder()
.setTrim(true)
.setSkipHeaderRecord(true)
.setHeader(Header.class)
.build();

// act
List<CSVRecord> recordList = new ArrayList<>();
try (Reader in = new FileReader(csvFile)) {
Iterable<CSVRecord> records = parser.parse(in);
records.forEach(recordList::add);
}

// assert
assertThat(recordList)
.describedAs("should contain 2 lines of content (no header)")
.hasSize(2);
CSVRecord record1 = recordList.get(0);
CSVRecord record2 = recordList.get(1);
assertThat(record1.get(Header.Name)).isEqualTo("robert");
assertThat(record1.get(Header.Email)).isEqualTo("robert@example.com");
assertThat(record1.get(Header.Comment)).isEqualTo("test data set 1");
assertThat(record2.get(Header.Name)).isEqualTo("micha");
assertThat(record2.get(Header.Email)).isEqualTo("micha@example.com");
assertThat(record2.get(Header.Comment)).isEqualTo("test data set 2");
}

private File getCsvFileFromClasspath() {
URL resource = getClass().getClassLoader().getResource("csv/testCsv.csv");
assertThat(resource)
.describedAs("file not found in classpath: testCsv.csv")
.isNotNull();
return new File(resource.getFile());
}
}
25 changes: 0 additions & 25 deletions src/test/java/org/example/csv2tex/HelloWorldTest.java

This file was deleted.

Loading