|
| 1 | +/* |
| 2 | + * Licensed to ObjectStyle LLC under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ObjectStyle LLC licenses |
| 6 | + * this file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.bootique.jdbc.junit5.derby.matcher; |
| 21 | + |
| 22 | +import io.bootique.jdbc.junit5.Table; |
| 23 | +import io.bootique.jdbc.junit5.derby.DerbyTester; |
| 24 | +import io.bootique.jdbc.junit5.matcher.CsvTableMatcher; |
| 25 | +import io.bootique.junit5.BQTest; |
| 26 | +import io.bootique.junit5.BQTestTool; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.math.BigDecimal; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 33 | + |
| 34 | +@BQTest |
| 35 | +public class CsvTableMatcherIT { |
| 36 | + |
| 37 | + @BQTestTool |
| 38 | + static final DerbyTester db = DerbyTester |
| 39 | + .db() |
| 40 | + .deleteBeforeEachTest("t1", "t2", "t3", "t4"); |
| 41 | + |
| 42 | + private static Table T1; |
| 43 | + private static Table T2; |
| 44 | + private static Table T3; |
| 45 | + private static Table T4; |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + public static void setupDB() { |
| 49 | + |
| 50 | + db.execStatement().exec("CREATE TABLE \"t1\" (\"c1\" INT, \"c2\" VARCHAR(10), \"c3\" VARCHAR(10))"); |
| 51 | + db.execStatement().exec("CREATE TABLE \"t2\" (\"c1\" INT, \"c2\" INT, \"c3\" DATE, \"c4\" TIMESTAMP)"); |
| 52 | + db.execStatement().exec("CREATE TABLE \"t3\" (\"c1\" INT, \"c2\" VARCHAR (10) FOR BIT DATA)"); |
| 53 | + db.execStatement().exec("CREATE TABLE \"t4\" (\"c1\" BIGINT, \"c2\" VARCHAR (10), \"c3\" DECIMAL(10,2))"); |
| 54 | + |
| 55 | + T1 = db.getTable("t1"); |
| 56 | + T2 = db.getTable("t2"); |
| 57 | + T3 = db.getTable("t3"); |
| 58 | + T4 = db.getTable("t4"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void assertMatchesCsv() { |
| 63 | + |
| 64 | + T1.insertColumns("c1", "c2", "c3") |
| 65 | + .values(2, "tt", "xyz") |
| 66 | + .values(1, "", "abcd") |
| 67 | + .exec(); |
| 68 | + |
| 69 | + new CsvTableMatcher(T1).keyColumns("c1").assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t1_ref.csv"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void assertMatchesCsv_EmptyAsNulls() { |
| 74 | + |
| 75 | + T1.insertColumns("c1", "c2", "c3") |
| 76 | + .values(2, "tt", "xyz") |
| 77 | + .values(1, null, "abcd") |
| 78 | + .exec(); |
| 79 | + |
| 80 | + new CsvTableMatcher(T1) |
| 81 | + .keyColumns("c1") |
| 82 | + .emptyStringIsNull() |
| 83 | + .assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t1_ref.csv"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void assertMatchesCsv_NoMatch() { |
| 88 | + |
| 89 | + T1.insertColumns("c1", "c2", "c3") |
| 90 | + .values(1, "tt", "xyz") |
| 91 | + .values(2, "", "abcd") |
| 92 | + .exec(); |
| 93 | + |
| 94 | + boolean succeeded; |
| 95 | + try { |
| 96 | + new CsvTableMatcher(T1).keyColumns("c1").assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t1_ref.csv"); |
| 97 | + succeeded = true; |
| 98 | + } catch (AssertionError e) { |
| 99 | + // expected |
| 100 | + succeeded = false; |
| 101 | + } |
| 102 | + |
| 103 | + assertFalse(succeeded, "Must have failed - data sets do not match"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void assertMatchesCsv_Dates() { |
| 108 | + |
| 109 | + |
| 110 | + T2.insertColumns("c1", "c2", "c3", "c4") |
| 111 | + .values(3, null, "2018-01-09", "2018-01-10 14:00:01") |
| 112 | + .values(1, null, "2016-01-09", "2016-01-10 10:00:00") |
| 113 | + .values(2, null, "2017-01-09", "2017-01-10 13:00:01") |
| 114 | + .exec(); |
| 115 | + |
| 116 | + new CsvTableMatcher(T2).keyColumns("c1").assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t2_ref.csv"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void assertMatchesCsv_Binary() { |
| 121 | + |
| 122 | + T3.insertColumns("c1", "c2") |
| 123 | + .values(3, null) |
| 124 | + .values(1, "abcd".getBytes()) |
| 125 | + .values(2, "kmln".getBytes()) |
| 126 | + .exec(); |
| 127 | + |
| 128 | + new CsvTableMatcher(T3).keyColumns("c1").assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t3_ref.csv"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void assertMatchesCsv_Numbers() { |
| 133 | + |
| 134 | + T4.insertColumns("c1", "c2", "c3") |
| 135 | + .values(1L, "abc", new BigDecimal("2.34")) |
| 136 | + .values(2L, "xyz", BigDecimal.ZERO) |
| 137 | + .exec(); |
| 138 | + |
| 139 | + new CsvTableMatcher(T4).keyColumns("c1").assertMatches("classpath:io/bootique/jdbc/junit5/derby/matcher/t4_ref.csv"); |
| 140 | + } |
| 141 | +} |
0 commit comments