limit.test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes, Op } = require('@sequelize/core');
  6. describe(Support.getTestDialectTeaser('Include'), () => {
  7. describe('LIMIT', () => {
  8. /*
  9. * shortcut for building simple {name: 'foo'} seed data
  10. */
  11. function build(...args) {
  12. return args.map(arg => ({ name: arg }));
  13. }
  14. /*
  15. * association overview
  16. * [Task]N---N[Project]N---N[User]N---N[Hobby]
  17. * 1
  18. * |
  19. * |
  20. * |
  21. * N
  22. * [Comment]N---1[Post]N---N[Tag]N---1[Color]
  23. * 1
  24. * |
  25. * |
  26. * |
  27. * N
  28. * [Footnote]
  29. */
  30. beforeEach(function () {
  31. this.Project = this.sequelize.define(
  32. 'Project',
  33. {
  34. name: {
  35. type: DataTypes.STRING,
  36. primaryKey: true,
  37. },
  38. },
  39. { timestamps: false },
  40. );
  41. this.User = this.sequelize.define(
  42. 'User',
  43. {
  44. name: {
  45. type: DataTypes.STRING,
  46. primaryKey: true,
  47. },
  48. },
  49. { timestamps: false },
  50. );
  51. this.Task = this.sequelize.define(
  52. 'Task',
  53. {
  54. name: {
  55. type: DataTypes.STRING,
  56. primaryKey: true,
  57. },
  58. },
  59. { timestamps: false },
  60. );
  61. this.Hobby = this.sequelize.define(
  62. 'Hobby',
  63. {
  64. name: {
  65. type: DataTypes.STRING,
  66. primaryKey: true,
  67. },
  68. },
  69. { timestamps: false },
  70. );
  71. this.User.belongsToMany(this.Project, { through: 'user_project' });
  72. this.Project.belongsToMany(this.User, { through: 'user_project' });
  73. this.Project.belongsToMany(this.Task, { through: 'task_project' });
  74. this.Task.belongsToMany(this.Project, { through: 'task_project' });
  75. this.User.belongsToMany(this.Hobby, { through: 'user_hobby' });
  76. this.Hobby.belongsToMany(this.User, { through: 'user_hobby' });
  77. this.Post = this.sequelize.define(
  78. 'Post',
  79. {
  80. name: {
  81. type: DataTypes.STRING,
  82. primaryKey: true,
  83. },
  84. },
  85. { timestamps: false },
  86. );
  87. this.Comment = this.sequelize.define(
  88. 'Comment',
  89. {
  90. name: {
  91. type: DataTypes.STRING,
  92. primaryKey: true,
  93. },
  94. },
  95. { timestamps: false },
  96. );
  97. this.Tag = this.sequelize.define(
  98. 'Tag',
  99. {
  100. name: {
  101. type: DataTypes.STRING,
  102. primaryKey: true,
  103. },
  104. },
  105. { timestamps: false },
  106. );
  107. this.Color = this.sequelize.define(
  108. 'Color',
  109. {
  110. name: {
  111. type: DataTypes.STRING,
  112. primaryKey: true,
  113. },
  114. },
  115. { timestamps: false },
  116. );
  117. this.Footnote = this.sequelize.define(
  118. 'Footnote',
  119. {
  120. name: {
  121. type: DataTypes.STRING,
  122. primaryKey: true,
  123. },
  124. },
  125. { timestamps: false },
  126. );
  127. this.Post.hasMany(this.Comment);
  128. this.Comment.belongsTo(this.Post);
  129. this.Post.belongsToMany(this.Tag, { through: 'post_tag' });
  130. this.Tag.belongsToMany(this.Post, { through: 'post_tag' });
  131. this.Post.hasMany(this.Footnote);
  132. this.Footnote.belongsTo(this.Post);
  133. this.User.hasMany(this.Post);
  134. this.Post.belongsTo(this.User);
  135. this.Tag.belongsTo(this.Color);
  136. this.Color.hasMany(this.Tag);
  137. });
  138. /*
  139. * many-to-many
  140. */
  141. it('supports many-to-many association with where clause', async function () {
  142. await this.sequelize.sync({ force: true });
  143. const [projects, users] = await Promise.all([
  144. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  145. this.User.bulkCreate(build('Alice', 'Bob')),
  146. ]);
  147. await Promise.all([
  148. projects[0].addUser(users[0]),
  149. projects[1].addUser(users[1]),
  150. projects[2].addUser(users[0]),
  151. ]);
  152. const result = await this.Project.findAll({
  153. include: [
  154. {
  155. model: this.User,
  156. where: {
  157. name: 'Alice',
  158. },
  159. },
  160. ],
  161. order: ['name'],
  162. limit: 1,
  163. offset: 1,
  164. });
  165. expect(result.length).to.equal(1);
  166. expect(result[0].name).to.equal('charlie');
  167. });
  168. it('supports 2 levels of required many-to-many associations', async function () {
  169. await this.sequelize.sync({ force: true });
  170. const [projects, users, hobbies] = await Promise.all([
  171. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  172. this.User.bulkCreate(build('Alice', 'Bob')),
  173. this.Hobby.bulkCreate(build('archery', 'badminton')),
  174. ]);
  175. await Promise.all([
  176. projects[0].addUser(users[0]),
  177. projects[1].addUser(users[1]),
  178. projects[2].addUser(users[0]),
  179. users[0].addHobby(hobbies[0]),
  180. ]);
  181. const result = await this.Project.findAll({
  182. include: [
  183. {
  184. model: this.User,
  185. required: true,
  186. include: [
  187. {
  188. model: this.Hobby,
  189. required: true,
  190. },
  191. ],
  192. },
  193. ],
  194. order: ['name'],
  195. limit: 1,
  196. offset: 1,
  197. });
  198. expect(result.length).to.equal(1);
  199. expect(result[0].name).to.equal('charlie');
  200. });
  201. it('supports 2 levels of required many-to-many associations with where clause', async function () {
  202. await this.sequelize.sync({ force: true });
  203. const [projects, users, hobbies] = await Promise.all([
  204. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  205. this.User.bulkCreate(build('Alice', 'Bob')),
  206. this.Hobby.bulkCreate(build('archery', 'badminton')),
  207. ]);
  208. await Promise.all([
  209. projects[0].addUser(users[0]),
  210. projects[1].addUser(users[1]),
  211. projects[2].addUser(users[0]),
  212. users[0].addHobby(hobbies[0]),
  213. users[1].addHobby(hobbies[1]),
  214. ]);
  215. const result = await this.Project.findAll({
  216. include: [
  217. {
  218. model: this.User,
  219. required: true,
  220. include: [
  221. {
  222. model: this.Hobby,
  223. where: {
  224. name: 'archery',
  225. },
  226. },
  227. ],
  228. },
  229. ],
  230. order: ['name'],
  231. limit: 1,
  232. offset: 1,
  233. });
  234. expect(result.length).to.equal(1);
  235. expect(result[0].name).to.equal('charlie');
  236. });
  237. it('supports 2 levels of required many-to-many associations with through.where clause', async function () {
  238. await this.sequelize.sync({ force: true });
  239. const [projects, users, hobbies] = await Promise.all([
  240. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  241. this.User.bulkCreate(build('Alice', 'Bob')),
  242. this.Hobby.bulkCreate(build('archery', 'badminton')),
  243. ]);
  244. await Promise.all([
  245. projects[0].addUser(users[0]),
  246. projects[1].addUser(users[1]),
  247. projects[2].addUser(users[0]),
  248. users[0].addHobby(hobbies[0]),
  249. users[1].addHobby(hobbies[1]),
  250. ]);
  251. const result = await this.Project.findAll({
  252. include: [
  253. {
  254. model: this.User,
  255. required: true,
  256. include: [
  257. {
  258. model: this.Hobby,
  259. required: true,
  260. through: {
  261. where: {
  262. hobbyName: 'archery',
  263. },
  264. },
  265. },
  266. ],
  267. },
  268. ],
  269. order: ['name'],
  270. limit: 1,
  271. offset: 1,
  272. });
  273. expect(result.length).to.equal(1);
  274. expect(result[0].name).to.equal('charlie');
  275. });
  276. it('supports 3 levels of required many-to-many associations with where clause', async function () {
  277. await this.sequelize.sync({ force: true });
  278. const [tasks, projects, users, hobbies] = await Promise.all([
  279. this.Task.bulkCreate(build('alpha', 'bravo', 'charlie')),
  280. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  281. this.User.bulkCreate(build('Alice', 'Bob', 'Charlotte')),
  282. this.Hobby.bulkCreate(build('archery', 'badminton')),
  283. ]);
  284. await Promise.all([
  285. tasks[0].addProject(projects[0]),
  286. tasks[1].addProject(projects[1]),
  287. tasks[2].addProject(projects[2]),
  288. projects[0].addUser(users[0]),
  289. projects[1].addUser(users[1]),
  290. projects[2].addUser(users[0]),
  291. users[0].addHobby(hobbies[0]),
  292. users[1].addHobby(hobbies[1]),
  293. ]);
  294. const result = await this.Task.findAll({
  295. include: [
  296. {
  297. model: this.Project,
  298. required: true,
  299. include: [
  300. {
  301. model: this.User,
  302. required: true,
  303. include: [
  304. {
  305. model: this.Hobby,
  306. where: {
  307. name: 'archery',
  308. },
  309. },
  310. ],
  311. },
  312. ],
  313. },
  314. ],
  315. order: ['name'],
  316. limit: 1,
  317. offset: 1,
  318. });
  319. expect(result.length).to.equal(1);
  320. expect(result[0].name).to.equal('charlie');
  321. });
  322. it('supports required many-to-many association', async function () {
  323. await this.sequelize.sync({ force: true });
  324. const [projects, users] = await Promise.all([
  325. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie')),
  326. this.User.bulkCreate(build('Alice', 'Bob')),
  327. ]);
  328. await Promise.all([
  329. // alpha
  330. projects[0].addUser(users[0]), // charlie
  331. projects[2].addUser(users[0]),
  332. ]);
  333. const result = await this.Project.findAll({
  334. include: [
  335. {
  336. model: this.User,
  337. required: true,
  338. },
  339. ],
  340. order: ['name'],
  341. limit: 1,
  342. offset: 1,
  343. });
  344. expect(result.length).to.equal(1);
  345. expect(result[0].name).to.equal('charlie');
  346. });
  347. it('supports 2 required many-to-many association', async function () {
  348. await this.sequelize.sync({ force: true });
  349. const [projects, users, tasks] = await Promise.all([
  350. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie', 'delta')),
  351. this.User.bulkCreate(build('Alice', 'Bob', 'David')),
  352. this.Task.bulkCreate(build('a', 'c', 'd')),
  353. ]);
  354. await Promise.all([
  355. projects[0].addUser(users[0]),
  356. projects[0].addTask(tasks[0]),
  357. projects[1].addUser(users[1]),
  358. projects[2].addTask(tasks[1]),
  359. projects[3].addUser(users[2]),
  360. projects[3].addTask(tasks[2]),
  361. ]);
  362. const result = await this.Project.findAll({
  363. include: [
  364. {
  365. model: this.User,
  366. required: true,
  367. },
  368. {
  369. model: this.Task,
  370. required: true,
  371. },
  372. ],
  373. order: ['name'],
  374. limit: 1,
  375. offset: 1,
  376. });
  377. expect(result.length).to.equal(1);
  378. expect(result[0].name).to.equal('delta');
  379. });
  380. /*
  381. * one-to-many
  382. */
  383. it('supports required one-to-many association', async function () {
  384. await this.sequelize.sync({ force: true });
  385. const [posts, comments] = await Promise.all([
  386. this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')),
  387. this.Comment.bulkCreate(build('comment0', 'comment1')),
  388. ]);
  389. await Promise.all([posts[0].addComment(comments[0]), posts[2].addComment(comments[1])]);
  390. const result = await this.Post.findAll({
  391. include: [
  392. {
  393. model: this.Comment,
  394. required: true,
  395. },
  396. ],
  397. order: ['name'],
  398. limit: 1,
  399. offset: 1,
  400. });
  401. expect(result.length).to.equal(1);
  402. expect(result[0].name).to.equal('charlie');
  403. });
  404. it('supports required one-to-many association with where clause', async function () {
  405. await this.sequelize.sync({ force: true });
  406. const [posts, comments] = await Promise.all([
  407. this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')),
  408. this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2')),
  409. ]);
  410. await Promise.all([
  411. posts[0].addComment(comments[0]),
  412. posts[1].addComment(comments[1]),
  413. posts[2].addComment(comments[2]),
  414. ]);
  415. const result = await this.Post.findAll({
  416. include: [
  417. {
  418. model: this.Comment,
  419. required: true,
  420. where: {
  421. [Op.or]: [
  422. {
  423. name: 'comment0',
  424. },
  425. {
  426. name: 'comment2',
  427. },
  428. ],
  429. },
  430. },
  431. ],
  432. order: ['name'],
  433. limit: 1,
  434. offset: 1,
  435. });
  436. expect(result.length).to.equal(1);
  437. expect(result[0].name).to.equal('charlie');
  438. });
  439. it('supports required one-to-many association with where clause (findOne)', async function () {
  440. await this.sequelize.sync({ force: true });
  441. const [posts, comments] = await Promise.all([
  442. this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')),
  443. this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2')),
  444. ]);
  445. await Promise.all([
  446. posts[0].addComment(comments[0]),
  447. posts[1].addComment(comments[1]),
  448. posts[2].addComment(comments[2]),
  449. ]);
  450. const post = await this.Post.findOne({
  451. include: [
  452. {
  453. model: this.Comment,
  454. required: true,
  455. where: {
  456. name: 'comment2',
  457. },
  458. },
  459. ],
  460. });
  461. expect(post.name).to.equal('charlie');
  462. });
  463. it('supports 2 levels of required one-to-many associations', async function () {
  464. await this.sequelize.sync({ force: true });
  465. const [users, posts, comments] = await Promise.all([
  466. this.User.bulkCreate(build('Alice', 'Bob', 'Charlotte', 'David')),
  467. this.Post.bulkCreate(build('post0', 'post1', 'post2')),
  468. this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2')),
  469. ]);
  470. await Promise.all([
  471. users[0].addPost(posts[0]),
  472. users[1].addPost(posts[1]),
  473. users[3].addPost(posts[2]),
  474. posts[0].addComment(comments[0]),
  475. posts[2].addComment(comments[2]),
  476. ]);
  477. const result = await this.User.findAll({
  478. include: [
  479. {
  480. model: this.Post,
  481. required: true,
  482. include: [
  483. {
  484. model: this.Comment,
  485. required: true,
  486. },
  487. ],
  488. },
  489. ],
  490. order: ['name'],
  491. limit: 1,
  492. offset: 1,
  493. });
  494. expect(result.length).to.equal(1);
  495. expect(result[0].name).to.equal('David');
  496. });
  497. /*
  498. * mixed many-to-many, one-to-many and many-to-one
  499. */
  500. it('supports required one-to-many association with nested required many-to-many association', async function () {
  501. await this.sequelize.sync({ force: true });
  502. const [users, posts, tags] = await Promise.all([
  503. this.User.bulkCreate(build('Alice', 'Bob', 'Charlotte', 'David')),
  504. this.Post.bulkCreate(build('alpha', 'charlie', 'delta')),
  505. this.Tag.bulkCreate(build('atag', 'btag', 'dtag')),
  506. ]);
  507. await Promise.all([
  508. users[0].addPost(posts[0]),
  509. users[2].addPost(posts[1]),
  510. users[3].addPost(posts[2]),
  511. posts[0].addTag([tags[0]]),
  512. posts[2].addTag([tags[2]]),
  513. ]);
  514. const result = await this.User.findAll({
  515. include: [
  516. {
  517. model: this.Post,
  518. required: true,
  519. include: [
  520. {
  521. model: this.Tag,
  522. required: true,
  523. },
  524. ],
  525. },
  526. ],
  527. order: ['name'],
  528. limit: 1,
  529. offset: 1,
  530. });
  531. expect(result.length).to.equal(1);
  532. expect(result[0].name).to.equal('David');
  533. });
  534. it('supports required many-to-many association with nested required one-to-many association', async function () {
  535. await this.sequelize.sync({ force: true });
  536. const [projects, users, posts] = await Promise.all([
  537. this.Project.bulkCreate(build('alpha', 'bravo', 'charlie', 'delta')),
  538. this.User.bulkCreate(build('Alice', 'Bob', 'David')),
  539. this.Post.bulkCreate(build('post0', 'post1', 'post2')),
  540. ]);
  541. await Promise.all([
  542. projects[0].addUser(users[0]),
  543. projects[1].addUser(users[1]),
  544. projects[3].addUser(users[2]),
  545. users[0].addPost([posts[0]]),
  546. users[2].addPost([posts[2]]),
  547. ]);
  548. const result = await this.Project.findAll({
  549. include: [
  550. {
  551. model: this.User,
  552. required: true,
  553. include: [
  554. {
  555. model: this.Post,
  556. required: true,
  557. duplicating: true,
  558. },
  559. ],
  560. },
  561. ],
  562. order: ['name'],
  563. limit: 1,
  564. offset: 1,
  565. });
  566. expect(result.length).to.equal(1);
  567. expect(result[0].name).to.equal('delta');
  568. });
  569. it('supports required many-to-one association with nested many-to-many association with where clause', async function () {
  570. await this.sequelize.sync({ force: true });
  571. const [posts, users, hobbies] = await Promise.all([
  572. this.Post.bulkCreate(build('post0', 'post1', 'post2', 'post3')),
  573. this.User.bulkCreate(build('Alice', 'Bob', 'Charlotte', 'David')),
  574. this.Hobby.bulkCreate(build('archery', 'badminton')),
  575. ]);
  576. await Promise.all([
  577. posts[0].setUser(users[0]),
  578. posts[1].setUser(users[1]),
  579. posts[3].setUser(users[3]),
  580. users[0].addHobby(hobbies[0]),
  581. users[1].addHobby(hobbies[1]),
  582. users[3].addHobby(hobbies[0]),
  583. ]);
  584. const result = await this.Post.findAll({
  585. include: [
  586. {
  587. model: this.User,
  588. required: true,
  589. include: [
  590. {
  591. model: this.Hobby,
  592. where: {
  593. name: 'archery',
  594. },
  595. },
  596. ],
  597. },
  598. ],
  599. order: ['name'],
  600. limit: 1,
  601. offset: 1,
  602. });
  603. expect(result.length).to.equal(1);
  604. expect(result[0].name).to.equal('post3');
  605. });
  606. it('supports required many-to-one association with nested many-to-many association with through.where clause', async function () {
  607. await this.sequelize.sync({ force: true });
  608. const [posts, users, hobbies] = await Promise.all([
  609. this.Post.bulkCreate(build('post0', 'post1', 'post2', 'post3')),
  610. this.User.bulkCreate(build('Alice', 'Bob', 'Charlotte', 'David')),
  611. this.Hobby.bulkCreate(build('archery', 'badminton')),
  612. ]);
  613. await Promise.all([
  614. posts[0].setUser(users[0]),
  615. posts[1].setUser(users[1]),
  616. posts[3].setUser(users[3]),
  617. users[0].addHobby(hobbies[0]),
  618. users[1].addHobby(hobbies[1]),
  619. users[3].addHobby(hobbies[0]),
  620. ]);
  621. const result = await this.Post.findAll({
  622. include: [
  623. {
  624. model: this.User,
  625. required: true,
  626. include: [
  627. {
  628. model: this.Hobby,
  629. required: true,
  630. through: {
  631. where: {
  632. hobbyName: 'archery',
  633. },
  634. },
  635. },
  636. ],
  637. },
  638. ],
  639. order: ['name'],
  640. limit: 1,
  641. offset: 1,
  642. });
  643. expect(result.length).to.equal(1);
  644. expect(result[0].name).to.equal('post3');
  645. });
  646. it('supports required many-to-one association with multiple nested associations with where clause', async function () {
  647. await this.sequelize.sync({ force: true });
  648. const [comments, posts, users, tags] = await Promise.all([
  649. this.Comment.bulkCreate(
  650. build('comment0', 'comment1', 'comment2', 'comment3', 'comment4', 'comment5'),
  651. ),
  652. this.Post.bulkCreate(build('post0', 'post1', 'post2', 'post3', 'post4')),
  653. this.User.bulkCreate(build('Alice', 'Bob')),
  654. this.Tag.bulkCreate(build('tag0', 'tag1')),
  655. ]);
  656. await Promise.all([
  657. comments[0].setPost(posts[0]),
  658. comments[1].setPost(posts[1]),
  659. comments[3].setPost(posts[2]),
  660. comments[4].setPost(posts[3]),
  661. comments[5].setPost(posts[4]),
  662. posts[0].addTag(tags[0]),
  663. posts[3].addTag(tags[0]),
  664. posts[4].addTag(tags[0]),
  665. posts[1].addTag(tags[1]),
  666. posts[0].setUser(users[0]),
  667. posts[2].setUser(users[0]),
  668. posts[4].setUser(users[0]),
  669. posts[1].setUser(users[1]),
  670. ]);
  671. const result = await this.Comment.findAll({
  672. include: [
  673. {
  674. model: this.Post,
  675. required: true,
  676. include: [
  677. {
  678. model: this.User,
  679. where: {
  680. name: 'Alice',
  681. },
  682. },
  683. {
  684. model: this.Tag,
  685. where: {
  686. name: 'tag0',
  687. },
  688. },
  689. ],
  690. },
  691. ],
  692. order: ['name'],
  693. limit: 1,
  694. offset: 1,
  695. });
  696. expect(result.length).to.equal(1);
  697. expect(result[0].name).to.equal('comment5');
  698. });
  699. it('supports required many-to-one association with nested one-to-many association with where clause', async function () {
  700. await this.sequelize.sync({ force: true });
  701. const [comments, posts, footnotes] = await Promise.all([
  702. this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2')),
  703. this.Post.bulkCreate(build('post0', 'post1', 'post2')),
  704. this.Footnote.bulkCreate(build('footnote0', 'footnote1', 'footnote2')),
  705. ]);
  706. await Promise.all([
  707. comments[0].setPost(posts[0]),
  708. comments[1].setPost(posts[1]),
  709. comments[2].setPost(posts[2]),
  710. posts[0].addFootnote(footnotes[0]),
  711. posts[1].addFootnote(footnotes[1]),
  712. posts[2].addFootnote(footnotes[2]),
  713. ]);
  714. const result = await this.Comment.findAll({
  715. include: [
  716. {
  717. model: this.Post,
  718. required: true,
  719. include: [
  720. {
  721. model: this.Footnote,
  722. where: {
  723. [Op.or]: [
  724. {
  725. name: 'footnote0',
  726. },
  727. {
  728. name: 'footnote2',
  729. },
  730. ],
  731. },
  732. },
  733. ],
  734. },
  735. ],
  736. order: ['name'],
  737. limit: 1,
  738. offset: 1,
  739. });
  740. expect(result.length).to.equal(1);
  741. expect(result[0].name).to.equal('comment2');
  742. });
  743. });
  744. });