Skip to content

Commit 7a025b5

Browse files
committed
Initial scaffolding and work on db unit testing
1 parent def39d1 commit 7a025b5

File tree

104 files changed

+1653
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1653
-0
lines changed

01-creating-databases/.idea/01-creating-databases.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/.idea/workspace.xml

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

01-creating-databases/creating-databases/.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/creating-databases/.idea/encodings.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/creating-databases/.idea/misc.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

01-creating-databases/creating-databases/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>creating-databases</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>23</maven.compiler.source>
13+
<maven.compiler.target>23</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter</artifactId>
20+
<version>RELEASE</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>ch.vorburger.mariaDB4j</groupId>
25+
<artifactId>mariaDB4j</artifactId>
26+
<version>3.1.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.mariadb.jdbc</groupId>
30+
<artifactId>mariadb-java-client</artifactId>
31+
<version>3.4.1</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.mybatis</groupId>
35+
<artifactId>mybatis</artifactId>
36+
<version>3.5.6</version>
37+
</dependency>
38+
39+
</dependencies>
40+
41+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
drop table if exists users;
2+
create table users (
3+
id integer primary key auto_increment,
4+
username varchar(255) not null,
5+
password varchar(255) not null
6+
);
7+
8+
drop table if exists topics;
9+
create table topics (
10+
id integer primary key auto_increment,
11+
subject varchar(255) not null,
12+
body text not null,
13+
author_id integer not null,
14+
foreign key(author_id) references users(id)
15+
);
16+
17+
drop table if exists comments;
18+
create table comments (
19+
id integer primary key auto_increment,
20+
body text not null,
21+
author_id integer not null,
22+
topic_id integer not null,
23+
foreign key(topic_id) references topics(id),
24+
foreign key(author_id) references users(id)
25+
);
26+
27+
insert into users (username, password) values ('bob', 'password');
28+
insert into users (username, password) values ('alice', 'password');
29+
insert into users (username, password) values ('charlie', 'password');
30+
31+
insert into topics (subject, body, author_id) values ('First topic', 'This is the first topic', 1);
32+
insert into topics (subject, body, author_id) values ('Second topic', 'This is the second topic', 2);
33+
insert into topics (subject, body, author_id) values ('Third topic', 'This is the third topic', 3);
34+
35+
insert into comments (body, author_id, topic_id) values ('This is the first comment', 1, 1);
36+
insert into comments (body, author_id, topic_id) values ('This is the second comment', 2, 1);
37+
insert into comments (body, author_id, topic_id) values ('This is the third comment', 1, 2);
38+
insert into comments (body, author_id, topic_id) values ('This is the fourth comment', 2, 2);
39+
insert into comments (body, author_id, topic_id) values ('This is the fifth comment', 1, 3);
40+
insert into comments (body, author_id, topic_id) values ('This is the sixth comment', 2, 3);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
2+
import ch.vorburger.mariadb4j.DB;
3+
import ch.vorburger.mariadb4j.DBConfigurationBuilder;
4+
import org.apache.ibatis.jdbc.ScriptRunner;
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.ResultSet;
11+
import java.sql.Statement;
12+
13+
public class TestCreateScript {
14+
private static DB db;
15+
private static Connection connection;
16+
17+
@BeforeAll
18+
public static void setup() throws Exception {
19+
DBConfigurationBuilder configBuilder = DBConfigurationBuilder.newBuilder();
20+
configBuilder.setPort(0);
21+
db = DB.newEmbeddedDB(configBuilder.build());
22+
db.start();
23+
24+
connection = DriverManager.getConnection(
25+
"jdbc:mariadb://localhost:" + db.getConfiguration().getPort() + "/test",
26+
"root",
27+
""
28+
);
29+
30+
java.io.Reader reader = new java.io.InputStreamReader(
31+
TestCreateScript.class.getResource("/create-database.sql").openStream()
32+
);
33+
34+
ScriptRunner sr = new ScriptRunner(connection);
35+
sr.setStopOnError(true);
36+
sr.runScript(reader);
37+
}
38+
39+
@AfterAll
40+
public static void tearDown() throws Exception {
41+
connection.close();
42+
db.stop();
43+
}
44+
45+
@Test
46+
public void checkUserColumns() throws Exception {
47+
String[] columns = {"id", "username", "password" };
48+
String[] types = {"int(11)", "varchar(255)", "varchar(255)"};
49+
String[] keys = {"PRI", "", ""};
50+
String[] extra = {"auto_increment", "", ""};
51+
52+
try (Statement statement = connection.createStatement()) {
53+
ResultSet resultSet = statement.executeQuery("SHOW COLUMNS FROM users");
54+
while (resultSet.next()) {
55+
String column = resultSet.getString("Field");
56+
boolean found = false;
57+
for (int i = 0; i < columns.length; i++) {
58+
String c = columns[i];
59+
if (column.equals(c)) {
60+
if (!resultSet.getString("Type").equals(types[i])) {
61+
throw new Exception("Column " + column + " has wrong type - Expected: " + types[i] + " Found: " + resultSet.getString("Type"));
62+
}
63+
if (!resultSet.getString("Key").equals(keys[i])) {
64+
throw new Exception("Column " + column + " has wrong key type - Expected: " + keys[i] + " Found: " + resultSet.getString("Key"));
65+
}
66+
columns[i] = null;
67+
found = true;
68+
break;
69+
}
70+
}
71+
if (!found) {
72+
throw new Exception("Column " + column + " not expected");
73+
}
74+
}
75+
76+
for (String c : columns) {
77+
if (c != null) {
78+
throw new Exception("Column " + c + " not found");
79+
}
80+
}
81+
}
82+
}
83+
84+
@Test
85+
public void topicUserForeignKeyShouldExist() throws Exception {
86+
String query = """
87+
SELECT *
88+
FROM information_schema.KEY_COLUMN_USAGE
89+
WHERE TABLE_NAME = 'topics'
90+
AND COLUMN_NAME = 'author_id'
91+
AND REFERENCED_TABLE_NAME = 'users'
92+
AND REFERENCED_COLUMN_NAME = 'id'
93+
""";
94+
95+
try (Statement statement = connection.createStatement()) {
96+
ResultSet resultSet = statement.executeQuery(query);
97+
if (!resultSet.next()) {
98+
throw new Exception("Foreign key constraint not found");
99+
}
100+
}
101+
}
102+
103+
@Test
104+
public void commentUserForeignKeyShouldExist() throws Exception {
105+
String query = """
106+
SELECT *
107+
FROM information_schema.KEY_COLUMN_USAGE
108+
WHERE TABLE_NAME = 'comments'
109+
AND COLUMN_NAME = 'author_id'
110+
AND REFERENCED_TABLE_NAME = 'users'
111+
AND REFERENCED_COLUMN_NAME = 'id'
112+
""";
113+
114+
try (Statement statement = connection.createStatement()) {
115+
ResultSet resultSet = statement.executeQuery(query);
116+
if (!resultSet.next()) {
117+
throw new Exception("Foreign key constraint not found");
118+
}
119+
}
120+
}
121+
122+
@Test
123+
public void commentTopicForeignKeyShouldExist() throws Exception {
124+
String query = """
125+
SELECT *
126+
FROM information_schema.KEY_COLUMN_USAGE
127+
WHERE TABLE_NAME = 'comments'
128+
AND COLUMN_NAME = 'topic_id'
129+
AND REFERENCED_TABLE_NAME = 'topics'
130+
AND REFERENCED_COLUMN_NAME = 'id'
131+
""";
132+
133+
try (Statement statement = connection.createStatement()) {
134+
ResultSet resultSet = statement.executeQuery(query);
135+
if (!resultSet.next()) {
136+
throw new Exception("Foreign key constraint not found");
137+
}
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)