Skip to content

Commit c7f1339

Browse files
committed
Added test for task 180.
1 parent c03a6eb commit c7f1339

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
22
select distinct num as ConsecutiveNums from
3-
(select num, lag(num,1) over() as l1, lag(num,2) over() as l2
3+
(select num, lag(num,1) over(order by id) as l1, lag(num,2) over(order by id) as l2
44
from Logs) con_thr
55
where num = l1 and num = l2
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g0101_0200.s0180_consecutive_numbers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.DriverManager;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.stream.Collectors;
15+
import org.junit.Rule;
16+
import org.junit.Test;
17+
import org.zapodot.junit.db.EmbeddedDatabaseRule;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
public class MysqlTest {
21+
@Rule
22+
public final EmbeddedDatabaseRule embeddedDatabaseRule =
23+
EmbeddedDatabaseRule.builder()
24+
.withMode(CompatibilityMode.MySQL)
25+
.withInitialSql(
26+
"CREATE TABLE Logs(id INTEGER PRIMARY KEY, num INTEGER); "
27+
+ "INSERT INTO Logs(id, num) VALUES (1, 1); "
28+
+ "INSERT INTO Logs(id, num) VALUES (2, 1); "
29+
+ "INSERT INTO Logs(id, num) VALUES (3, 1); "
30+
+ "INSERT INTO Logs(id, num) VALUES (4, 2); "
31+
+ "INSERT INTO Logs(id, num) VALUES (5, 1); "
32+
+ "INSERT INTO Logs(id, num) VALUES (6, 2); "
33+
+ "INSERT INTO Logs(id, num) VALUES (7, 2); ")
34+
.build();
35+
36+
@Test
37+
public void testScript() throws SQLException, FileNotFoundException {
38+
try (final Connection connection =
39+
DriverManager.getConnection(embeddedDatabaseRule.getConnectionJdbcUrl())) {
40+
try (final Statement statement = connection.createStatement();
41+
final ResultSet resultSet =
42+
statement.executeQuery(
43+
new BufferedReader(
44+
new FileReader(
45+
"src/main/java/g0101_0200/"
46+
+ "s0180_consecutive_numbers/script.sql"))
47+
.lines()
48+
.collect(Collectors.joining("\n"))
49+
.replaceAll("#.*?\\r?\\n", ""))) {
50+
assertThat(resultSet.next(), equalTo(true));
51+
assertThat(resultSet.getInt(1), equalTo(1));
52+
assertThat(resultSet.next(), equalTo(false));
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)