终于干完这一章节,收获很多啊。

和DJANGO有类似,也有不同。

User.groovy:

package com.grailsinactionclass User {String loginIdString passwordDate dateCreatedstatic hasOne = [profile: Profile]static hasMany = [posts: Post, tags: Tag, following: User]static mapping = {posts sort: 'dateCreated'}static constraints = {loginId size: 3..20, unique: true, nullable: falsepassword size: 6..8, nullable:false, validator: { passwd, user -> passwd != user.loginId }profile nullable: true}
}

Profile.groovy:

package com.grailsinactionclass Profile {User userbyte[] photoString fullNameString bioString homepageString emailString timezoneString countryString jabberAddressstatic constraints = {fullName blank: falsebio nullable: true, maxSize: 1000homepage url:true, nullable: trueemail email: true, blank: falsephoto nullable:true, maxSize: 2 * 1024 * 1024country nullable: truetimezone nullable: truejabberAddress email: true, nullable: true}
}

Post.groovy:

package com.grailsinactionclass Post {String contentDate dateCreatedstatic constraints = {content blank: false}static belongsTo = [user: User]static hasMany = [tags: Tag]static mapping = { sort dateCreated: "desc"}
}

Tag.groovy:

package com.grailsinactionclass Tag {String nameUser userstatic constraints = {name blank: false}static hasMany = [posts: Post]static belongsTo = [User, Post]
}

UserIntegrationSpec.groovy:

package com.grailsinactionimport grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*@Integration
@Rollback
class UserIntegrationSpec extends Specification {def setup() {}def cleanup() {}def "Saving our first user to the database"() {given: "A brand new user"def joe = new User(loginId: 'Joe', password: 'secret')when: "the user is saved"joe.save()then: "it saved successfully and can be found in the database"joe.errors.errorCount == 0joe.id != nullUser.get(joe.id).loginId == joe.loginId}def "Updating a saved user changes its properties"() {given: "An existing user"def existingUser = new User(loginId: 'Joe', password: 'secret')existingUser.save(failOnError: true)when: "A property is changed"def foundUser = User.get(existingUser.id)foundUser.password = 'sesame'foundUser.save(failOnError: true)then: "The change is reflected in the database"User.get(existingUser.id).password == 'sesame'}def "Deleting an existing user removes it from the database"() {given: "An existing user"def user = new User(loginId: 'Joe', password: 'secret')user.save(failOnError: true)when: "The user is deleted"def foundUser = User.get(user.id)foundUser.delete(flush: true)then: "The user is removed from the database"!User.exists(foundUser.id)}def "Saving a user with invalid properties causes an error"() {given: "A user which fails several field validations"def user = new User(loginId: 'Joe', password: 'tiny')when: "The user is validated"user.validate()then:user.hasErrors()"size.toosmall" == user.errors.getFieldError("password").code"tiny" == user.errors.getFieldError("password").rejectedValue!user.errors.getFieldError("loginId")}def "Recovering from a failed save by fixing invalid properties"() {given: "A user that has invalid properties"def chuck = new User(loginId: 'chuck', password: 'tiny')assert chuck.save() == nullassert chuck.hasErrors()when: "We fix the invalid properties"chuck.password = "fistfist"chuck.validate()then: "The user saves and validates fine"!chuck.hasErrors()chuck.save()}def "Ensure a user can follow other users"() {given: "A set of baseline users"def joe = new User(loginId: 'joe', password: 'password').save()def jane = new User(loginId: 'jane', password: 'password').save()def jill = new User(loginId: 'jill', password: 'password').save()when: "Joe follows Jan & Jill, and Jill follows Jane"joe.addToFollowing(jane)joe.addToFollowing(jill)jill.addToFollowing(jane)then: "Follower counts should match follow people"2 == joe.following.size()1 == jill.following.size()}}

PostIntegrationSpec.groovy:

package com.grailsinactionimport grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*@Integration
@Rollback
class PostIntegrationSpec extends Specification {def setup() {}def cleanup() {}def "Adding posts to user links post to user"() {given: "A brand new user"def user = new User(loginId: 'joe', password: 'secret')user.save(failOnError: true)when: "Several posts are added to the user"user.addToPosts(new Post(content: "First post.....w002!"))user.addToPosts(new Post(content: "Second post......"))user.addToPosts(new Post(content: "Third post...."))then: "The user has a list of posts attached"3 == User.get(user.id).posts.size()}def "Ensure posts linked to a user can be retrieved"() {given: "A user with several posts"def user = new User(loginId: 'joe', password: 'secret')user.addToPosts(new Post(content: "First"))user.addToPosts(new Post(content: "Second"))user.addToPosts(new Post(content: "Third"))user.save(failOnError: true)when: "The user is retrieved by their id"def foundUser = User.get(user.id)def sortedPostContent = foundUser.posts.collect {it.content}.sort()then: "The posts appear on the retrieved user "sortedPostContent == ['First', 'Second', 'Third']}def "Exercise tagging several post with various tags"() {given: "A user with a set of tags"def user = new User(loginId: 'joe', password: 'secret')def tagGroovy = new Tag(name:"groovy")def tagGrails = new Tag(name:"grails")user.addToTags(tagGroovy)user.addToTags(tagGrails)user.save(failOnError: true)when:"The user tags two fresh posts"def groovyPost = new Post(content: "A groovy post")user.addToPosts(groovyPost)groovyPost.addToTags(tagGroovy)def bothPost = new Post(content: "A groovy and grails post")user.addToPosts(bothPost)bothPost.addToTags(tagGroovy)bothPost.addToTags(tagGrails)then:user.tags*.name.sort() == ['grails', 'groovy']1 == groovyPost.tags.size()2 == bothPost.tags.size()}
}

