package testUnitaire.database.crud.alltest;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.Date;
import java.sql.SQLException;

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

import testUnitaire.database.crud.testCrud;
import database.crud.FermetureCrud;
import database.dataset.Fermeture;

public class testCrudFermeture extends testCrud<Fermeture>{
	
	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new FermetureCrud(getConnection()));
		setDataset(new Fermeture(magicID, Date.valueOf("2013-02-02"), magicID, magicID, "cest un bon commentaire"));
		setDataset2(new Fermeture(magicID+1, Date.valueOf("2013-02-02"), magicID, magicID, "cest un bon commentaire"));
	}

	@Test(expected = SQLException.class)  
	public void insertionDuneFKemployeNonValide() throws SQLException {  
		getCrud().create(new Fermeture(magicID-1, Date.valueOf("2013-02-02"), magicID+1, magicID+1, "c'est une mauvais commentaire"));
	}
	
	@Test
	public void testDeReadByFkemploye() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a :((FermetureCrud)getCrud()).readByFk_employe(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fkemploye not work",ok);
	}
	
	@Test
	public void testDeReadAllLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike(""+magicID, "", "", "", "", "", ""))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}

	@Test
	public void testDeReadAllLike2() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike("", "2013-02", "", "", "", "", ""))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike3() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike("", "", "", "", "", "", ""))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike4() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike("", "", "", "", "", "", ""))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike5() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike("", "", "", "", "", "", "commentaire"))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllNotLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Fermeture a : ((FermetureCrud)getCrud()).readAllLike("", "", "", "" , "", "","Bob"))
			ok = a.equals(getDataset());
		assertFalse("should fail",ok);
	}
}
