Grails里DOMAIN类的一对一,一对多,多对多关系总结及集成测试
终于干完这一章节,收获很多啊。
和DJANGO有类似,也有不同。
User.groovy:
package com.grailsinaction class User { String loginId String password Date dateCreated static 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: false password size: 6..8, nullable:false, validator: { passwd, user -> passwd != user.loginId } profile nullable: true } }
Profile.groovy:
package com.grailsinaction class Profile { User user byte[] photo String fullName String bio String homepage String email String timezone String country String jabberAddress static constraints = { fullName blank: false bio nullable: true, maxSize: 1000 homepage url:true, nullable: true email email: true, blank: false photo nullable:true, maxSize: 2 * 1024 * 1024 country nullable: true timezone nullable: true jabberAddress email: true, nullable: true } }
Post.groovy:
package com.grailsinaction class Post { String content Date dateCreated static constraints = { content blank: false } static belongsTo = [user: User] static hasMany = [tags: Tag] static mapping = { sort dateCreated: "desc"} }
Tag.groovy:
package com.grailsinaction class Tag { String name User user static constraints = { name blank: false } static hasMany = [posts: Post] static belongsTo = [User, Post] }
UserIntegrationSpec.groovy:
package com.grailsinaction import 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 == 0 joe.id != null User.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() == null assert 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.grailsinaction import 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类的一对一,一对多,多对多关系总结及集成测试的更多相关文章
- SQLAlchemy_定义(一对一/一对多/多对多)关系
目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模 ...
- JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用
下面把项目中的用户类中有个:一对一 一对多 多对多的注解对应关系列取出来用于学习 说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...
- Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作
Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: 由于如果只使用一张表存储所有的数据,就会操作数 ...
- mybatis 一对一 一对多 多对多
一对一 一对多 多对多
- SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份
SSAS事实表中的数据,有时候会因为一对多或多对多关系发生复制变成多份,如下图所示: 图1 我们可以从上面图片中看到,在这个例子中,有三个事实表Fact_People_Money(此表用字段Money ...
- JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析
JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...
- day 69-70 一对一 一对多 多对一联表查询
day 69 orm操作之表关系,多对多,多对一 多对一/一对多, 多对多{类中的定义方法} day69 1. 昨日内容回顾 1. 单表增删改查 2. 单表查询API 返回QuerySet对象的: 1 ...
- 使用NHibernate(7)-- 一对一 && 一对多 && 多对多
1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...
- JPA 一对一 一对多 多对一 多对多配置
1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...
随机推荐
- AD9850驱动程序--MSP430版本
前段时间忙着画板子搞运放搞滤波了,程序更新的少,发现MSP430不是太好用,尤其Timer,不过也与我使用内部晶振有关,产生正玄波之前用MSP430发出PWM,再进行滤波变为正弦波太麻烦了,这次改用D ...
- 搭建Git服务器(转载)
转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360 ...
- [Swift通天遁地]八、媒体与动画-(14)使用TextKit快速实现文章的分栏效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]九、拔剑吧-(5)创建Tab图标具有多种样式的Tab动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- SpringMvc快速入门之使用篇
文章是为了结合工作需求来介绍springmvc,本文章只是切合实际的开发的场景对springmvc进行快速的入门介绍. 本篇文章不会对原理进行讲解.因为个人觉得有些对于新技术方面可以分为一下几个层次. ...
- XML解析-Dom4j的DOM解析方式更新XML
Dom4j工具,是非官方的,不在jdk中. 使用步骤: 1)导入dom4j的核心包. dom4j-1.6.1.jar 2)编写Dom4j读取xml文件的代码 1.更新XML 1.1.写出内容到xml文 ...
- java学习笔记_BeatBox(GUI部分)
import java.awt.*; import javax.swing.*; public class BeatBox { JFrame theFrame; JPanel mainPanel; S ...
- Android 在fragment中实现返回键单击提醒 双击退出
尝试用mvp架构加dagger2来重写了一下,大致功能都实现了,还没有全部完成. 项目地址 接近完成的时候,想在天气信息页面实现一个很常见的功能,也就是点击屏幕下方的返回键的时候不是返回到上一个act ...
- drupal 8——图片组(list)在前台的显示顺序在登录状态和非登录状态不同
问题描述:该页面是通过view来输出的,然而,登录状态下其页面中的图片组输出顺序是乱序的,而非登录状态下则根据id值升序输出. 原因:在原view配置页面中,没有配置默认的排序字段 解决方案:在vie ...
- 把多种验证规则用到一个model上
基于标记特性的Model验证,真的太棒了,与jquery validate结合后激情四射,有木有,一句话完成前后端验证.简直让人秒爱... 但是爱是爱了,生活中总会有些小摩擦,这不问题来了:看图中那个 ...