Created
August 18, 2012 13:13
-
-
Save joeRinehart/3386780 to your computer and use it in GitHub Desktop.
GrailsModelsPart2
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 recordstore | |
class Album { | |
String name | |
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 recordstore | |
class Album { | |
// Mappings | |
static hasMany = [ | |
songs : Song | |
] | |
// Properties | |
String name | |
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
@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 | |
} |
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
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 | |
} | |
} |
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
@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 | |
} |
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 recordstore | |
class Song { | |
String name | |
// Many-to-one Album | |
Album album | |
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 recordstore | |
class Song { | |
// Mappings | |
static belongsTo = [ | |
album : Album | |
] | |
String name | |
// Many-to-one Album | |
Album album | |
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
@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 | |
} |
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
@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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, but I still confused about hasMany and blongsTo works with Album album..