From 641d298bd314742ba0ecb0249861127da0f4b56f Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Wed, 29 Apr 2020 19:14:18 +0200 Subject: [PATCH] Database script continue + tests Junit5 #4 --- src/database/QueryGenerator.java | 9 +++++++ src/database/Record.java | 1 - src/database/Table.java | 8 ++++++ tests/database/TestDatabse.java | 43 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/database/TestDatabse.java diff --git a/src/database/QueryGenerator.java b/src/database/QueryGenerator.java index 3ec85ee..d91faae 100644 --- a/src/database/QueryGenerator.java +++ b/src/database/QueryGenerator.java @@ -18,4 +18,13 @@ public class QueryGenerator { return "public." + '"' + s + '"'; } + public static String getIdWithNameFrom(String table, String name) { + return "SELECT 'id' FROM " + getTable(table) + "WHERE nom ='" + name + "'"; + } + + public static String getNameWithIdFrom(String table, int d) { + return "SELECT name FROM " + getTable(table) + "WHERE id =" + d; + } + + } diff --git a/src/database/Record.java b/src/database/Record.java index f95f1b9..960f90f 100644 --- a/src/database/Record.java +++ b/src/database/Record.java @@ -39,6 +39,5 @@ public class Record { public String toString() { return String.format("%-20.30s %-30.30s %-20.30s%n", this.getId(), this.getNom(), this.getImg()); } - } diff --git a/src/database/Table.java b/src/database/Table.java index 2545740..8cf0936 100644 --- a/src/database/Table.java +++ b/src/database/Table.java @@ -84,6 +84,14 @@ public class Table { return " " + this.getList(); } + public boolean isEmpty() { + if(list.isEmpty()) { + return true; + } else { + return false; + } + } + diff --git a/tests/database/TestDatabse.java b/tests/database/TestDatabse.java new file mode 100644 index 0000000..6b54c00 --- /dev/null +++ b/tests/database/TestDatabse.java @@ -0,0 +1,43 @@ +package database; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestDatabse { + + Table t; + + @BeforeEach + private void initialize() { + System.out.println("=====Initialisation du test====="); + t = new Table("Table de Test"); + } + + @AfterEach + private void free() { + System.out.println("=====Fin du test=====\n\n\n"); + } + + @Test + private void testDBConnexion() { + try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ShadowHunterDatabase", "shManager", "shadowhunter1234")) { //notre utilisateur que l'on utilisera (: + + System.out.println("Connected to PostgreSQL database!"); + } catch (SQLException e) { + System.out.println("Connection failure."); + e.printStackTrace(); + } + } + + @Test + private void getEntireTableCartesLumiere() { + t.remplirTableAllFrom("CartesLumiere"); + Assert.assertFalse(t.isEmpty()); + } +}