Skip to content

Commit

Permalink
little refactoring, improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojta Hordejcuk committed Nov 25, 2016
1 parent 3371856 commit 99b87f5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void testInsertSingleRowAndGetKeyUsingPreparedStatement() throws Exceptio
assertThat(actual).isGreaterThan(1);
}


@Test
public void testInsertSingleRowAndGetKeyUsingRawValues() throws Exception {
final Long actual = toTest().insertSingleAndGetFirstLongKey("INSERT INTO t (c_text) VALUES ('testInsertSingleRowAndGetKeyUsingRawValues')");
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/cz/voho/jdbcis/operation/ListQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class ListQueriesTest extends AbstractOperationTest {
@Test
public void queryForListUsingPreparedStatement() throws Exception {
final List<TestRow> list = toTest().queryForList(
"SELECT * FROM t WHERE c_text LIKE ?",
"SELECT c_pk,c_decimal,c_date,c_timestamp,c_double,c_integer,c_bigint,c_varchar,c_text,c_time FROM t WHERE c_text LIKE ?",
preparedStatement -> DataType.STRING.setNullableToPreparedStatement(preparedStatement, 1, "Predefined-%"),
TestRow.ROW_MAPPER
);

assertThat(list)
.extracting(TestRow::getcString)
.extracting(TestRow::getcLongString)
.contains(
"Predefined-1",
"Predefined-2",
Expand All @@ -34,12 +34,12 @@ public void queryForListUsingPreparedStatement() throws Exception {
@Test
public void queryForListUsingRawValues() throws Exception {
final List<TestRow> list = toTest().queryForList(
"SELECT * FROM t WHERE c_text LIKE 'Predefined-%'",
"SELECT c_pk,c_decimal,c_date,c_timestamp,c_double,c_integer,c_bigint,c_varchar,c_text,c_time FROM t WHERE c_text LIKE 'Predefined-%'",
TestRow.ROW_MAPPER
);

assertThat(list)
.extracting(TestRow::getcString)
.extracting(TestRow::getcLongString)
.contains(
"Predefined-1",
"Predefined-2",
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/cz/voho/jdbcis/operation/SingleQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
public class SingleQueriesTest extends AbstractOperationTest {
@Test
public void queryForSingleRowUsingPreparedStatement() throws Exception {
TestRow row = toTest().queryForSingle(
"SELECT * FROM t WHERE c_pk = ?",
final TestRow row = toTest().queryForSingle(
"SELECT c_pk,c_decimal,c_date,c_timestamp,c_double,c_integer,c_bigint,c_varchar,c_text,c_time FROM t WHERE c_pk = ?",
preparedStatement -> DataType.LONG.setNullableToPreparedStatement(preparedStatement, 1, 1L),
TestRow.ROW_MAPPER);

assertThat(row.getcString()).isEqualTo("Predefined-1");
assertThat(row.getcLongString()).isEqualTo("Predefined-1");
}


@Test
public void queryForSingleRowUsingRawValues() throws Exception {
TestRow row = toTest().queryForSingle(
"SELECT * FROM t WHERE c_pk = 1",
final TestRow row = toTest().queryForSingle(
"SELECT c_pk,c_decimal,c_date,c_timestamp,c_double,c_integer,c_bigint,c_varchar,c_text,c_time FROM t WHERE c_pk = 1",
TestRow.ROW_MAPPER);

assertThat(row.getcString()).isEqualTo("Predefined-1");
assertThat(row.getcLongString()).isEqualTo("Predefined-1");
}
}
4 changes: 2 additions & 2 deletions src/test/java/cz/voho/jdbcis/operation/UpdateQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class UpdateQueriesTest extends AbstractOperationTest {
@Test
public void updateSingleRowUsingPreparedStatement() throws Exception {
long actual = toTest().update("UPDATE t SET c_text = ?", preparedStatement -> {
final long actual = toTest().update("UPDATE t SET c_text = ?", preparedStatement -> {
DataType.STRING.setNullableToPreparedStatement(preparedStatement, 1, "updateSingleRowUsingPreparedStatement");
});

Expand All @@ -20,7 +20,7 @@ public void updateSingleRowUsingPreparedStatement() throws Exception {

@Test
public void updateSingleRowUsingRawValues() throws Exception {
long actual = toTest().update("UPDATE t SET c_text = 'updateSingleRowUsingRawValues'");
final long actual = toTest().update("UPDATE t SET c_text = 'updateSingleRowUsingRawValues'");

assertThat(actual).isEqualTo(5L);
}
Expand Down
65 changes: 51 additions & 14 deletions src/test/java/cz/voho/jdbcis/operation/model/TestRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,95 +8,132 @@
import java.time.LocalDateTime;
import java.time.LocalTime;

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

/**
* Test model object.
*/
public class TestRow {
public static RowMapper<TestRow> ROW_MAPPER = resultSet -> {
TestRow row = new TestRow();

// primary key
row.setcLong(DataType.LONG.getNullableFromResultSet(resultSet, "c_pk"));
row.setcPK(DataType.LONG.getNullableFromResultSet(resultSet, "c_pk"));
// other columns
row.setcBigDecimal(DataType.BIG_DECIMAL.getNullableFromResultSet(resultSet, "c_decimal"));
row.setcLocalDate(DataType.DATE.getNullableFromResultSet(resultSet, "c_date"));
row.setcLocalDateTime(DataType.DATE_TIME.getNullableFromResultSet(resultSet, "c_timestamp"));
row.setcDouble(DataType.DOUBLE.getNullableFromResultSet(resultSet, "c_double"));
row.setcInteger(DataType.INTEGER.getNullableFromResultSet(resultSet, "c_integer"));
row.setcLong(DataType.LONG.getNullableFromResultSet(resultSet, "c_bigint"));
row.setcString(DataType.STRING.getNullableFromResultSet(resultSet, "c_text"));
row.setcShortString(DataType.STRING.getNullableFromResultSet(resultSet, "c_varchar"));
row.setcLongString(DataType.STRING.getNullableFromResultSet(resultSet, "c_text"));
row.setcLocalTime(DataType.TIME.getNullableFromResultSet(resultSet, "c_time"));

// primary key
assertThat(DataType.LONG.getNullableFromResultSet(resultSet, 1)).isEqualTo(row.getcPK());
// other columns
assertThat(DataType.BIG_DECIMAL.getNullableFromResultSet(resultSet, 2)).isEqualTo(row.getcBigDecimal());
assertThat(DataType.DATE.getNullableFromResultSet(resultSet, 3)).isEqualTo(row.getcLocalDate());
assertThat(DataType.DATE_TIME.getNullableFromResultSet(resultSet, 4)).isEqualTo(row.getcLocalDateTime());
assertThat(DataType.DOUBLE.getNullableFromResultSet(resultSet, 5)).isEqualTo(row.getcDouble());
assertThat(DataType.INTEGER.getNullableFromResultSet(resultSet, 6)).isEqualTo(row.getcInteger());
assertThat(DataType.LONG.getNullableFromResultSet(resultSet, 7)).isEqualTo(row.getcLong());
assertThat(DataType.STRING.getNullableFromResultSet(resultSet, 8)).isEqualTo(row.getcShortString());
assertThat(DataType.STRING.getNullableFromResultSet(resultSet, 9)).isEqualTo(row.getcLongString());
assertThat(DataType.TIME.getNullableFromResultSet(resultSet, 10)).isEqualTo(row.getcLocalTime());

return row;
};

private Long cPK;
private BigDecimal cBigDecimal;
private LocalDate cLocalDate;
private LocalDateTime cLocalDateTime;
private Double cDouble;
private Integer cInteger;
private Long cLong;
private String cString;
private String cShortString;
private String cLongString;
private LocalTime cLocalTime;

public Long getcPK() {
return cPK;
}

public void setcPK(final Long cPK) {
this.cPK = cPK;
}

public BigDecimal getcBigDecimal() {
return cBigDecimal;
}

public void setcBigDecimal(BigDecimal cBigDecimal) {
public void setcBigDecimal(final BigDecimal cBigDecimal) {
this.cBigDecimal = cBigDecimal;
}

public LocalDate getcLocalDate() {
return cLocalDate;
}

public void setcLocalDate(LocalDate cLocalDate) {
public void setcLocalDate(final LocalDate cLocalDate) {
this.cLocalDate = cLocalDate;
}

public LocalDateTime getcLocalDateTime() {
return cLocalDateTime;
}

public void setcLocalDateTime(LocalDateTime cLocalDateTime) {
public void setcLocalDateTime(final LocalDateTime cLocalDateTime) {
this.cLocalDateTime = cLocalDateTime;
}

public Double getcDouble() {
return cDouble;
}

public void setcDouble(Double cDouble) {
public void setcDouble(final Double cDouble) {
this.cDouble = cDouble;
}

public Integer getcInteger() {
return cInteger;
}

public void setcInteger(Integer cInteger) {
public void setcInteger(final Integer cInteger) {
this.cInteger = cInteger;
}

public Long getcLong() {
return cLong;
}

public void setcLong(Long cLong) {
public void setcLong(final Long cLong) {
this.cLong = cLong;
}

public String getcString() {
return cString;
public String getcShortString() {
return cShortString;
}

public void setcShortString(final String cShortString) {
this.cShortString = cShortString;
}

public String getcLongString() {
return cLongString;
}

public void setcString(String cString) {
this.cString = cString;
public void setcLongString(final String cLongString) {
this.cLongString = cLongString;
}

public LocalTime getcLocalTime() {
return cLocalTime;
}

public void setcLocalTime(LocalTime cLocalTime) {
public void setcLocalTime(final LocalTime cLocalTime) {
this.cLocalTime = cLocalTime;
}
}

0 comments on commit 99b87f5

Please sign in to comment.