Created
August 14, 2012 22:56
-
-
Save joeRinehart/3353719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
class CoinToss { | |
static constraints = { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
import static org.junit.Assert.* | |
import org.junit.* | |
class CoinTossPersistenceTests { | |
CoinTossService coinTossService | |
@Test | |
void we_can_crud_coin_tosses() { | |
// Make sure the service can list | |
def tosses = coinTossService.list() | |
def originalTossCount = tosses.size() | |
// Make sure the service can create | |
def ct = new CoinToss() | |
coinTossService.save( ct ) | |
assert coinTossService.list().size() == originalTossCount + 1 | |
// Make sure the serivce can update without creating new instances | |
ct.wasHeads = !ct.wasHeads | |
coinTossService.save( ct ) | |
assert coinTossService.list().size() == originalTossCount + 1 | |
// Make sure we can delete | |
coinTossService.delete( ct ) | |
assert coinTossService.list().size() == originalTossCount | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
class CoinTossService { | |
List list() { | |
return CoinToss.list() | |
} | |
CoinToss save( coinToss ) { | |
coinToss.save() | |
} | |
CoinToss delete( coinToss ) { | |
coinToss.delete() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
import groovy.sql.Sql | |
class CoinTossService { | |
def dataSource | |
List headsOrTailsReport() { | |
return new Sql( dataSource ).rows(""" | |
select | |
count(1) as times_landed_this_side, | |
was_heads | |
from | |
coin_toss | |
group by | |
was_heads | |
""") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
class CoinToss { | |
Boolean wasHeads = false | |
// Autotimestamping by convention | |
Date dateCreated | |
Date lastUpdated | |
static constraints = { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List listHeads() { | |
return CoinToss.findByWasHeads( true ) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.findAllByNameLikeAndNameNot( | |
"%Bo%", | |
"Bob", | |
[ | |
max : 5, | |
offset : 5, | |
sort : "name", | |
order : "desc" | |
] | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.findAll( """ | |
from | |
Person p | |
where | |
p.name like '%Bo%' | |
and p.name != 'BoB' | |
order by | |
p.name desc | |
""", | |
[ | |
max : 5, | |
offset : 5 | |
] | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grailsmodelspart1 | |
class CoinToss { | |
Boolean wasHeads = false | |
static constraints = { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment