From d34ee233f7bd331123fc2f7118f5b8d9c4b90646 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Mon, 13 Dec 2010 20:58:42 +0000 Subject: [PATCH] Boil some plates, whatever that means --- .../com/samskivert/depot/ProjectionTest.java | 14 ++++++++++ .../java/com/samskivert/depot/QueryTest.java | 26 ++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/samskivert/depot/ProjectionTest.java b/src/test/java/com/samskivert/depot/ProjectionTest.java index 6ca9ddd..f80d322 100644 --- a/src/test/java/com/samskivert/depot/ProjectionTest.java +++ b/src/test/java/com/samskivert/depot/ProjectionTest.java @@ -202,6 +202,13 @@ public class ProjectionTest extends TestBase selectInto(IdName.class, TestRecord.RECORD_ID, TestRecord.NAME).toString()); } + @Test public void testSelectBuilder () + { + assertEquals("[0 Elvis, 1 Elvis]", + _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.lessThan(2)). + select(IDNAME_BUILDER, TestRecord.RECORD_ID, TestRecord.NAME).toString()); + } + protected static class IdName { public final int id; public final String name; @@ -214,6 +221,13 @@ public class ProjectionTest extends TestBase } } + protected static Builder2 IDNAME_BUILDER = + new Builder2() { + public IdName build (Integer a, String b) { + return new IdName(a, b); + } + }; + // the HSQL in-memory database persists for the lifetime of the VM, which means we have to // clean up after ourselves in every test; thus we go ahead and share a repository protected TestRepository _repo = createTestRepository(); diff --git a/src/test/java/com/samskivert/depot/QueryTest.java b/src/test/java/com/samskivert/depot/QueryTest.java index f462cad..2aef2d2 100644 --- a/src/test/java/com/samskivert/depot/QueryTest.java +++ b/src/test/java/com/samskivert/depot/QueryTest.java @@ -20,16 +20,20 @@ package com.samskivert.depot; -import java.util.Collections; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import com.google.common.collect.Sets; -import com.samskivert.util.RandomUtil; +import java.util.Collections; +import java.util.List; import org.junit.Test; -import static org.junit.Assert.*; + +import com.google.common.collect.Sets; import com.samskivert.depot.annotation.Computed; import com.samskivert.depot.expression.SQLExpression; +import com.samskivert.depot.util.Builder2; +import com.samskivert.util.RandomUtil; /** * Tests queries. @@ -53,6 +57,7 @@ public class QueryTest extends TestBase TestRecord record = createTestRecord(ii); record.name = "Spam! " + ii; record.age = RandomUtil.getInt(100); + record.awesomeness = RandomUtil.getFloat(1.0F); record.homeTown = "Over there"; _repo.insert(record); } @@ -77,6 +82,19 @@ public class QueryTest extends TestBase assertEquals("Spam! " + tnr.recordId, tnr.name); } + Builder2 ageAwesome = new Builder2() { + public Float build (Integer a, Float b) { + return a * b; + } + }; + List results = + _repo.from(TestRecord.class).select(ageAwesome, TestRecord.AGE, TestRecord.AWESOMENESS); + assertEquals(CREATE_RECORDS, results.size()); + for (float result : results) { + assertTrue("Age goes from [0,99) and awesomeness goes from [0.0,1.0), so their " + + "product should be [0.0,100), not " + result, result >= 0 && result < 100); + } + assertEquals(CREATE_RECORDS, _repo.findAll(TestRecord.class).size()); _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.lessEq(CREATE_RECORDS/2)).delete(); assertEquals(CREATE_RECORDS/2, _repo.findAll(TestRecord.class).size());