Grails里DOMAIN类的一对一,一对多,多对多关系总结及集成测试相关推荐

  1. SQLAlchemy_定义(一对一/一对多/多对多)关系

    SQLAlchemy_定义(一对一/一对多/多对多)关系 目录 Basic Relationship Patterns One To Many One To One Many To Many Basi ...

  2. 数据库一对一 一对多 多对多关系

    参考:https://blog.csdn.net/u013144287/article/details/79024130 自己在项目中写的实例: '实体'和'公理'具有多对多关系,即一个实体可以对应多 ...

  3. 7. MyBatis多表查询 - 一对一 - 一对多 - 多对多

    7. MyBatis多表查询 - 一对一 - 一对多 - 多对多 前言 在前面的篇章,我们已经熟悉了单表查询,下面我们来看看如何进行 多表查询. 数据准备 create database if not ...

  4. mybatis的一对一 一对多 多对多

    mybatis的一对一 一对多 多对多 1.表 2.建表语句 order_t表 CREATE TABLE `order_t` ( `id` int(11) NOT NULL, `user_id` in ...

  5. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  6. day 69-70 一对一 一对多 多对一联表查询

    day 69 orm操作之表关系,多对多,多对一多对一/一对多,多对多{类中的定义方法}day691. 昨日内容回顾1. 单表增删改查2. 单表查询API返回QuerySet对象的:1. .all() ...

  7. JPA 一对一 一对多 多对一 多对多配置

    1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  8. Django一对一 ,一对多,多对多

    Django 数据库一对多,多对多 目录 app01/models.py app01/views.py app01/urls.py Python_Django/urls.py 目录 app01/mod ...

  9. Hibernate一对多/多对一关系映射详解及相应的增删查改操作

    以客户与订单的关系为例 1.在实体模型类中绑定两者关系: 客户类: public class Customer {private Integer id;//客户idprivate String nam ...

最新文章

  1. RememberMe 功能的实现(base-auth使用说明)
  2. 【小白学习C++ 教程】二十二、C++ 中的STL容器stack、queue和map
  3. linux编程两个子进程,Linux中fork同时创建多个子进程的方法
  4. 详解Vue中watch的高级用法
  5. 从入门到入土:基于C语言采用UDP协议实现通信功能的程序
  6. 数字int转换成文字string形式的方法
  7. DWG文件不大,打开却慢的要死?
  8. Apple官方对于Http Live Streaming的常见问题回答
  9. 杂文 | 金沙江创投朱啸虎谈微信小程序
  10. 打开 igv java_IGV加载很久很烦人?三步帮你解决!
  11. Boardcast receiver
  12. 杀疯了!本科大神毕设:3D虚拟主播软件!独创AR直播!支持OBS!开源!
  13. 关于string字符串大小比较以及运算符重载
  14. 前百度测试经理手把手教你用Jmeter实现登录测试
  15. 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第28讲:PHP数组
  16. 群晖NAS搭建WebDav服务,并内网穿透实现远程访问
  17. dllcache“转移”到别的盘
  18. 如何用tkinter给你爱人画一个爱心,这满满的油腻感是怎么回事?
  19. 【每周安全资讯】 2022 第 二 期
  20. 体验过后只剩惊叹,K80双模耳机带你游戏人生

热门文章

  1. 信息太多,时间太少: 大脑如何区分重要和不重要的事?
  2. 人工智能产业发展联盟公布首轮AI芯片基准评测结果,评估工具已开源
  3. 关于量子计算,你应该知道的七个事实
  4. 学界 | DeepMind想用IQ题测试AI的抽象思维能力,进展还不错
  5. 数据流通实现“可用不可见”?腾讯巧夺“天工”
  6. GitHub 标星 2.5K+,U^2-Net 跨界肖像画,完美复刻人物细节!
  7. ​Google 鼓励的 13 条代码审查标准,建议收藏!
  8. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解
  9. [error] - Build path is incomplete. Cannot find class file for org/aspectj/weaver/refl
  10. 高通研发VR软件进军医疗领域,帮助医生诊断中风病患