Skip to content

Instantly share code, notes, and snippets.

@joeRinehart
Created August 14, 2012 22:56
Show Gist options
  • Save joeRinehart/3353719 to your computer and use it in GitHub Desktop.
Save joeRinehart/3353719 to your computer and use it in GitHub Desktop.
class Author {
static hasMany = [books: Book]
String name
}
class Book {
static belongsTo = [author: Author]
String title
}
package grailsmodelspart1
class CoinToss {
static constraints = {
}
}
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
}
}
package grailsmodelspart1
class CoinTossService {
List list() {
return CoinToss.list()
}
CoinToss save( coinToss ) {
coinToss.save()
}
CoinToss delete( coinToss ) {
coinToss.delete()
}
}
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
""")
}
}
package grailsmodelspart1
class CoinToss {
Boolean wasHeads = false
// Autotimestamping by convention
Date dateCreated
Date lastUpdated
static constraints = {
}
}
List listHeads() {
return CoinToss.findByWasHeads( true )
}
Person.findAllByNameLikeAndNameNot(
"%Bo%",
"Bob",
[
max : 5,
offset : 5,
sort : "name",
order : "desc"
]
)
Person.findAll( """
from
Person p
where
p.name like '%Bo%'
and p.name != 'BoB'
order by
p.name desc
""",
[
max : 5,
offset : 5
]
)
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