package com.peterphi.std.guice.hibernate.webquery.impl.functions;

import com.peterphi.std.guice.hibernate.webquery.impl.QProperty;
import com.peterphi.std.guice.hibernate.webquery.impl.QPropertyRef;
import org.hibernate.criterion.Criterion;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class BetweenTest
{
	@Test
	public void testBinaryRange()
	{
		final QProperty prop = new QProperty(null, null, "x", Integer.class, false);
		final QPropertyRef ref = new QPropertyRef(null, prop);

		final Criterion criterion = new Between(ref, "1..2").encode();

		assertEquals("x between 1 and 2", criterion.toString());
	}


	@Test
	public void testLeftOnlyRange()
	{
		final QProperty prop = new QProperty(null, null, "x", Integer.class, false);
		final QPropertyRef ref = new QPropertyRef(null, prop);

		final Criterion criterion = new Between(ref, "1..").encode();

		assertEquals("x>=1", criterion.toString());
	}


	@Test
	public void testRightOnlyRange()
	{
		final QProperty prop = new QProperty(null, null, "x", Integer.class, false);
		final QPropertyRef ref = new QPropertyRef(null, prop);

		final Criterion criterion = new Between(ref, "..1").encode();

		assertEquals("x<=1", criterion.toString());
	}
}

@@ -193,16 +193,150 @@
 rtyRef;%0A
+import com.peterphi.std.guice.hibernate.webquery.impl.QRelation;%0Aimport com.peterphi.std.guice.hibernate.webquery.impl.QSizeProperty;%0A
 import o
@@ -766,32 +766,517 @@
 String());%0A%09%7D%0A%0A%0A
+%09@Test%0A%09public void testBinarySizeRange()%0A%09%7B%0A%09%09final QRelation relation = new QRelation(null, null, %22children%22, null, false);%0A%09%09final QSizeProperty prop = new QSizeProperty(relation);%0A%09%09final QPropertyRef ref = new QPropertyRef(null, prop);%0A%0A%09%09final Criterion criterion = new Between(ref, %221..2%22).encode();%0A%0A%09%09// N.B. the toString of size restrictions is incorrect due to HHH-9869 so this looks wrong%0A%09%09assertEquals(%22children.size%3C=1 and children.size%3E=2%22, criterion.toString());%0A%09%7D%0A%0A%0A
 %09@Test%0A%09public v

Add a unit test for Between applied to a size field (note: the assertEquals expected value looks wrong due to Hibernate bug https://hibernate.atlassian.net/browse/HHH-9869 - see a potential fix PR at https://github.com/hibernate/hibernate-orm/pull/980)