scope.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const chai = require('chai');
  3. const { DataTypes, Op, Sequelize } = require('@sequelize/core');
  4. const expect = chai.expect;
  5. const Support = require('../support');
  6. describe(Support.getTestDialectTeaser('Model'), () => {
  7. describe('scope', () => {
  8. beforeEach(async function () {
  9. this.ScopeMe = this.sequelize.define(
  10. 'ScopeMe',
  11. {
  12. username: DataTypes.STRING,
  13. email: DataTypes.STRING,
  14. access_level: DataTypes.INTEGER,
  15. other_value: DataTypes.INTEGER,
  16. },
  17. {
  18. scopes: {
  19. lowAccess: {
  20. attributes: ['other_value', 'access_level'],
  21. where: {
  22. access_level: {
  23. [Op.lte]: 5,
  24. },
  25. },
  26. },
  27. withName: {
  28. attributes: ['username'],
  29. },
  30. highAccess: {
  31. where: {
  32. [Op.or]: [{ access_level: { [Op.gte]: 5 } }, { access_level: { [Op.eq]: 10 } }],
  33. },
  34. },
  35. lessThanFour: {
  36. where: {
  37. [Op.and]: [{ access_level: { [Op.lt]: 4 } }],
  38. },
  39. },
  40. issue8473: {
  41. where: {
  42. [Op.or]: {
  43. access_level: 3,
  44. other_value: 10,
  45. },
  46. access_level: 5,
  47. },
  48. },
  49. like_t: {
  50. where: Sequelize.where(
  51. Sequelize.fn('LOWER', Sequelize.col('username')),
  52. Op.like,
  53. '%t%',
  54. ),
  55. },
  56. },
  57. },
  58. );
  59. await this.sequelize.sync({ force: true });
  60. const records = [
  61. { username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
  62. { username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
  63. { username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
  64. { username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 },
  65. ];
  66. await this.ScopeMe.bulkCreate(records);
  67. });
  68. it('should be able to merge attributes as array', async function () {
  69. const record = await this.ScopeMe.withScope('lowAccess', 'withName').findOne();
  70. expect(record.other_value).to.exist;
  71. expect(record.username).to.exist;
  72. expect(record.access_level).to.exist;
  73. });
  74. it('should work with Symbol operators', async function () {
  75. const record = await this.ScopeMe.withScope('highAccess').findOne();
  76. expect(record.username).to.equal('tobi');
  77. const records0 = await this.ScopeMe.withScope('lessThanFour').findAll();
  78. expect(records0).to.have.length(2);
  79. expect(records0[0].get('access_level')).to.equal(3);
  80. expect(records0[1].get('access_level')).to.equal(3);
  81. const records = await this.ScopeMe.withScope('issue8473').findAll();
  82. expect(records).to.have.length(1);
  83. expect(records[0].get('access_level')).to.equal(5);
  84. expect(records[0].get('other_value')).to.equal(10);
  85. });
  86. it('should keep symbols after default assignment', async function () {
  87. const record = await this.ScopeMe.withScope('highAccess').findOne();
  88. expect(record.username).to.equal('tobi');
  89. const records = await this.ScopeMe.withScope('lessThanFour').findAll({
  90. where: {},
  91. });
  92. expect(records).to.have.length(2);
  93. expect(records[0].get('access_level')).to.equal(3);
  94. expect(records[1].get('access_level')).to.equal(3);
  95. await this.ScopeMe.withScope('issue8473').findAll();
  96. });
  97. it('should not throw error with sequelize.where', async function () {
  98. const records = await this.ScopeMe.withScope('like_t').findAll();
  99. expect(records).to.have.length(2);
  100. });
  101. });
  102. });