Skip to content

Instantly share code, notes, and snippets.

@joeRinehart
Created August 18, 2012 13:13
Show Gist options
  • Save joeRinehart/3386780 to your computer and use it in GitHub Desktop.
Save joeRinehart/3386780 to your computer and use it in GitHub Desktop.
GrailsModelsPart2
package recordstore
class Album {
String name
static constraints = {
}
}
package recordstore
class Album {
// Mappings
static hasMany = [
songs : Song
]
// Properties
String name
static constraints = {
}
}
@Test
void albumHasManySongShouldCascadeSaves() {
def originalSongCount = Song.list().size()
// Create and save an album
def album = new Album( name: "Emotionalism" ).save()
// Create a song without an album: shoudn't be valid
def song = new Song( name: "Salvation Song" )
album.addToSongs( song )
// References should be set up
assert song.album == album
assert album.songs.contains( song )
// Saving album should save song
assert !song.id
album.save()
assert Song.list().size() == originalSongCount + 1
assert song.id
}
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost/recordstore_dev?useUnicode=yes&characterEncoding=UTF-8"
username = "root"
password = ""
logSql = true
}
}
test {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost/recordstore_test?useUnicode=yes&characterEncoding=UTF-8"
username = "root"
password = ""
logSql = true
}
}
@Test
void settingSongsAlbumDoesntAddToAlbumsSongs() {
// Create and save an album
def album = new Album( name: "Emotionalism" ).save()
// Create a song without an album: shoudn't be valid
def song = new Song( name: "Salvation Song" )
song.album = album
// Back references should not be set up
assert song.album == album
assert album.songs == null
}
package recordstore
class Song {
String name
// Many-to-one Album
Album album
static constraints = {
}
}
package recordstore
class Song {
// Mappings
static belongsTo = [
album : Album
]
String name
// Many-to-one Album
Album album
static constraints = {
}
}
@Test
void songBelongsToAlbumShouldCascadeDeletes() {
def originalSongCount = Song.list().size()
// Create and save an album
def album = new Album( name: "Emotionalism" )
def song = new Song( name: "Salvation Song" )
album.addToSongs( song )
// Saving an album should also save its songs
assert !album.id
assert !song.id
album.save()
assert album.id
assert song.id
// Deleting an album should delete its songs
album.delete()
assert Song.list().size() == originalSongCount
}
@Test
void songShouldHaveAnAlbum() {
// Create and save an album
def album = new Album( name: "Emotionalism" ).save()
// Create a song without an album: shoudn't be valid
def song = new Song( name: "Salvation Song" )
// Shouldn't validate, shouldn't have an id
assert !song.validate()
assert !song.id
// Set its album: should validate and save
song.album = album
assert song.validate()
song.save()
assert song.id
}
@rehmetjan
Copy link

Nice, but I still confused about hasMany and blongsTo works with Album album..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment