package testUnitaire.database.crud.alltest;

import static org.junit.Assert.*;

import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

import testUnitaire.database.crud.testCrud;
import tool.regEx.Adresse;
import database.crud.AdresseCrud;

public class testCrudAdresse extends testCrud<Adresse> {

	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new AdresseCrud(getConnection()));
		setDataset(new Adresse(magicID, "2020 du finfin", magicID, "hohoho"));
		setDataset2(new Adresse(magicID+1, "2020 du finfin", magicID, "hohoho"));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKvilleNonValide() throws SQLException {  
		getCrud().create(new Adresse(magicID-1, "2020 du finfin", magicID+1, "hohoho"));
	}
	
	@Test
	public void testDeReadByFkVille() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Adresse a :((AdresseCrud)getCrud()).readByFk_ville(magicID)){
			ok |= a.equals(getDataset());
			System.out.println(a.toString());
			System.out.println(getDataset().toString());
		}
		assertTrue("ByFkVille not work",ok);
	}
	
	@Test
	public void testDeReadAllLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Adresse a : ((AdresseCrud)getCrud()).readAllLike("finfin", "", "","",""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike2() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Adresse a : ((AdresseCrud)getCrud()).readAllLike("", "hohoh", "","",""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike3() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Adresse a : ((AdresseCrud)getCrud()).readAllLike("", "", "simcity","",""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllNotLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Adresse a : ((AdresseCrud)getCrud()).readAllLike("chien", "", "","",""))
			ok |= a.equals(getDataset());
		assertFalse("read all not like not work",ok);
	}
}
