package com.peterphi.std.guice.hibernate.largetable;

import com.google.inject.Inject;
import com.peterphi.std.guice.database.annotation.Transactional;
import com.peterphi.std.guice.hibernate.dao.HibernateDao;
import com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery;
import com.peterphi.std.guice.testing.GuiceUnit;
import com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.GuiceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(GuiceUnit.class)
@GuiceConfig(config = "hibernate-tests-in-memory-hsqldb.properties",
		classPackages = LargeTableQueryTest.class)
public class LargeTableQueryTest
{
	@Inject
	MyTestDaoImpl dao;

	@Inject
	HibernateDao<LargeTableComplexPKEntity, SomePrimaryKey> complexPKdao;


	/**
	 * Test that searching works with a simple primary key
	 */
	@Test
	@Transactional
	public void testSearchWorks()
	{
		dao.save(new LargeTableSimplePKEntity("Alice"));
		dao.save(new LargeTableSimplePKEntity("Bob"));
		dao.save(new LargeTableSimplePKEntity("Carol"));
		dao.save(new LargeTableSimplePKEntity("Dave"));
		dao.save(new LargeTableSimplePKEntity("Eve"));

		// Try a regular query
		assertEquals(2, dao.countWithAInName());

		// Now try a web query
		assertEquals(2, dao.findByUriQuery(new WebQuery().contains("name", "a")).getList().size());
	}


	/**
	 * Test that searching works even with a complex primary key
	 */
	@Test
	@Transactional
	public void testSearchComplexPK()
	{
		complexPKdao.save(new LargeTableComplexPKEntity("Alice"));
		complexPKdao.save(new LargeTableComplexPKEntity("Bob"));
		complexPKdao.save(new LargeTableComplexPKEntity("Carol"));
		complexPKdao.save(new LargeTableComplexPKEntity("Dave"));
		complexPKdao.save(new LargeTableComplexPKEntity("Eve"));

		assertEquals(2, complexPKdao.findByUriQuery(new WebQuery().contains("name", "a")).getList().size());
	}
}

@@ -468,16 +468,40 @@
 nWith;%0A%0A
+import java.util.List;%0A%0A
 import s
@@ -1391,16 +1391,661 @@
 ));%0A%09%7D%0A%0A
+%09/**%0A%09 * Test that searching works with ordering%0A%09 */%0A%09@Test%0A%09@Transactional%0A%09public void testSearchWorksWithOrder()%0A%09%7B%0A%09%09dao.save(new LargeTableSimplePKEntity(%22Alice%22));%0A%09%09dao.save(new LargeTableSimplePKEntity(%22Bob%22));%0A%09%09dao.save(new LargeTableSimplePKEntity(%22Carol%22));%0A%09%09dao.save(new LargeTableSimplePKEntity(%22Dave%22));%0A%09%09dao.save(new LargeTableSimplePKEntity(%22Eve%22));%0A%0A%09%09// Now try a web query%0A%0A%09%09List%3CLargeTableSimplePKEntity%3E list = dao.findByUriQuery(new WebQuery().contains(%22name%22, %22l%22).orderDesc(%22name%22)).getList();%0A%0A%09%09assertEquals(2,list.size());%0A%09%09assertEquals(%22Carol%22,list.get(0).name);%0A%09%09assertEquals(%22Alice%22,list.get(1).name);%0A%09%7D%0A%0A%0A%0A
 %0A%09/**%0A%09 

add ordering test to large table querying support (currently failing)