Skip to content

Commit 334f020

Browse files
authored
Added tests for tasks 1587, 1667, 1693, 1729, 1741.
1 parent 9682f10 commit 334f020

File tree

6 files changed

+330
-1
lines changed

6 files changed

+330
-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
# #Easy #Database #SQL_I_Day_8_Function #2022_05_03_Time_414_ms_(93.27%)_Space_0B_(100.00%)
3-
SELECT event_day AS day, emp_id, SUM(out_time) - SUM(in_time) AS total_time
3+
SELECT event_day AS "day", emp_id, SUM(out_time) - SUM(in_time) AS total_time
44
FROM Employees
55
GROUP BY event_day, emp_id
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g1501_1600.s1587_bank_account_summary_ii;
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.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Users(account INTEGER PRIMARY KEY, name VARCHAR); "
24+
+ "INSERT INTO Users(account, name) VALUES (900001, 'Alice'); "
25+
+ "INSERT INTO Users(account, name) VALUES (900002, 'Bob'); "
26+
+ "INSERT INTO Users(account, name) VALUES (900003, 'Charlie'); "
27+
+ "CREATE TABLE Transactions(trans_id INTEGER PRIMARY KEY,"
28+
+ " account INTEGER, amount INTEGER, transacted_on DATE); "
29+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
30+
+ " VALUES (1, 900001, 7000, '2020-08-01'); "
31+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
32+
+ " VALUES (2, 900001, 7000, '2020-09-01'); "
33+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
34+
+ " VALUES (3, 900001, -3000, '2020-09-02'); "
35+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
36+
+ " VALUES (4, 900002, 1000, '2020-09-12'); "
37+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
38+
+ " VALUES (5, 900003, 6000, '2020-08-07'); "
39+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
40+
+ " VALUES (6, 900003, 6000, '2020-09-07'); "
41+
+ "INSERT INTO Transactions(trans_id, account, amount, transacted_on)"
42+
+ " VALUES (7, 900003, -4000, '2020-09-11'); ")
43+
class MysqlTest {
44+
@Test
45+
void testScript(@EmbeddedDatabase DataSource dataSource)
46+
throws SQLException, FileNotFoundException {
47+
try (final Connection connection = dataSource.getConnection()) {
48+
try (final Statement statement = connection.createStatement();
49+
final ResultSet resultSet =
50+
statement.executeQuery(
51+
new BufferedReader(
52+
new FileReader(
53+
"src/main/java/g1501_1600/s1587_bank_"
54+
+ "account_summary_ii/script.sql"))
55+
.lines()
56+
.collect(Collectors.joining("\n"))
57+
.replaceAll("#.*?\\r?\\n", ""))) {
58+
assertThat(resultSet.next(), equalTo(true));
59+
assertThat(resultSet.getNString(1), equalTo("Alice"));
60+
assertThat(resultSet.getInt(2), equalTo(11000));
61+
assertThat(resultSet.next(), equalTo(false));
62+
}
63+
}
64+
}
65+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g1601_1700.s1667_fix_names_in_a_table;
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.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Users(user_id INTEGER PRIMARY KEY, name VARCHAR); "
24+
+ "INSERT INTO Users(user_id, name) VALUES (1, 'aLice'); "
25+
+ "INSERT INTO Users(user_id, name) VALUES (2, 'bOB'); ")
26+
class MysqlTest {
27+
@Test
28+
void testScript(@EmbeddedDatabase DataSource dataSource)
29+
throws SQLException, FileNotFoundException {
30+
try (final Connection connection = dataSource.getConnection()) {
31+
try (final Statement statement = connection.createStatement();
32+
final ResultSet resultSet =
33+
statement.executeQuery(
34+
new BufferedReader(
35+
new FileReader(
36+
"src/main/java/g1601_1700/s1667_fix_"
37+
+ "names_in_a_table/script.sql"))
38+
.lines()
39+
.collect(Collectors.joining("\n"))
40+
.replaceAll("#.*?\\r?\\n", ""))) {
41+
assertThat(resultSet.next(), equalTo(true));
42+
assertThat(resultSet.getNString(2), equalTo("Alice"));
43+
assertThat(resultSet.getInt(1), equalTo(1));
44+
assertThat(resultSet.next(), equalTo(true));
45+
assertThat(resultSet.getNString(2), equalTo("Bob"));
46+
assertThat(resultSet.getInt(1), equalTo(2));
47+
assertThat(resultSet.next(), equalTo(false));
48+
}
49+
}
50+
}
51+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g1601_1700.s1693_daily_leads_and_partners;
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.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE DailySales(date_id DATE, make_name VARCHAR, lead_id INTEGER,"
24+
+ " partner_id INTEGER); "
25+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
26+
+ " VALUES ('2020-12-8', 'toyota', 0, 1); "
27+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
28+
+ " VALUES ('2020-12-8', 'toyota', 1, 0); "
29+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
30+
+ " VALUES ('2020-12-8', 'toyota', 1, 2); "
31+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
32+
+ " VALUES ('2020-12-7', 'toyota', 0, 2); "
33+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
34+
+ " VALUES ('2020-12-7', 'toyota', 0, 1); "
35+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
36+
+ " VALUES ('2020-12-8', 'honda', 1, 2); "
37+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
38+
+ " VALUES ('2020-12-8', 'honda', 2, 1); "
39+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
40+
+ " VALUES ('2020-12-7', 'honda', 0, 1); "
41+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
42+
+ " VALUES ('2020-12-7', 'honda', 1, 2); "
43+
+ "INSERT INTO DailySales(date_id, make_name, lead_id, partner_id) "
44+
+ " VALUES ('2020-12-7', 'honda', 2, 1); ")
45+
class MysqlTest {
46+
@Test
47+
void testScript(@EmbeddedDatabase DataSource dataSource)
48+
throws SQLException, FileNotFoundException {
49+
try (final Connection connection = dataSource.getConnection()) {
50+
try (final Statement statement = connection.createStatement();
51+
final ResultSet resultSet =
52+
statement.executeQuery(
53+
new BufferedReader(
54+
new FileReader(
55+
"src/main/java/g1601_1700/s1693_daily_le"
56+
+ "ads_and_partners/script.sql"))
57+
.lines()
58+
.collect(Collectors.joining("\n"))
59+
.replaceAll("#.*?\\r?\\n", ""))) {
60+
assertThat(resultSet.next(), equalTo(true));
61+
assertThat(resultSet.getNString(1), equalTo("2020-12-07"));
62+
assertThat(resultSet.getNString(2), equalTo("honda"));
63+
assertThat(resultSet.getInt(3), equalTo(3));
64+
assertThat(resultSet.getInt(4), equalTo(2));
65+
assertThat(resultSet.next(), equalTo(true));
66+
assertThat(resultSet.getNString(1), equalTo("2020-12-07"));
67+
assertThat(resultSet.getNString(2), equalTo("toyota"));
68+
assertThat(resultSet.getInt(3), equalTo(1));
69+
assertThat(resultSet.getInt(4), equalTo(2));
70+
assertThat(resultSet.next(), equalTo(true));
71+
assertThat(resultSet.getNString(1), equalTo("2020-12-08"));
72+
assertThat(resultSet.getNString(2), equalTo("honda"));
73+
assertThat(resultSet.getInt(3), equalTo(2));
74+
assertThat(resultSet.getInt(4), equalTo(2));
75+
assertThat(resultSet.next(), equalTo(true));
76+
assertThat(resultSet.getNString(1), equalTo("2020-12-08"));
77+
assertThat(resultSet.getNString(2), equalTo("toyota"));
78+
assertThat(resultSet.getInt(3), equalTo(2));
79+
assertThat(resultSet.getInt(4), equalTo(3));
80+
assertThat(resultSet.next(), equalTo(false));
81+
}
82+
}
83+
}
84+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g1701_1800.s1729_find_followers_count;
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.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Followers(user_id INTEGER, follower_id INTEGER); "
24+
+ "INSERT INTO Followers(user_id, follower_id) "
25+
+ " VALUES (0, 1); "
26+
+ "INSERT INTO Followers(user_id, follower_id) "
27+
+ " VALUES (1, 0); "
28+
+ "INSERT INTO Followers(user_id, follower_id) "
29+
+ " VALUES (2, 0); "
30+
+ "INSERT INTO Followers(user_id, follower_id) "
31+
+ " VALUES (2, 1); ")
32+
class MysqlTest {
33+
@Test
34+
void testScript(@EmbeddedDatabase DataSource dataSource)
35+
throws SQLException, FileNotFoundException {
36+
try (final Connection connection = dataSource.getConnection()) {
37+
try (final Statement statement = connection.createStatement();
38+
final ResultSet resultSet =
39+
statement.executeQuery(
40+
new BufferedReader(
41+
new FileReader(
42+
"src/main/java/g1701_1800/s1729_find_fol"
43+
+ "lowers_count/script.sql"))
44+
.lines()
45+
.collect(Collectors.joining("\n"))
46+
.replaceAll("#.*?\\r?\\n", ""))) {
47+
assertThat(resultSet.next(), equalTo(true));
48+
assertThat(resultSet.getInt(1), equalTo(0));
49+
assertThat(resultSet.getInt(2), equalTo(1));
50+
assertThat(resultSet.next(), equalTo(true));
51+
assertThat(resultSet.getInt(1), equalTo(1));
52+
assertThat(resultSet.getInt(2), equalTo(1));
53+
assertThat(resultSet.next(), equalTo(true));
54+
assertThat(resultSet.getInt(1), equalTo(2));
55+
assertThat(resultSet.getInt(2), equalTo(2));
56+
assertThat(resultSet.next(), equalTo(false));
57+
}
58+
}
59+
}
60+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g1701_1800.s1741_find_total_time_spent_by_each_employee;
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.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Employees(emp_id INTEGER, event_day DATE, in_time INTEGER, out_time INTEGER); "
24+
+ "INSERT INTO Employees(emp_id, event_day, in_time, out_time) "
25+
+ " VALUES (1, '2020-11-28', 4, 32); "
26+
+ "INSERT INTO Employees(emp_id, event_day, in_time, out_time) "
27+
+ " VALUES (1, '2020-11-28', 55, 200); "
28+
+ "INSERT INTO Employees(emp_id, event_day, in_time, out_time) "
29+
+ " VALUES (1, '2020-12-03', 1, 42); "
30+
+ "INSERT INTO Employees(emp_id, event_day, in_time, out_time) "
31+
+ " VALUES (2, '2020-11-28', 3, 33); "
32+
+ "INSERT INTO Employees(emp_id, event_day, in_time, out_time) "
33+
+ " VALUES (2, '2020-12-09', 47, 74); ")
34+
class MysqlTest {
35+
@Test
36+
void testScript(@EmbeddedDatabase DataSource dataSource)
37+
throws SQLException, FileNotFoundException {
38+
try (final Connection connection = dataSource.getConnection()) {
39+
try (final Statement statement = connection.createStatement();
40+
final ResultSet resultSet =
41+
statement.executeQuery(
42+
new BufferedReader(
43+
new FileReader(
44+
"src/main/java/g1701_1800/s1741_find_total_time_"
45+
+ "spent_by_each_employee/script.sql"))
46+
.lines()
47+
.collect(Collectors.joining("\n"))
48+
.replaceAll("#.*?\\r?\\n", ""))) {
49+
assertThat(resultSet.next(), equalTo(true));
50+
assertThat(resultSet.getNString(1), equalTo("2020-11-28"));
51+
assertThat(resultSet.getInt(2), equalTo(1));
52+
assertThat(resultSet.getInt(3), equalTo(173));
53+
assertThat(resultSet.next(), equalTo(true));
54+
assertThat(resultSet.getNString(1), equalTo("2020-11-28"));
55+
assertThat(resultSet.getInt(2), equalTo(2));
56+
assertThat(resultSet.getInt(3), equalTo(30));
57+
assertThat(resultSet.next(), equalTo(true));
58+
assertThat(resultSet.getNString(1), equalTo("2020-12-03"));
59+
assertThat(resultSet.getInt(2), equalTo(1));
60+
assertThat(resultSet.getInt(3), equalTo(41));
61+
assertThat(resultSet.next(), equalTo(true));
62+
assertThat(resultSet.getNString(1), equalTo("2020-12-09"));
63+
assertThat(resultSet.getInt(2), equalTo(2));
64+
assertThat(resultSet.getInt(3), equalTo(27));
65+
assertThat(resultSet.next(), equalTo(false));
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)