|
| 1 | +const Interface = require('forest-express'); |
| 2 | +const { STRING, INTEGER } = require('sequelize'); |
| 3 | +const { Op } = require('sequelize'); |
| 4 | +const databases = require('../databases'); |
| 5 | +const runWithConnection = require('../helpers/run-with-connection'); |
| 6 | +const ResourcesGetter = require('../../src/services/resources-getter'); |
| 7 | + |
| 8 | +function rot13(s) { |
| 9 | + return s.replace(/[A-Z]/gi, (c) => |
| 10 | + 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'[ |
| 11 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.indexOf(c)]); |
| 12 | +} |
| 13 | + |
| 14 | +async function setup(sequelize) { |
| 15 | + Interface.Schemas = { schemas: {} }; |
| 16 | + |
| 17 | + // Shelves |
| 18 | + const Shelves = sequelize.define('shelves', { |
| 19 | + id: { type: INTEGER, primaryKey: true }, |
| 20 | + floor: { field: 'floor', type: INTEGER }, |
| 21 | + }, { tableName: 'shelves', timestamps: false }); |
| 22 | + |
| 23 | + Interface.Schemas.schemas.shelves = { |
| 24 | + name: 'shelves', |
| 25 | + idField: 'id', |
| 26 | + primaryKeys: ['id'], |
| 27 | + isCompositePrimary: false, |
| 28 | + fields: [ |
| 29 | + { field: 'id', type: 'Number' }, |
| 30 | + { field: 'floor', columnName: 'floor', type: 'Number' }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + |
| 34 | + // Books |
| 35 | + const Books = sequelize.define('books', { |
| 36 | + id: { type: INTEGER, primaryKey: true }, |
| 37 | + title: { field: 'title', type: STRING }, |
| 38 | + }, { tableName: 'books', timestamps: false }); |
| 39 | + |
| 40 | + Interface.Schemas.schemas.books = { |
| 41 | + name: 'books', |
| 42 | + idField: 'id', |
| 43 | + primaryKeys: ['id'], |
| 44 | + isCompositePrimary: false, |
| 45 | + fields: [ |
| 46 | + { field: 'id', type: 'Number' }, |
| 47 | + { field: 'title', columnName: 'title', type: 'String' }, |
| 48 | + { |
| 49 | + field: 'encrypted', |
| 50 | + isVirtual: true, |
| 51 | + type: 'String', |
| 52 | + get: (record) => rot13(record.title), |
| 53 | + search: (query, search) => { |
| 54 | + query.where[Op.and][0][Op.or].push({ |
| 55 | + title: rot13(search), |
| 56 | + }); |
| 57 | + }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + |
| 62 | + // Reviews |
| 63 | + const Reviews = sequelize.define('reviews', { |
| 64 | + id: { type: INTEGER, primaryKey: true }, |
| 65 | + content: { field: 'content', type: STRING }, |
| 66 | + }, { tableName: 'reviews', timestamps: false }); |
| 67 | + |
| 68 | + Interface.Schemas.schemas.reviews = { |
| 69 | + name: 'reviews', |
| 70 | + idField: 'id', |
| 71 | + primaryKeys: ['id'], |
| 72 | + isCompositePrimary: false, |
| 73 | + fields: [ |
| 74 | + { field: 'id', type: 'Number' }, |
| 75 | + { field: 'content', columnName: 'content', type: 'String' }, |
| 76 | + { |
| 77 | + field: 'floor', |
| 78 | + isVirtual: true, |
| 79 | + type: 'String', |
| 80 | + get: () => null, |
| 81 | + search: (query, search) => { |
| 82 | + query.include.push({ |
| 83 | + association: 'book', |
| 84 | + include: [{ association: 'shelve' }], |
| 85 | + }); |
| 86 | + |
| 87 | + query.where[Op.and][0][Op.or].push({ |
| 88 | + '$book.shelve.floor$': Number.parseInt(search, 10), |
| 89 | + }); |
| 90 | + }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }; |
| 94 | + |
| 95 | + // Relations |
| 96 | + |
| 97 | + Books.belongsTo(Shelves, { |
| 98 | + foreignKey: { name: 'shelveId' }, |
| 99 | + as: 'shelve', |
| 100 | + }); |
| 101 | + |
| 102 | + Reviews.belongsTo(Books, { |
| 103 | + foreignKey: { name: 'bookId' }, |
| 104 | + as: 'book', |
| 105 | + }); |
| 106 | + |
| 107 | + |
| 108 | + await sequelize.sync({ force: true }); |
| 109 | + |
| 110 | + await Shelves.create({ id: 1, floor: 666 }); |
| 111 | + await Shelves.create({ id: 2, floor: 667 }); |
| 112 | + |
| 113 | + await Books.create({ id: 1, shelveId: 1, title: 'nowhere' }); |
| 114 | + |
| 115 | + await Reviews.create({ id: 1, bookId: 1, content: 'abc' }); |
| 116 | + |
| 117 | + return { Shelves, Books, Reviews }; |
| 118 | +} |
| 119 | + |
| 120 | +describe('integration > Smart field', () => { |
| 121 | + Object.values(databases).forEach((connectionManager) => { |
| 122 | + describe(`dialect ${connectionManager.getDialect()}`, () => { |
| 123 | + it('should not find books matching the encrypted field', async () => { |
| 124 | + expect.assertions(1); |
| 125 | + |
| 126 | + await runWithConnection(connectionManager, async (sequelize) => { |
| 127 | + const { Books } = await setup(sequelize); |
| 128 | + const params = { |
| 129 | + fields: { books: 'id,title,encrypted' }, |
| 130 | + sort: 'id', |
| 131 | + page: { number: '1', size: '30' }, |
| 132 | + timezone: 'Europe/Paris', |
| 133 | + search: 'hello', |
| 134 | + }; |
| 135 | + |
| 136 | + const count = await new ResourcesGetter(Books, null, params).count(); |
| 137 | + expect(count).toStrictEqual(0); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should find books matching the encrypted field', async () => { |
| 142 | + expect.assertions(1); |
| 143 | + |
| 144 | + await runWithConnection(connectionManager, async (sequelize) => { |
| 145 | + const { Books } = await setup(sequelize); |
| 146 | + const params = { |
| 147 | + fields: { books: 'id,title,encrypted' }, |
| 148 | + sort: 'id', |
| 149 | + page: { number: '1', size: '30' }, |
| 150 | + timezone: 'Europe/Paris', |
| 151 | + search: 'abjurer', |
| 152 | + }; |
| 153 | + |
| 154 | + const count = await new ResourcesGetter(Books, null, params).count(); |
| 155 | + expect(count).toStrictEqual(1); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not find reviews on the floor 500', async () => { |
| 160 | + expect.assertions(1); |
| 161 | + |
| 162 | + await runWithConnection(connectionManager, async (sequelize) => { |
| 163 | + const { Reviews } = await setup(sequelize); |
| 164 | + const params = { |
| 165 | + fields: { books: 'id,title,encrypted' }, |
| 166 | + sort: 'id', |
| 167 | + page: { number: '1', size: '30' }, |
| 168 | + timezone: 'Europe/Paris', |
| 169 | + search: '500', |
| 170 | + }; |
| 171 | + |
| 172 | + const count = await new ResourcesGetter(Reviews, null, params).count(); |
| 173 | + expect(count).toStrictEqual(0); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should find reviews on the floor 666', async () => { |
| 178 | + expect.assertions(1); |
| 179 | + |
| 180 | + await runWithConnection(connectionManager, async (sequelize) => { |
| 181 | + const { Reviews } = await setup(sequelize); |
| 182 | + const params = { |
| 183 | + fields: { books: 'id,title,encrypted' }, |
| 184 | + sort: 'id', |
| 185 | + page: { number: '1', size: '30' }, |
| 186 | + timezone: 'Europe/Paris', |
| 187 | + search: '666', |
| 188 | + }; |
| 189 | + |
| 190 | + const count = await new ResourcesGetter(Reviews, null, params).count(); |
| 191 | + expect(count).toStrictEqual(1); |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments