SQL restart on test

This commit is contained in:
jeffcheasey88 2023-09-04 15:58:38 +02:00
parent ebabcf4d2c
commit e3b18ace64
6 changed files with 151 additions and 7 deletions

View file

@ -1 +1,126 @@
DROP TABLE IF EXISTS `containsTags`;
DROP TABLE IF EXISTS `tags`;
DROP TABLE IF EXISTS `containsBadges`;
DROP TABLE IF EXISTS `badges`;
DROP TABLE IF EXISTS `containsGroups`;
DROP TABLE IF EXISTS `nextPart`;
DROP TABLE IF EXISTS `groups`;
DROP TABLE IF EXISTS `completions`;
DROP TABLE IF EXISTS `players`;
DROP TABLE IF EXISTS `puzzles`;
DROP TABLE IF EXISTS `chapters`;
CREATE TABLE `players` (
`id_player` int(11) NOT NULL AUTO_INCREMENT,
`pseudo` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`passwd` varchar(150) NOT NULL,
`firstname` varchar(100) NOT NULL,
`lastname` varchar(100) NOT NULL,
`description` varchar(200) DEFAULT NULL,
`avatar` blob DEFAULT NULL,
PRIMARY KEY (`id_player`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE badges (
id_badge int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
logo mediumblob DEFAULT NULL,
level int(11) DEFAULT 1,
PRIMARY KEY (id_badge)
);
CREATE TABLE `chapters` (
`id_chapter` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) NOT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
PRIMARY KEY (`id_chapter`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `puzzles` (
`id_puzzle` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) NOT NULL,
`content` text NOT NULL,
`soluce` blob NOT NULL,
`verify` text DEFAULT NULL,
`score_max` int(11) NOT NULL,
`fk_chapter` int(11) NOT NULL,
PRIMARY KEY (`id_puzzle`),
KEY `fk_chapter` (`fk_chapter`),
CONSTRAINT `puzzles_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `groups` (
`id_group` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) DEFAULT NULL,
`fk_chapter` int(11) DEFAULT NULL,
`fk_puzzle` int(11) DEFAULT NULL,
PRIMARY KEY (`id_group`),
KEY `fk_chapter` (`fk_chapter`),
KEY `fk_puzzle` (`fk_puzzle`),
CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`),
CONSTRAINT `groups_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `nextPart` (
`origin` int(11) NOT NULL,
`next` int(11) NOT NULL,
PRIMARY KEY (`origin`,`next`),
KEY `next` (`next`),
CONSTRAINT `nextPart_ibfk_1` FOREIGN KEY (`origin`) REFERENCES `puzzles` (`id_puzzle`),
CONSTRAINT `nextPart_ibfk_2` FOREIGN KEY (`next`) REFERENCES `puzzles` (`id_puzzle`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `completions` (
`id_completion` int(11) NOT NULL AUTO_INCREMENT,
`fk_puzzle` int(11) NOT NULL,
`fk_player` int(11) NOT NULL,
`tries` int(11) DEFAULT 0,
`code` blob DEFAULT NULL,
`score` int(11) DEFAULT 0,
`fileName` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id_completion`),
KEY `fk_puzzle` (`fk_puzzle`),
KEY `fk_player` (`fk_player`),
CONSTRAINT `completions_ibfk_1` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`),
CONSTRAINT `completions_ibfk_2` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `tags` (
`id_tag` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id_tag`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `containsBadges` (
`fk_player` int(11) NOT NULL,
`fk_badge` int(11) NOT NULL,
PRIMARY KEY (`fk_player`,`fk_badge`),
KEY `fk_badge` (`fk_badge`),
CONSTRAINT `containsBadges_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
CONSTRAINT `containsBadges_ibfk_2` FOREIGN KEY (`fk_badge`) REFERENCES `badges` (`id_badge`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `containsGroups` (
`fk_player` int(11) NOT NULL,
`fk_group` int(11) NOT NULL,
PRIMARY KEY (`fk_player`,`fk_group`),
KEY `fk_group` (`fk_group`),
CONSTRAINT `containsGroups_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
CONSTRAINT `containsGroups_ibfk_2` FOREIGN KEY (`fk_group`) REFERENCES `groups` (`id_group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `containsTags` (
`fk_tag` int(11) NOT NULL,
`fk_puzzle` int(11) NOT NULL,
PRIMARY KEY (`fk_tag`,`fk_puzzle`),
KEY `fk_puzzle` (`fk_puzzle`),
CONSTRAINT `containsTags_ibfk_1` FOREIGN KEY (`fk_tag`) REFERENCES `tags` (`id_tag`),
CONSTRAINT `containsTags_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

View file

@ -34,7 +34,7 @@ public class DatabaseRepository {
}
private void ensureConnection() throws SQLException {
if (con == null || (!con.isValid(5))) {
if (con == null || (!con.isValid(5))){
this.con = DriverManager.getConnection(
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
config.getDbUser(), config.getDbPassword());

View file

@ -6,6 +6,7 @@ import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -21,7 +22,7 @@ public class TestDatabaseRepository extends DatabaseRepository{
schem = "";
BufferedReader reader = new BufferedReader(new FileReader(databaseSchem));
String line;
while((line = reader.readLine()) != null) schem+=line+"\n";
while((line = reader.readLine()) != null) schem+=line;
reader.close();
}catch(Exception e){
e.printStackTrace();
@ -37,15 +38,26 @@ public class TestDatabaseRepository extends DatabaseRepository{
Field field = DatabaseRepository.class.getDeclaredField("con");
field.setAccessible(true);
this.con = (Connection) field.get(this);
System.out.println("connected !");
}catch(Exception e){
e.getCause().printStackTrace();
}
}
public void close(){
try {
this.con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getCause());
}
}
public void reset(){
try{
this.con.prepareStatement(schem).execute();
String[] split = schem.split(";");
for(String statment : split){
this.con.prepareStatement(statment).execute();
}
}catch(Exception e){
e.printStackTrace();
}

View file

@ -12,9 +12,11 @@ public class BaseUserStoriesTest {
public BaseUserStoriesTest(){}
public void init(){
public void init() throws Exception{
this.config = new Configuration("config-test.txt");
this.repo = new TestDatabaseRepository(config, new File("database-schem.sql"));
this.config.load();
}
public Configuration getConfig(){

View file

@ -16,7 +16,9 @@ public class LoginTests extends BaseUserStoriesTest{
private WebClient client;
@BeforeEach
public void init(){
public void init() throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
super.init();
getRepository().init();
getRepository().reset();

View file

@ -17,7 +17,9 @@ public class RegisterTests extends BaseUserStoriesTest{
private WebClient client;
@BeforeEach
public void init(){
public void init() throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
super.init();
getRepository().init();
getRepository().reset();
@ -39,6 +41,7 @@ public class RegisterTests extends BaseUserStoriesTest{
@AfterEach
public void stop(){
server.interrupt();
getRepository().close();
}
@Test