Permalink
Browse files

closes #5 createCriteria().list(params)

  • Loading branch information...
fabiooshiro committed Mar 16, 2013
1 parent 327bded commit 6224ac1d39adf007359b633cedab04c44c8241f3
@@ -1,5 +1,5 @@
class PlasticCriteriaGrailsPlugin {
- def version = "0.6"
+ def version = "0.7"
def grailsVersion = "1.3 > *"
def pluginExcludes = [
'grails-app/domain/**',
@@ -483,4 +483,65 @@ class CriteriaDocTests {
assert 1 == rs.size()
assert 'Japanese Bridge' == rs[0].bestPlace.name
}
+
+ // next release 0.7
+ void test_list_params_max(){
+ def monet = new Artist(name: 'Monet').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ new Portrait(artist: monet, name: 'Soleil levant 2').save()
+ new Portrait(artist: monet, name: 'Soleil levant 3').save()
+ new Portrait(artist: monet, name: 'Soleil levant 4').save()
+ new Portrait(artist: monet, name: 'Soleil levant 5').save()
+ new Portrait(artist: monet, name: 'Soleil levant 6').save()
+ new Portrait(artist: monet, name: 'Soleil levant 7').save()
+ def rs = Portrait.createCriteria().list([max: 3]){
+ eq('artist', monet)
+ }
+ assert 3 == rs.size()
+ }
+
+ void test_list_params_max_and_offset(){
+ def monet = new Artist(name: 'Monet').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ new Portrait(artist: monet, name: 'Soleil levant 2').save()
+ new Portrait(artist: monet, name: 'Soleil levant 3').save()
+ new Portrait(artist: monet, name: 'Soleil levant 4').save()
+ new Portrait(artist: monet, name: 'Soleil levant 5').save()
+ new Portrait(artist: monet, name: 'Soleil levant 6').save()
+ new Portrait(artist: monet, name: 'Soleil levant 7').save()
+ def rs = Portrait.createCriteria().list([max: 3, offset: 2]){
+ eq('artist', monet)
+ }
+ assert 3 == rs.size()
+ assert 'Soleil levant 3' == rs[0].name
+ assert 'Soleil levant 4' == rs[1].name
+ assert 'Soleil levant 5' == rs[2].name
+ }
+
+ void test_list_params_sort(){
+ def monet = new Artist(name: 'Monet').save()
+ new Portrait(artist: monet, name: 'Soleil levant 2').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ new Portrait(artist: monet, name: 'Soleil levant 3').save()
+ def rs = Portrait.createCriteria().list([sort: 'name']){
+ eq('artist', monet)
+ }
+ assert 'Soleil levant 1' == rs[0].name
+ assert 'Soleil levant 2' == rs[1].name
+ assert 'Soleil levant 3' == rs[2].name
+ }
+
+ void test_list_params_sort_order(){
+ def monet = new Artist(name: 'Monet').save()
+ new Portrait(artist: monet, name: 'Soleil levant 2').save()
+ new Portrait(artist: monet, name: 'Soleil levant 3').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ def rs = Portrait.createCriteria().list([sort: 'name', order: 'desc']){
+ eq('artist', monet)
+ }
+ assert 'Soleil levant 3' == rs[0].name
+ assert 'Soleil levant 2' == rs[1].name
+ assert 'Soleil levant 1' == rs[2].name
+ }
+
}
@@ -3,6 +3,7 @@ package plastic.criteria
class PlasticCriteria {
def _clazz
def _maxRes
+ def _offset
def _props = []
def _hasCalcProp = false
def _groupProps = []
@@ -166,7 +167,16 @@ class PlasticCriteria {
theAndOrNotPush('not', clos)
}
- def list(clos){
+ def list(params, Closure clos){
+ _maxRes = params.max
+ _offset = params.offset
+ if(params.sort){
+ order(params.sort, params.order ?: 'asc')
+ }
+ return list(clos)
+ }
+
+ def list(Closure clos){
clos.delegate = this
clos()
def ls = _filteredList()
@@ -225,7 +235,13 @@ class PlasticCriteria {
}
ls = rs
}
- return _handleUniqueResult(ls)
+ return _handleUniqueResult(_maxAndOffset(ls))
+ }
+
+ def _maxAndOffset(ls){
+ if(_offset) ls = ls[_offset..-1]
+ if(_maxRes) ls = ls[0..(_maxRes -1)]
+ ls
}
def _handleUniqueResult(ls){
@@ -291,10 +307,8 @@ class PlasticCriteria {
}
}
_orders.each{
- def arr = it.split(' ')
- def prop = arr[0]
- def order = arr[1]
- r.sort{ a, b->
+ def (prop, order) = it.split(' ')
+ r.sort{ a, b ->
try{
if(order == 'asc'){
return _getProp(a, prop).compareTo(_getProp(b, prop))
@@ -0,0 +1,18 @@
+package plastic.criteria;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Make sure that the same unit tests pass in integration enviroment
+ */
+public class PlasticCriteriaIntegrationTests extends CriteriaDocTests{
+
+ @Before
+ void setUp(){
+
+ }
+
+}

0 comments on commit 6224ac1

Please sign in to comment.