package testUnitaire.database.crud.alltest;

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

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;

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

import testUnitaire.database.crud.testCrud;
import database.crud.FactureCrud;
import database.dataset.Facture;

public class testCrudFacture extends testCrud<Facture> {

	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new FactureCrud(getConnection()));
		setDataset(new Facture(magicID, magicID, magicID, "commentaire de qualiter", magicID, Date.valueOf("2012-05-05"), Time.valueOf("00:10:00"), new BigDecimal("1.12")));
		setDataset2(new Facture(magicID+1, magicID, magicID, "commentaire de qualiter", magicID, Date.valueOf("2012-05-05"), Time.valueOf("00:10:00"), new BigDecimal("1.12")));
	}

	@Test(expected = SQLException.class)  
	public void insertionDuneFKclientNonValide() throws SQLException {  
		getCrud().create(new Facture(magicID-1, magicID+1, magicID, "commentaire de qualiter", magicID, Date.valueOf("2012-05-05"), Time.valueOf("00:10:00"), new BigDecimal(1.12)));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKfermetureNonValide() throws SQLException {  
		getCrud().create(new Facture(magicID-1, magicID, magicID+1, "commentaire de qualiter", magicID, Date.valueOf("2012-05-05"), Time.valueOf("00:10:00"), new BigDecimal(1.12)));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKtaxeNonValide() throws SQLException {  
		getCrud().create(new Facture(magicID-1, magicID, magicID, "commentaire de qualiter", magicID+1, Date.valueOf("2012-05-05"), Time.valueOf("00:10:00"), new BigDecimal(1.12)));
	}
	
	@Test
	public void testDeReadByFkclient() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a :((FactureCrud)getCrud()).readByFk_client(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fk client not work",ok);
	}
	
	@Test
	public void testDeReadByFkfermeture() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a :((FactureCrud)getCrud()).readByFk_fermeture(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fkfermeture not work",ok);
	}
	
	@Test
	public void testDeReadByFktaxe() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a :((FactureCrud)getCrud()).readByFk_taxe(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fkemploye not work",ok);
	}

	@Test
	public void testDeReadAllLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a : ((FactureCrud)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 (Facture a : ((FactureCrud)getCrud()).readAllLike("", "", "", "", "", "", "", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}

	@Test
	public void testDeReadAllLike3() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a : ((FactureCrud)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 (Facture a : ((FactureCrud)getCrud()).readAllLike("", "", "", "", "", "qualiter", "", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}

	@Test
	public void testDeReadAllLike5() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a : ((FactureCrud)getCrud()).readAllLike("", "", "", "", "", "", "2012-05-05", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike6() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a : ((FactureCrud)getCrud()).readAllLike("", "", "", "", "", "", "", "00:10:00"))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllNotLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Facture a : ((FactureCrud)getCrud()).readAllLike("", "", "", "", "", "", "2014", "00:10:00"))
			ok |= a.equals(getDataset());
		assertFalse("should fail",ok);
	}
}
