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.ReservationCrudGeneral;
import database.dataset.Reservation;

public class testCrudReservation extends testCrud<Reservation> {
  
	@Override
	@Before
	public void init() throws SQLException {
		setCrud(new ReservationCrudGeneral(getConnection()));
		setDataset(new Reservation(magicID, magicID, Date.valueOf("2012-05-05"), Date.valueOf("2012-05-07"), "un commentaire de qualiter", magicID));
		setDataset2(new Reservation(magicID+2, magicID, Date.valueOf("2012-05-05"), Date.valueOf("2012-05-07"), "un commentaire de qualiter", magicID));
	}
	
	@Test(expected = SQLException.class)  
	public void insertionDuneFKstatusNonValide() throws SQLException {  
		getCrud().create(new Reservation(magicID-1, 
				                         magicID, 
				                         Date.valueOf("2012-05-05"), 
				                         Date.valueOf("2012-05-05"), 
				                         "un commentaire de qualiter", 
				                         magicID+1));
	}

	@Test
	public void readAllBetween() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Reservation a : ((ReservationCrudGeneral)getCrud()).readAllBetween("2012-05-04", "2012-05-08"))
		{
			ok = a.equals(getDataset());
		}
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void readAllBetween2() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Reservation a : ((ReservationCrudGeneral)getCrud()).readAllBetween("2012-05-04", "2012-05-06"))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void readAllBetween3() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Reservation a : ((ReservationCrudGeneral)getCrud()).readAllBetween("2012-05-06", "2012-05-08"))
			ok = a.equals(getDataset());
		assertTrue("read all like not work",ok);
	}
	
	@Test
	public void readAllNotBetween() throws SQLException{
		getCrud().update(getDataset());
		boolean ok=false;
		for (Reservation a : ((ReservationCrudGeneral)getCrud()).readAllBetween("2012-05-02", "2012-05-04"))
			ok = a.equals(getDataset());
		assertFalse("read all like not work",ok);
	}
}
