to-json.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes } = require('@sequelize/core');
  6. describe(Support.getTestDialectTeaser('Instance'), () => {
  7. describe('toJSON', () => {
  8. beforeEach(async function () {
  9. this.User = this.sequelize.define(
  10. 'User',
  11. {
  12. username: { type: DataTypes.STRING },
  13. age: DataTypes.INTEGER,
  14. level: { type: DataTypes.INTEGER },
  15. isUser: {
  16. type: DataTypes.BOOLEAN,
  17. defaultValue: false,
  18. },
  19. isAdmin: { type: DataTypes.BOOLEAN },
  20. },
  21. {
  22. timestamps: false,
  23. },
  24. );
  25. this.Project = this.sequelize.define(
  26. 'NiceProject',
  27. { title: DataTypes.STRING },
  28. { timestamps: false },
  29. );
  30. this.User.hasMany(this.Project, { as: 'Projects', foreignKey: 'lovelyUserId' });
  31. this.Project.belongsTo(this.User, { as: 'LovelyUser', foreignKey: 'lovelyUserId' });
  32. await this.User.sync({ force: true });
  33. await this.Project.sync({ force: true });
  34. });
  35. it("doesn't return instance that isn't defined", async function () {
  36. const project0 = await this.Project.create({ lovelyUserId: null });
  37. const project = await this.Project.findOne({
  38. where: {
  39. id: project0.id,
  40. },
  41. include: [{ model: this.User, as: 'LovelyUser' }],
  42. });
  43. const json = project.toJSON();
  44. expect(json.LovelyUser).to.equal(null);
  45. });
  46. it("doesn't return instances that aren't defined", async function () {
  47. const user0 = await this.User.create({ username: 'cuss' });
  48. const user = await this.User.findOne({
  49. where: {
  50. id: user0.id,
  51. },
  52. include: [{ model: this.Project, as: 'Projects' }],
  53. });
  54. expect(user.Projects).to.be.instanceof(Array);
  55. expect(user.Projects).to.be.length(0);
  56. });
  57. describe('build', () => {
  58. it('returns an object containing all values', function () {
  59. const user = this.User.build({
  60. username: 'Adam',
  61. age: 22,
  62. level: -1,
  63. isUser: false,
  64. isAdmin: true,
  65. });
  66. expect(user.toJSON()).to.deep.equal({
  67. id: null,
  68. username: 'Adam',
  69. age: 22,
  70. level: -1,
  71. isUser: false,
  72. isAdmin: true,
  73. });
  74. });
  75. it('returns a response that can be stringified', function () {
  76. const user = this.User.build({
  77. username: 'test.user',
  78. age: 99,
  79. isAdmin: true,
  80. isUser: false,
  81. });
  82. expect(JSON.stringify(user)).to.deep.equal(
  83. '{"id":null,"username":"test.user","age":99,"isAdmin":true,"isUser":false}',
  84. );
  85. });
  86. it('returns a response that can be stringified and then parsed', function () {
  87. const user = this.User.build({ username: 'test.user', age: 99, isAdmin: true });
  88. expect(JSON.parse(JSON.stringify(user))).to.deep.equal({
  89. username: 'test.user',
  90. age: 99,
  91. isAdmin: true,
  92. isUser: false,
  93. id: null,
  94. });
  95. });
  96. });
  97. describe('create', () => {
  98. it('returns an object containing all values', async function () {
  99. const user = await this.User.create({
  100. username: 'Adam',
  101. age: 22,
  102. level: -1,
  103. isUser: false,
  104. isAdmin: true,
  105. });
  106. expect(user.toJSON()).to.deep.equal({
  107. id: user.get('id'),
  108. username: 'Adam',
  109. age: 22,
  110. isUser: false,
  111. isAdmin: true,
  112. level: -1,
  113. });
  114. });
  115. it('returns a response that can be stringified', async function () {
  116. const user = await this.User.create({
  117. username: 'test.user',
  118. age: 99,
  119. isAdmin: true,
  120. isUser: false,
  121. level: null,
  122. });
  123. expect(JSON.stringify(user)).to.deep.equal(
  124. `{"id":${user.get('id')},"username":"test.user","age":99,"isAdmin":true,"isUser":false,"level":null}`,
  125. );
  126. });
  127. it('returns a response that can be stringified and then parsed', async function () {
  128. const user = await this.User.create({
  129. username: 'test.user',
  130. age: 99,
  131. isAdmin: true,
  132. level: null,
  133. });
  134. expect(JSON.parse(JSON.stringify(user))).to.deep.equal({
  135. age: 99,
  136. id: user.get('id'),
  137. isAdmin: true,
  138. isUser: false,
  139. level: null,
  140. username: 'test.user',
  141. });
  142. });
  143. });
  144. describe('find', () => {
  145. it('returns an object containing all values', async function () {
  146. const user0 = await this.User.create({
  147. username: 'Adam',
  148. age: 22,
  149. level: -1,
  150. isUser: false,
  151. isAdmin: true,
  152. });
  153. const user = await this.User.findByPk(user0.get('id'));
  154. expect(user.toJSON()).to.deep.equal({
  155. id: user.get('id'),
  156. username: 'Adam',
  157. age: 22,
  158. level: -1,
  159. isUser: false,
  160. isAdmin: true,
  161. });
  162. });
  163. it('returns a response that can be stringified', async function () {
  164. const user0 = await this.User.create({
  165. username: 'test.user',
  166. age: 99,
  167. isAdmin: true,
  168. isUser: false,
  169. });
  170. const user = await this.User.findByPk(user0.get('id'));
  171. expect(JSON.stringify(user)).to.deep.equal(
  172. `{"id":${user.get('id')},"username":"test.user","age":99,"level":null,"isUser":false,"isAdmin":true}`,
  173. );
  174. });
  175. it('returns a response that can be stringified and then parsed', async function () {
  176. const user0 = await this.User.create({
  177. username: 'test.user',
  178. age: 99,
  179. isAdmin: true,
  180. });
  181. const user = await this.User.findByPk(user0.get('id'));
  182. expect(JSON.parse(JSON.stringify(user))).to.deep.equal({
  183. id: user.get('id'),
  184. username: 'test.user',
  185. age: 99,
  186. isAdmin: true,
  187. isUser: false,
  188. level: null,
  189. });
  190. });
  191. });
  192. it('includes the eagerly loaded associations', async function () {
  193. const user = await this.User.create({ username: 'fnord', age: 1, isAdmin: true });
  194. const project = await this.Project.create({ title: 'fnord' });
  195. await user.setProjects([project]);
  196. const users = await this.User.findAll({ include: [{ model: this.Project, as: 'Projects' }] });
  197. const _user = users[0];
  198. expect(_user.Projects).to.exist;
  199. expect(JSON.parse(JSON.stringify(_user)).Projects).to.exist;
  200. const projects = await this.Project.findAll({
  201. include: [{ model: this.User, as: 'LovelyUser' }],
  202. });
  203. const _project = projects[0];
  204. expect(_project.LovelyUser).to.exist;
  205. expect(JSON.parse(JSON.stringify(_project)).LovelyUser).to.exist;
  206. });
  207. });
  208. });