Skip to content

Commit 90e5cc8

Browse files
authored
Added tests for tasks 1084, 1141, 1148, 1158, 1179.
1 parent 7bb1b7b commit 90e5cc8

File tree

7 files changed

+397
-14
lines changed

7 files changed

+397
-14
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Write your MySQL query statement below
22
# #Easy #LeetCode_Curated_SQL_70 #Database #SQL_I_Day_7_Function
33
# #2022_05_24_Time_543_ms_(29.24%)_Space_0B_(100.00%)
4-
select activity_date as day, count(distinct user_id) as active_users
4+
select activity_date as "day", count(distinct user_id) as active_users
55
from Activity
66
where activity_date between '2019-06-28' and '2019-07-27'
7-
group by day
7+
group by "day"
88
having count(activity_type) > 0;

src/main/java/g1101_1200/s1179_reformat_department_table/script.sql

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# #Easy #Database #2022_03_09_Time_493_ms_(64.80%)_Space_0B_(100.00%)
33
SELECT
44
id,
5-
SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue,
6-
SUM(CASE WHEN month = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue,
7-
SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue,
8-
SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue,
9-
SUM(CASE WHEN month = 'May' THEN revenue ELSE NULL END) AS May_Revenue,
10-
SUM(CASE WHEN month = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue,
11-
SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue,
12-
SUM(CASE WHEN month = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue,
13-
SUM(CASE WHEN month = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue,
14-
SUM(CASE WHEN month = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue,
15-
SUM(CASE WHEN month = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue,
16-
SUM(CASE WHEN month = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
5+
SUM(CASE WHEN "month" = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue,
6+
SUM(CASE WHEN "month" = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue,
7+
SUM(CASE WHEN "month" = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue,
8+
SUM(CASE WHEN "month" = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue,
9+
SUM(CASE WHEN "month" = 'May' THEN revenue ELSE NULL END) AS May_Revenue,
10+
SUM(CASE WHEN "month" = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue,
11+
SUM(CASE WHEN "month" = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue,
12+
SUM(CASE WHEN "month" = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue,
13+
SUM(CASE WHEN "month" = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue,
14+
SUM(CASE WHEN "month" = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue,
15+
SUM(CASE WHEN "month" = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue,
16+
SUM(CASE WHEN "month" = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
1717
FROM Department
1818
GROUP BY id
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g1001_1100.s1084_sales_analysis_iii;
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 product(product_id INTEGER PRIMARY KEY, product_name VARCHAR(512)"
24+
+ ", unit_price INTEGER); "
25+
+ "INSERT INTO product(product_id, product_name, unit_price) "
26+
+ "VALUES (1, 'S8', 1000); "
27+
+ "INSERT INTO product(product_id, product_name, unit_price) "
28+
+ "VALUES (2, 'G4', 800); "
29+
+ "INSERT INTO product(product_id, product_name, unit_price) "
30+
+ "VALUES (3, 'iPhone', 800); "
31+
+ "CREATE TABLE sales(seller_id INTEGER, product_id INTEGER, buyer_id INTEGER, sale_date DATE"
32+
+ ", quantity INTEGER, price INTEGER); "
33+
+ "INSERT INTO sales(seller_id, product_id, buyer_id, sale_date, quantity, price) "
34+
+ "VALUES (1, 1, 1, '2019-01-21', 2, 2000); "
35+
+ "INSERT INTO sales(seller_id, product_id, buyer_id, sale_date, quantity, price) "
36+
+ "VALUES (1, 2, 2, '2019-02-17', 1, 800); "
37+
+ "INSERT INTO sales(seller_id, product_id, buyer_id, sale_date, quantity, price) "
38+
+ "VALUES (2, 2, 3, '2019-06-021', 1, 800); "
39+
+ "INSERT INTO sales(seller_id, product_id, buyer_id, sale_date, quantity, price) "
40+
+ "VALUES (3, 3, 4, '2019-05-13', 2, 2800); ")
41+
class MysqlTest {
42+
@Test
43+
void testScript(@EmbeddedDatabase DataSource dataSource)
44+
throws SQLException, FileNotFoundException {
45+
try (final Connection connection = dataSource.getConnection()) {
46+
try (final Statement statement = connection.createStatement();
47+
final ResultSet resultSet =
48+
statement.executeQuery(
49+
new BufferedReader(
50+
new FileReader(
51+
"src/main/java/g1001_1100/"
52+
+ "s1084_sales_analysis_iii/script.sql"))
53+
.lines()
54+
.collect(Collectors.joining("\n"))
55+
.replaceAll("#.*?\\r?\\n", ""))) {
56+
assertThat(resultSet.next(), equalTo(true));
57+
assertThat(resultSet.getInt(1), equalTo(1));
58+
assertThat(resultSet.getNString(2), equalTo("S8"));
59+
assertThat(resultSet.next(), equalTo(false));
60+
}
61+
}
62+
}
63+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g1101_1200.s1141_user_activity_for_the_past_30_days_i;
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 activity(user_id INTEGER, session_id INTEGER"
24+
+ ", activity_date DATE, activity_type VARCHAR(512)); "
25+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
26+
+ "VALUES (1, 1, ' 2019-07-20', 'open_session'); "
27+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
28+
+ "VALUES (1, 1, ' 2019-07-20', 'scroll_down'); "
29+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
30+
+ "VALUES (1, 1, ' 2019-07-20', 'end_session'); "
31+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
32+
+ "VALUES (2, 4, ' 2019-07-20', 'open_session'); "
33+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
34+
+ "VALUES (2, 4, ' 2019-07-21', 'send_message'); "
35+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
36+
+ "VALUES (2, 4, ' 2019-07-21', 'end_session'); "
37+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
38+
+ "VALUES (3, 2, ' 2019-07-21', 'open_session'); "
39+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
40+
+ "VALUES (3, 2, ' 2019-07-21', 'send_message'); "
41+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
42+
+ "VALUES (3, 2, ' 2019-07-21', 'end_session'); "
43+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
44+
+ "VALUES (4, 3, ' 2019-06-25', 'open_session'); "
45+
+ "INSERT INTO activity(user_id, session_id, activity_date, activity_type) "
46+
+ "VALUES (4, 3, ' 2019-06-25', 'end_session'); ")
47+
class MysqlTest {
48+
@Test
49+
void testScript(@EmbeddedDatabase DataSource dataSource)
50+
throws SQLException, FileNotFoundException {
51+
try (final Connection connection = dataSource.getConnection()) {
52+
try (final Statement statement = connection.createStatement();
53+
final ResultSet resultSet =
54+
statement.executeQuery(
55+
new BufferedReader(
56+
new FileReader(
57+
"src/main/java/g1101_1200/s1141_user_activity_"
58+
+ "for_the_past_30_days_i/script.sql"))
59+
.lines()
60+
.collect(Collectors.joining("\n"))
61+
.replaceAll("#.*?\\r?\\n", ""))) {
62+
assertThat(resultSet.next(), equalTo(true));
63+
assertThat(resultSet.getNString(1), equalTo("2019-07-20"));
64+
assertThat(resultSet.getInt(2), equalTo(2));
65+
assertThat(resultSet.next(), equalTo(true));
66+
assertThat(resultSet.getNString(1), equalTo("2019-07-21"));
67+
assertThat(resultSet.getInt(2), equalTo(2));
68+
assertThat(resultSet.next(), equalTo(false));
69+
}
70+
}
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g1101_1200.s1148_article_views_i;
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 views(article_id INTEGER, author_id INTEGER"
24+
+ ", viewer_id INTEGER, view_date DATE); "
25+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
26+
+ "VALUES (1, 3, 5, '2019-08-01'); "
27+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
28+
+ "VALUES (1, 3, 6, '2019-08-02'); "
29+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
30+
+ "VALUES (2, 7, 7, '2019-08-01'); "
31+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
32+
+ "VALUES (2, 7, 6, '2019-08-02'); "
33+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
34+
+ "VALUES (4, 7, 1, '2019-07-22'); "
35+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
36+
+ "VALUES (3, 4, 4, '2019-07-21'); "
37+
+ "INSERT INTO views(article_id, author_id, viewer_id, view_date) "
38+
+ "VALUES (3, 4, 4, '2019-07-21'); ")
39+
class MysqlTest {
40+
@Test
41+
void testScript(@EmbeddedDatabase DataSource dataSource)
42+
throws SQLException, FileNotFoundException {
43+
try (final Connection connection = dataSource.getConnection()) {
44+
try (final Statement statement = connection.createStatement();
45+
final ResultSet resultSet =
46+
statement.executeQuery(
47+
new BufferedReader(
48+
new FileReader(
49+
"src/main/java/g1101_1200/s1148"
50+
+ "_article_views_i/script.sql"))
51+
.lines()
52+
.collect(Collectors.joining("\n"))
53+
.replaceAll("#.*?\\r?\\n", ""))) {
54+
assertThat(resultSet.next(), equalTo(true));
55+
assertThat(resultSet.getInt(1), equalTo(4));
56+
assertThat(resultSet.next(), equalTo(true));
57+
assertThat(resultSet.getInt(1), equalTo(7));
58+
assertThat(resultSet.next(), equalTo(false));
59+
}
60+
}
61+
}
62+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package g1101_1200.s1158_market_analysis_i;
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, join_date DATE, favorite_brand VARCHAR(512)); "
24+
+ "INSERT INTO users(user_id, join_date, favorite_brand) "
25+
+ "VALUES (1, '2018-01-01', 'Lenovo'); "
26+
+ "INSERT INTO users(user_id, join_date, favorite_brand) "
27+
+ "VALUES (2, '2018-02-09', 'Samsung'); "
28+
+ "INSERT INTO users(user_id, join_date, favorite_brand) "
29+
+ "VALUES (3, '2018-01-19', 'LG'); "
30+
+ "INSERT INTO users(user_id, join_date, favorite_brand) "
31+
+ "VALUES (4, '2018-05-21', 'HP'); "
32+
+ "CREATE TABLE orders(order_id INTEGER, order_date DATE, item_id INTEGER,"
33+
+ " buyer_id INTEGER, seller_id INTEGER); "
34+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
35+
+ "VALUES (1, '2019-08-01', 4, 1, 2); "
36+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
37+
+ "VALUES (2, '2018-08-02', 2, 1, 3); "
38+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
39+
+ "VALUES (3, '2019-08-03', 3, 2, 3); "
40+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
41+
+ "VALUES (4, '2018-08-04', 1, 4, 2); "
42+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
43+
+ "VALUES (5, '2018-08-04', 1, 3, 4); "
44+
+ "INSERT INTO orders(order_id, order_date, item_id, buyer_id, seller_id) "
45+
+ "VALUES (6, '2019-08-05', 2, 2, 4); "
46+
+ "CREATE TABLE items(item_id INTEGER, item_brand VARCHAR(512)); "
47+
+ "INSERT INTO items(item_id, item_brand) "
48+
+ "VALUES (1, 'Samsung'); "
49+
+ "INSERT INTO items(item_id, item_brand) "
50+
+ "VALUES (2, 'Lenovo'); "
51+
+ "INSERT INTO items(item_id, item_brand) "
52+
+ "VALUES (3, 'LG'); "
53+
+ "INSERT INTO items(item_id, item_brand) "
54+
+ "VALUES (4, 'HP'); ")
55+
class MysqlTest {
56+
@Test
57+
void testScript(@EmbeddedDatabase DataSource dataSource)
58+
throws SQLException, FileNotFoundException {
59+
try (final Connection connection = dataSource.getConnection()) {
60+
try (final Statement statement = connection.createStatement();
61+
final ResultSet resultSet =
62+
statement.executeQuery(
63+
new BufferedReader(
64+
new FileReader(
65+
"src/main/java/g1101_1200/s1158_"
66+
+ "market_analysis_i/script.sql"))
67+
.lines()
68+
.collect(Collectors.joining("\n"))
69+
.replaceAll("#.*?\\r?\\n", ""))) {
70+
assertThat(resultSet.next(), equalTo(true));
71+
assertThat(resultSet.getInt(1), equalTo(1));
72+
assertThat(resultSet.getNString(2), equalTo("2018-01-01"));
73+
assertThat(resultSet.getInt(3), equalTo(1));
74+
assertThat(resultSet.next(), equalTo(true));
75+
assertThat(resultSet.getInt(1), equalTo(2));
76+
assertThat(resultSet.getNString(2), equalTo("2018-02-09"));
77+
assertThat(resultSet.getInt(3), equalTo(2));
78+
assertThat(resultSet.next(), equalTo(true));
79+
assertThat(resultSet.getInt(1), equalTo(3));
80+
assertThat(resultSet.getNString(2), equalTo("2018-01-19"));
81+
assertThat(resultSet.getInt(3), equalTo(0));
82+
assertThat(resultSet.next(), equalTo(true));
83+
assertThat(resultSet.getInt(1), equalTo(4));
84+
assertThat(resultSet.getNString(2), equalTo("2018-05-21"));
85+
assertThat(resultSet.getInt(3), equalTo(0));
86+
assertThat(resultSet.next(), equalTo(false));
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)