package testUnitaire.database.crud.alltest;

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

import java.sql.SQLException;

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

import testUnitaire.database.crud.testCrud;
import database.crud.ClientCrud;
import database.dataset.Client;

public class testCrudClient extends testCrud<Client> {

	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new ClientCrud(getConnection()));
		setDataset(new Client(magicID, "Bob", "Pitov", magicID, "il est riche"));
		setDataset2(new Client(magicID+1, "Bob", "Pitov", magicID, "il est riche"));
	}

	@Test(expected = SQLException.class)  
	public void insertionDuneFKadresseNonValide() throws SQLException {  
		getCrud().create(new Client(magicID-1, "BOB", "PITOV", magicID+1, "IL EST RICHE"));
	}
	
	@Test
	public void testDeReadByFkAdresse() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a :((ClientCrud)getCrud()).readByFk_adresse(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By adresse_fk_id not work",ok);
	}
	
	@Test
	public void testDeReadAllLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a : ((ClientCrud)getCrud()).readAllLike("BOB", "", "", "", "", "", "", "", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike2() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a : ((ClientCrud)getCrud()).readAllLike("", "PITOV", "", "", "", "", "", "", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike3() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a : ((ClientCrud)getCrud()).readAllLike("", "", "", "", "", "", "", "RICHE", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllLike4() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a : ((ClientCrud)getCrud()).readAllLike("", "", "", "", "", "", "", "", "819-819-8199"))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void testDeReadAllNotLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Client a : ((ClientCrud)getCrud()).readAllLike("MAURICE", "", "", "", "", "", "", "", ""))
			ok |= a.equals(getDataset());
		assertFalse("read all not like not work",ok);
	}
}
