From 61f09125b4aff6cb6e1d842d3492de2cc8c8c995 Mon Sep 17 00:00:00 2001 From: "maxence.antoine.etu" <maxence.antoine.etu@univ-lille.fr> Date: Thu, 24 Oct 2024 20:41:25 +0200 Subject: [PATCH] ajout des tests pour ClassificationModel --- .../model/ClassificationModelTest.java | 66 +++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/src/test/java/fr/univlille/sae/classification/model/ClassificationModelTest.java b/src/test/java/fr/univlille/sae/classification/model/ClassificationModelTest.java index e8ed459..2b82654 100644 --- a/src/test/java/fr/univlille/sae/classification/model/ClassificationModelTest.java +++ b/src/test/java/fr/univlille/sae/classification/model/ClassificationModelTest.java @@ -1,24 +1,80 @@ package fr.univlille.sae.classification.model; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; class ClassificationModelTest { + private ClassificationModel model; + + @BeforeEach + void setUp() { + model = ClassificationModel.getClassificationModel(); + } + + @Test + void testSingletonInstance() { + ClassificationModel anotherModel = ClassificationModel.getClassificationModel(); + assertSame(model, anotherModel); + } + @Test - void getClassificationModel() { + void testAjouterDonnee() { + double[] coords = {5.1, 3.5, 1.4, 0.2}; + model.ajouterDonnee(coords); + + List<LoadableData> dataToClass = model.getDataToClass(); + assertEquals(1, dataToClass.size()); + assertNotNull(dataToClass.get(0).getClassification()); } @Test - void getDatas() { + void testAjouterDonneeInsuffisante() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + model.ajouterDonnee(5.1); + }); + assertEquals(null, exception.getMessage()); } @Test - void getDataToClass() { + void testLoadData() throws IOException { + File csvTemp = File.createTempFile("test", ".csv"); + String csvTest = "sepal_length,sepal_width,petal_length,petal_width,class\n" + + "5.1,3.5,1.4,0.2,Iris-setosa\n" + + "4.9,3.0,1.4,0.2,Iris-setosa\n"; + Files.write(Paths.get(csvTemp.getAbsolutePath()), csvTest.getBytes()); + + model.loadData(csvTemp); + + List<LoadableData> datas = model.getDatas(); + assertEquals(2, datas.size()); + + csvTemp.delete(); + } + + @Test + void testClassifierDonnees() { + double[] coords1 = {5.1, 3.5, 1.4, 0.2}; + double[] coords2 = {4.9, 3.0, 1.4, 0.2}; + model.ajouterDonnee(coords1); + model.ajouterDonnee(coords2); + + model.classifierDonnees(); + + assertEquals(0, model.getDataToClass().size()); } @Test - void getType() { + void testSetType() { + model.setType(DataType.IRIS); + assertEquals(DataType.IRIS, model.getType()); } -} \ No newline at end of file +} -- GitLab