ok trying another forum - not getting any reply off nabble and i need some help
running grails 2.01
trying to do some real basic dummy code to get used to working with Gorm and keep failing at first hurdle
my integration tests fail, and i cant get domain class created in console either ! - advicxe required
i create three dummy domain classes
Author 1:m to Book, and Book m:n with Reader. I make Book the 'owner' for both relationshiops like this
Author Domain class:
package com.softwood.book
class Author {
String name
Date dateCreated
//List books //change set to list
static hasMany = [books:Book]
static constraints = {
name (size: 3..20)
}
}
Book domaion class:
package com.softwood.book
class Book {
String title
Date dateCreated
static hasMany = [readBy:Reader]
static belongsTo = [author:Author]
static constraints = {
title (size: 3..20)
}
}
Reader domain class
package com.softwood.book
class Reader {
String name
Date dateCreated
static hasMany = [hasRead:Book]
static belongsTo = [Book]
static constraints = {
}
}
- when i try to run from console the following i cant cant create a domain class - it returns null - script like this
import com.softwood.book.Book
import com.softwood.book.Author
import com.softwood.book.Reader
class Dummy
{
String name
}
def d = new Dummy (name : "will")
assert d.name == "will"
res = new Book (title:"my book ")
assert res.title == "my book"
def sav = res.save()
assert sav?.hasErrors () == null
def books = Book.list ()
books.each {-> println it.title}
output says
Assertion failed:
assert res.title == "my book"
| | |
| | false
| my book
com.softwood.book.Book : null
when i try to build integration test and run it both these tests return [] when running a query across an association (its ok if i only query an attribute in the class) - what am i doing wrong ??
...
@Test
void testReadersQuery()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")
def will = new Reader (name:"william")
def maz = new Reader (name:"marian")
book1.addToReadBy (will)
book2.addToReadBy (maz)
book3.addToReadBy (will)
book3.addToReadBy (maz)
book1.save()
book2.save()
book3.save ()
assert book1.readBy.size() == 1
assert book2.readBy.size() == 1
assert book3.readBy.size() == 2
assert will.hasRead.size() ==2
assert maz.hasRead.size() == 2
//query m:n relationship
def bQuery = Book.where {
readBy{name == "william"}
}
def resBook = bQuery.findAll()
assert resBook != [] //fails here
assert resBook.size() == 2
}
@Test
void testAuthorsQuery ()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")
def unk = new Author (name:"uknown")
unk.addToBooks (book1)
unk.addToBooks (book2)
unk.save()
assertFalse unk.hasErrors()
def jk = new Author (name: "JK Rowling")
jk.addToBooks (book3).save()
assertFalse jk.hasErrors()
//query 1:m relationship
def resUnkAuthorQuery = Author.where
{
books.title == "unknown"
}
def resUnkAuth = resUnkAuthorQuery.findAll()
assert resUnkAuth != [] //fails here
assert resUnkAuth.size() == 2
}
running grails 2.01
trying to do some real basic dummy code to get used to working with Gorm and keep failing at first hurdle
my integration tests fail, and i cant get domain class created in console either ! - advicxe required
i create three dummy domain classes
Author 1:m to Book, and Book m:n with Reader. I make Book the 'owner' for both relationshiops like this
Author Domain class:
package com.softwood.book
class Author {
String name
Date dateCreated
//List books //change set to list
static hasMany = [books:Book]
static constraints = {
name (size: 3..20)
}
}
Book domaion class:
package com.softwood.book
class Book {
String title
Date dateCreated
static hasMany = [readBy:Reader]
static belongsTo = [author:Author]
static constraints = {
title (size: 3..20)
}
}
Reader domain class
package com.softwood.book
class Reader {
String name
Date dateCreated
static hasMany = [hasRead:Book]
static belongsTo = [Book]
static constraints = {
}
}
- when i try to run from console the following i cant cant create a domain class - it returns null - script like this
import com.softwood.book.Book
import com.softwood.book.Author
import com.softwood.book.Reader
class Dummy
{
String name
}
def d = new Dummy (name : "will")
assert d.name == "will"
res = new Book (title:"my book ")
assert res.title == "my book"
def sav = res.save()
assert sav?.hasErrors () == null
def books = Book.list ()
books.each {-> println it.title}
output says
Assertion failed:
assert res.title == "my book"
| | |
| | false
| my book
com.softwood.book.Book : null
when i try to build integration test and run it both these tests return [] when running a query across an association (its ok if i only query an attribute in the class) - what am i doing wrong ??
...
@Test
void testReadersQuery()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")
def will = new Reader (name:"william")
def maz = new Reader (name:"marian")
book1.addToReadBy (will)
book2.addToReadBy (maz)
book3.addToReadBy (will)
book3.addToReadBy (maz)
book1.save()
book2.save()
book3.save ()
assert book1.readBy.size() == 1
assert book2.readBy.size() == 1
assert book3.readBy.size() == 2
assert will.hasRead.size() ==2
assert maz.hasRead.size() == 2
//query m:n relationship
def bQuery = Book.where {
readBy{name == "william"}
}
def resBook = bQuery.findAll()
assert resBook != [] //fails here
assert resBook.size() == 2
}
@Test
void testAuthorsQuery ()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")
def unk = new Author (name:"uknown")
unk.addToBooks (book1)
unk.addToBooks (book2)
unk.save()
assertFalse unk.hasErrors()
def jk = new Author (name: "JK Rowling")
jk.addToBooks (book3).save()
assertFalse jk.hasErrors()
//query 1:m relationship
def resUnkAuthorQuery = Author.where
{
books.title == "unknown"
}
def resUnkAuth = resUnkAuthorQuery.findAll()
assert resUnkAuth != [] //fails here
assert resUnkAuth.size() == 2
}