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.TelephoneCrud;
import database.dataset.Telephone;

public class testCrudTelephone extends testCrud<Telephone> {

	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new TelephoneCrud(getConnection()));
		setDataset(new Telephone(magicID, magicID, "819-416-2222", magicID));
		setDataset2(new Telephone(magicID+1, magicID, "819-416-2222", magicID));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKtypeNonValide() throws SQLException {  
		getCrud().create(new Telephone(magicID-1, magicID+1, "819-416-2222", magicID));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKclientNonValide() throws SQLException {  
		getCrud().create(new Telephone(magicID-1, magicID, "819-416-2222", magicID+1));
	}

	@Test
	public void testDeReadByFkclient() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Telephone a :((TelephoneCrud)getCrud()).readByFk_client(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fkclient not work",ok);
	}

	@Test
	public void testDeReadByFktelephone() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Telephone a :((TelephoneCrud)getCrud()).readByFk_telephone_type(magicID))
			ok |= a.equals(getDataset());
		assertTrue("By Fktelephone not work",ok);
	}
	
	@Test
	public void testDeReadAllNotLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Telephone a : ((TelephoneCrud)getCrud()).readAllLike("", "450", "", ""))
			ok |= a.equals(getDataset());
		assertFalse("should fail",ok);
	}

	@Test
	public void testDeReadAllLike() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Telephone a : ((TelephoneCrud)getCrud()).readAllLike("", "819", "", ""))
			ok |= a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
}