Skip to content

Commit d133d2c

Browse files
authored
Added test 1393.
1 parent 43c7586 commit d133d2c

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# Write your MySQL query statement below
22
# #Medium #LeetCode_Curated_SQL_70 #Database #SQL_I_Day_9_Control_of_Flow
3-
# #2022_05_24_Time_457_ms_(79.39%)_Space_0B_(100.00%)
4-
SELECT
5-
stock_name,
6-
SUM(IF (operation = 1, price, -1 * price)) AS capital_gain_loss
7-
FROM
8-
Stocks
9-
GROUP BY
10-
stock_name
11-
ORDER BY
12-
operation_day asc;
3+
# #2022_07_26_Time_428_ms_(92.01%)_Space_0B_(100.00%)
4+
SELECT stock_name, SUM(CASE WHEN operation='Sell' THEN price ELSE -price END) AS capital_gain_loss
5+
FROM Stocks GROUP BY stock_name;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g1301_1400.s1393_capital_gainloss;
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 Stocks(stock_name VARCHAR, operation VARCHAR,"
24+
+ " operation_day INTEGER, price INTEGER); "
25+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
26+
+ " VALUES ('Leetcode', 'Buy', 1, 1000); "
27+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
28+
+ " VALUES ('Corona Masks', 'Buy', 2, 10); "
29+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
30+
+ " VALUES ('Leetcode', 'Sell', 5, 9000); "
31+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
32+
+ " VALUES ('Handbags', 'Buy', 17, 30000); "
33+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
34+
+ " VALUES ('Corona Masks', 'Sell', 3, 1010); "
35+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
36+
+ " VALUES ('Corona Masks', 'Buy', 4, 1000); "
37+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
38+
+ " VALUES ('Corona Masks', 'Sell', 5, 500); "
39+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
40+
+ " VALUES ('Corona Masks', 'Buy', 6, 1000); "
41+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
42+
+ " VALUES ('Handbags', 'Sell', 29, 7000); "
43+
+ "INSERT INTO Stocks(stock_name, operation, operation_day, price)"
44+
+ " VALUES ('Corona Masks', 'Sell', 10, 10000); ")
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/g1301_1400/s1393_"
56+
+ "capital_gainloss"
57+
+ "/script.sql"))
58+
.lines()
59+
.collect(Collectors.joining("\n"))
60+
.replaceAll("#.*?\\r?\\n", ""))) {
61+
assertThat(resultSet.next(), equalTo(true));
62+
assertThat(resultSet.getNString(1), equalTo("Corona Masks"));
63+
assertThat(resultSet.getInt(2), equalTo(9500));
64+
assertThat(resultSet.next(), equalTo(true));
65+
assertThat(resultSet.getNString(1), equalTo("Handbags"));
66+
assertThat(resultSet.getInt(2), equalTo(-23000));
67+
assertThat(resultSet.next(), equalTo(true));
68+
assertThat(resultSet.getNString(1), equalTo("Leetcode"));
69+
assertThat(resultSet.getInt(2), equalTo(8000));
70+
assertThat(resultSet.next(), equalTo(false));
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)