Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/java/com/sciencesakura/dbsetup/csv/Import.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public static Builder csv(@NotNull String location) {
return new Builder(urlLocation);
}

/**
* Creates a new {@code Import.Builder} instance with TSV format.
*
* @param location the {@code /}-separated path from classpath root to the TSV file
* @return the new {@code Import.Builder} instance
* @throws IllegalArgumentException if the TSV file is not found
*/
@NotNull
public static Builder tsv(@NotNull String location) {
return csv(location).withDelimiter('\t');
}

private static CSVFormat createFormat(Builder builder) {
var fb = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setDelimiter(builder.delimiter)
Expand Down
18 changes: 16 additions & 2 deletions core/src/test/java/com/sciencesakura/dbsetup/csv/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static com.ninja_squad.dbsetup.Operations.sql;
import static com.ninja_squad.dbsetup.Operations.truncate;
import static com.sciencesakura.dbsetup.csv.Import.csv;
import static com.sciencesakura.dbsetup.csv.Import.tsv;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.db.api.Assertions.assertThat;

Expand Down Expand Up @@ -281,8 +282,21 @@ void specify_delimiter_explicitly() {
.hasNumberOfChanges(1)
.changeOfCreation()
.rowAtEndPoint()
.value("id").isEqualTo(1)
.value("name").isEqualTo("Alice");
.value("id").isEqualTo(2)
.value("name").isEqualTo("Bob");
}

@Test
void use_tab_if_tsv_function_invoked() {
changes.setStartPointNow();
var operation = tsv("WithDelimiter/with_delimiter.tsv").build();
new DbSetup(destination, operation).launch();
assertThat(changes.setEndPointNow())
.hasNumberOfChanges(1)
.changeOfCreation()
.rowAtEndPoint()
.value("id").isEqualTo(2)
.value("name").isEqualTo("Bob");
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/WithDelimiter/with_delimiter.tsv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
id name
1 Alice
2 Bob
26 changes: 26 additions & 0 deletions kotlin/src/main/kotlin/com/sciencesakura/dbsetup/csv/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ fun DbSetupBuilder.csv(
builder.configure()
this.execute(builder.build())
}

/**
* Creates a TSV import operation.
*
* @param location the `/`-separated path from classpath root to the TSV file
* @throws IllegalArgumentException if the TSV file is not found
*/
fun DbSetupBuilder.tsv(location: String) {
this.execute(Import.tsv(location).build())
}

/**
* Creates a TSV import operation.
*
* @param location the `/`-separated path from classpath root to the TSV file
* @param configure A lambda to configure the import operation
* @throws IllegalArgumentException if the TSV file is not found
*/
fun DbSetupBuilder.tsv(
location: String,
configure: Import.Builder.() -> Unit,
) {
val builder = Import.tsv(location)
builder.configure()
this.execute(builder.build())
}
32 changes: 32 additions & 0 deletions kotlin/src/test/kotlin/com/sciencesakura/dbsetup/csv/CsvTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ class CsvTest {
}
}.launch()
@Suppress("ktlint:standard:chain-method-continuation")
assertThat(changes.setEndPointNow())
.hasNumberOfChanges(1)
.changeOfCreation()
.rowAtEndPoint()
.value("id").isEqualTo(2)
.value("name").isEqualTo("bar")
}

@Test
fun import_tsv() {
changes.setStartPointNow()
dbSetup(destination) {
tsv("kt_test.tsv")
}.launch()
@Suppress("ktlint:standard:chain-method-continuation")
assertThat(changes.setEndPointNow())
.hasNumberOfChanges(1)
.changeOfCreation()
.rowAtEndPoint()
.value("id").isEqualTo(2)
.value("name").isEqualTo("bar")
}

@Test
fun import_tsv_with_configure() {
changes.setStartPointNow()
dbSetup(destination) {
tsv("kt_test.csv") {
withDelimiter(',')
}
}.launch()
@Suppress("ktlint:standard:chain-method-continuation")
assertThat(changes.setEndPointNow())
.hasNumberOfChanges(1)
.changeOfCreation()
Expand Down
2 changes: 1 addition & 1 deletion kotlin/src/test/resources/kt_test.tsv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
id name
1 foo
2 bar