schema.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var SchemaBuilder = require('../lib/waterline-schema/schema');
  4. describe('Schema Builder :: ', function() {
  5. describe('Validating Identity', function() {
  6. it('should throw an error when a collection is missing an identity', function() {
  7. var collection = function() {};
  8. collection.prototype = {
  9. primaryKey: 'id',
  10. attributes: {
  11. id: {
  12. type: 'number'
  13. }
  14. }
  15. };
  16. assert.throws(
  17. function() {
  18. SchemaBuilder([collection]);
  19. }
  20. );
  21. });
  22. it('should be valid when an identity is present', function() {
  23. var collection = function() {};
  24. collection.prototype = {
  25. identity: 'FOO',
  26. primaryKey: 'id',
  27. attributes: {
  28. id: {
  29. type: 'number'
  30. }
  31. }
  32. };
  33. assert.doesNotThrow(
  34. function() {
  35. SchemaBuilder([collection]);
  36. }
  37. );
  38. });
  39. it('should lowercase the identity', function() {
  40. var collection = function() {};
  41. collection.prototype = {
  42. identity: 'FOO',
  43. primaryKey: 'id',
  44. attributes: {
  45. id: {
  46. type: 'number'
  47. }
  48. }
  49. };
  50. var schema = SchemaBuilder([collection]);
  51. assert(schema.foo);
  52. assert.equal(schema.foo.identity, 'foo');
  53. });
  54. it('should allow tableName to suffice for identity', function() {
  55. var collection = function() {};
  56. collection.prototype = {
  57. tableName: 'foo',
  58. primaryKey: 'id',
  59. attributes: {
  60. id: {
  61. type: 'number'
  62. }
  63. }
  64. };
  65. assert.doesNotThrow(
  66. function() {
  67. SchemaBuilder([collection]);
  68. }
  69. );
  70. });
  71. });
  72. describe('Validating Primary Key', function() {
  73. it('should enforce a primary key attribute to be included', function() {
  74. var collection = function() {};
  75. collection.prototype = {
  76. identity: 'foo',
  77. attributes: {}
  78. };
  79. assert.throws(
  80. function() {
  81. SchemaBuilder([collection]);
  82. }
  83. );
  84. });
  85. it('should allow the primary key to be set as a model option', function() {
  86. var collection = function() {};
  87. collection.prototype = {
  88. identity: 'foo',
  89. primaryKey: 'bar',
  90. attributes: {
  91. bar: {
  92. type: 'string'
  93. }
  94. }
  95. };
  96. assert.doesNotThrow(
  97. function() {
  98. SchemaBuilder([collection]);
  99. }
  100. );
  101. });
  102. it('should NOT allow the primary key to be set as a flag on an attribute', function() {
  103. var collection = function() {};
  104. collection.prototype = {
  105. identity: 'foo',
  106. attributes: {
  107. bar: {
  108. type: 'string'
  109. },
  110. baz: {
  111. type: 'number',
  112. primaryKey: true
  113. }
  114. }
  115. };
  116. assert.throws(
  117. function() {
  118. SchemaBuilder([collection]);
  119. }
  120. );
  121. });
  122. it('should NOT allow the primary key to have a defaultsTo value', function() {
  123. var collection = function() {};
  124. collection.prototype = {
  125. identity: 'foo',
  126. primaryKey: 'baz',
  127. attributes: {
  128. bar: {
  129. type: 'string'
  130. },
  131. baz: {
  132. type: 'number',
  133. defaultsTo: 123
  134. }
  135. }
  136. };
  137. assert.throws(
  138. function() {
  139. SchemaBuilder([collection]);
  140. }
  141. );
  142. });
  143. });
  144. describe('Validating Instance Methods', function() {
  145. it('should not allow instance methods', function() {
  146. var collection = function() {};
  147. collection.prototype = {
  148. identity: 'foo',
  149. primaryKey: 'id',
  150. attributes: {
  151. id: {
  152. type: 'number'
  153. },
  154. getName: function() {}
  155. }
  156. };
  157. assert.throws(
  158. function() {
  159. SchemaBuilder([collection]);
  160. }
  161. );
  162. });
  163. });
  164. describe('Validating Attribute Properties', function() {
  165. it('should not allow migration attributes', function() {
  166. var collection = function() {};
  167. collection.prototype = {
  168. identity: 'foo',
  169. primaryKey: 'id',
  170. attributes: {
  171. id: {
  172. type: 'number',
  173. unique: true
  174. }
  175. }
  176. };
  177. assert.throws(
  178. function() {
  179. SchemaBuilder([collection]);
  180. }
  181. );
  182. });
  183. it('should allow types', function() {
  184. var collection = function() {};
  185. collection.prototype = {
  186. identity: 'foo',
  187. primaryKey: 'id',
  188. attributes: {
  189. id: {
  190. type: 'number'
  191. }
  192. }
  193. };
  194. assert.doesNotThrow(
  195. function() {
  196. SchemaBuilder([collection]);
  197. }
  198. );
  199. });
  200. it('should validate types', function() {
  201. var collection = function() {};
  202. collection.prototype = {
  203. identity: 'foo',
  204. primaryKey: 'id',
  205. attributes: {
  206. id: {
  207. type: 'integer'
  208. }
  209. }
  210. };
  211. assert.throws(
  212. function() {
  213. SchemaBuilder([collection]);
  214. }
  215. );
  216. });
  217. it('should add a required flag', function() {
  218. var collection = function() {};
  219. collection.prototype = {
  220. identity: 'foo',
  221. primaryKey: 'id',
  222. attributes: {
  223. id: {
  224. type: 'number'
  225. },
  226. name: {
  227. type: 'string'
  228. }
  229. }
  230. };
  231. assert.doesNotThrow(
  232. function() {
  233. SchemaBuilder([collection]);
  234. }
  235. );
  236. var schema = SchemaBuilder([collection]);
  237. assert.equal(schema.foo.attributes.name.required, false);
  238. });
  239. it('should prevent attributes from having a null default value', function() {
  240. var collection = function() {};
  241. collection.prototype = {
  242. identity: 'foo',
  243. primaryKey: 'id',
  244. attributes: {
  245. id: {
  246. type: 'number'
  247. },
  248. name: {
  249. type: 'string',
  250. defaultsTo: null
  251. }
  252. }
  253. };
  254. assert.throws(
  255. function() {
  256. SchemaBuilder([collection]);
  257. }
  258. );
  259. });
  260. it('should prevent attributes from having a default value and a required flag', function() {
  261. var collection = function() {};
  262. collection.prototype = {
  263. identity: 'foo',
  264. primaryKey: 'id',
  265. attributes: {
  266. id: {
  267. type: 'number'
  268. },
  269. name: {
  270. type: 'string',
  271. defaultsTo: 'foo',
  272. required: true
  273. }
  274. }
  275. };
  276. assert.throws(
  277. function() {
  278. SchemaBuilder([collection]);
  279. }
  280. );
  281. });
  282. it('should prevent attributes from having a default value that doesn\'t match its type', function() {
  283. var collection = function() {};
  284. collection.prototype = {
  285. identity: 'foo',
  286. primaryKey: 'id',
  287. attributes: {
  288. id: {
  289. type: 'number'
  290. },
  291. name: {
  292. type: 'string',
  293. defaultsTo: 123
  294. }
  295. }
  296. };
  297. assert.throws(
  298. function() {
  299. SchemaBuilder([collection]);
  300. }
  301. );
  302. });
  303. it('should allow attributes to have a default value that does match its type', function() {
  304. var collection = function() {};
  305. collection.prototype = {
  306. identity: 'foo',
  307. primaryKey: 'id',
  308. attributes: {
  309. id: {
  310. type: 'number'
  311. },
  312. name: {
  313. type: 'number',
  314. defaultsTo: 123
  315. }
  316. }
  317. };
  318. assert.doesNotThrow(
  319. function() {
  320. SchemaBuilder([collection]);
  321. }
  322. );
  323. });
  324. it('should not allow associations to have default values', function() {
  325. var collection = function() {};
  326. collection.prototype = {
  327. identity: 'foo',
  328. primaryKey: 'id',
  329. attributes: {
  330. id: {
  331. type: 'number'
  332. },
  333. name: {
  334. model: 'bar',
  335. defaultsTo: 123
  336. }
  337. }
  338. };
  339. assert.throws(
  340. function() {
  341. SchemaBuilder([collection]);
  342. }
  343. );
  344. });
  345. it('should not allow singular associations to have a type', function() {
  346. var collection = function() {};
  347. collection.prototype = {
  348. identity: 'foo',
  349. primaryKey: 'id',
  350. attributes: {
  351. id: {
  352. type: 'number'
  353. },
  354. name: {
  355. model: 'bar',
  356. type: 'string'
  357. }
  358. }
  359. };
  360. assert.throws(
  361. function() {
  362. SchemaBuilder([collection]);
  363. }
  364. );
  365. });
  366. it('should not allow required values to have default values', function() {
  367. var collection = function() {};
  368. collection.prototype = {
  369. identity: 'foo',
  370. primaryKey: 'id',
  371. attributes: {
  372. id: {
  373. type: 'number'
  374. },
  375. name: {
  376. type: 'string',
  377. required: true,
  378. defaultsTo: 'abc'
  379. }
  380. }
  381. };
  382. assert.throws(
  383. function() {
  384. SchemaBuilder([collection]);
  385. }
  386. );
  387. });
  388. it('should not allow plural associations to have a required flag', function() {
  389. var collection = function() {};
  390. collection.prototype = {
  391. identity: 'foo',
  392. primaryKey: 'id',
  393. attributes: {
  394. id: {
  395. type: 'number'
  396. },
  397. name: {
  398. collection: 'bar',
  399. required: true
  400. }
  401. }
  402. };
  403. assert.throws(
  404. function() {
  405. SchemaBuilder([collection]);
  406. }
  407. );
  408. });
  409. it('should not allow plural associations to have a columnName property', function() {
  410. var collection = function() {};
  411. collection.prototype = {
  412. identity: 'foo',
  413. primaryKey: 'id',
  414. attributes: {
  415. id: {
  416. type: 'number'
  417. },
  418. name: {
  419. collection: 'bar',
  420. columnName: 'blah'
  421. }
  422. }
  423. };
  424. assert.throws(
  425. function() {
  426. SchemaBuilder([collection]);
  427. }
  428. );
  429. });
  430. it('should not allow plural associations to point at themselves', function() {
  431. var collection = function() {};
  432. collection.prototype = {
  433. identity: 'foo',
  434. primaryKey: 'id',
  435. attributes: {
  436. id: {
  437. type: 'number'
  438. },
  439. name: {
  440. collection: 'foo',
  441. via: 'name'
  442. }
  443. }
  444. };
  445. assert.throws(
  446. function() {
  447. SchemaBuilder([collection]);
  448. }
  449. );
  450. });
  451. it('should not allow both timestamp flags', function() {
  452. var collection = function() {};
  453. collection.prototype = {
  454. identity: 'foo',
  455. primaryKey: 'id',
  456. attributes: {
  457. id: {
  458. type: 'number'
  459. },
  460. timestamp: {
  461. type: 'number',
  462. autoUpdatedAt: true,
  463. autoCreatedAt: true
  464. }
  465. }
  466. };
  467. assert.throws(
  468. function() {
  469. SchemaBuilder([collection]);
  470. }
  471. );
  472. });
  473. it('should not allow both an autoUpdatedAt timestamp and a required flag', function() {
  474. var collection = function() {};
  475. collection.prototype = {
  476. identity: 'foo',
  477. primaryKey: 'id',
  478. attributes: {
  479. id: {
  480. type: 'number'
  481. },
  482. timestamp: {
  483. type: 'number',
  484. autoUpdatedAt: true,
  485. required: true
  486. }
  487. }
  488. };
  489. assert.throws(
  490. function() {
  491. SchemaBuilder([collection]);
  492. }
  493. );
  494. });
  495. it('should not allow both an autoCreatedAt timestamp and a required flag', function() {
  496. var collection = function() {};
  497. collection.prototype = {
  498. identity: 'foo',
  499. primaryKey: 'id',
  500. attributes: {
  501. id: {
  502. type: 'number'
  503. },
  504. timestamp: {
  505. type: 'number',
  506. autoCreatedAt: true,
  507. required: true
  508. }
  509. }
  510. };
  511. assert.throws(
  512. function() {
  513. SchemaBuilder([collection]);
  514. }
  515. );
  516. });
  517. it('should not allow both an autoCreatedAt timestamp and a defaultsTo value', function() {
  518. var collection = function() {};
  519. collection.prototype = {
  520. identity: 'foo',
  521. primaryKey: 'id',
  522. attributes: {
  523. id: {
  524. type: 'number'
  525. },
  526. timestamp: {
  527. type: 'number',
  528. autoCreatedAt: true,
  529. defaultsTo: 123
  530. }
  531. }
  532. };
  533. assert.throws(
  534. function() {
  535. SchemaBuilder([collection]);
  536. }
  537. );
  538. });
  539. it('should not allow both an autoUpdatedAt timestamp and a defaultsTo value', function() {
  540. var collection = function() {};
  541. collection.prototype = {
  542. identity: 'foo',
  543. primaryKey: 'id',
  544. attributes: {
  545. id: {
  546. type: 'number'
  547. },
  548. timestamp: {
  549. type: 'number',
  550. autoUpdatedAt: true,
  551. defaultsTo: 123
  552. }
  553. }
  554. };
  555. assert.throws(
  556. function() {
  557. SchemaBuilder([collection]);
  558. }
  559. );
  560. });
  561. it('should not allow an autoCreatedAt timestamp to be a json type', function() {
  562. var collection = function() {};
  563. collection.prototype = {
  564. identity: 'foo',
  565. primaryKey: 'id',
  566. attributes: {
  567. id: {
  568. type: 'number'
  569. },
  570. timestamp: {
  571. type: 'json',
  572. autoCreatedAt: true,
  573. }
  574. }
  575. };
  576. assert.throws(
  577. function() {
  578. SchemaBuilder([collection]);
  579. }
  580. );
  581. });
  582. it('should not allow an autoUpdatedAt timestamp to be a json type', function() {
  583. var collection = function() {};
  584. collection.prototype = {
  585. identity: 'foo',
  586. primaryKey: 'id',
  587. attributes: {
  588. id: {
  589. type: 'number'
  590. },
  591. timestamp: {
  592. type: 'json',
  593. autoUpdatedAt: true,
  594. }
  595. }
  596. };
  597. assert.throws(
  598. function() {
  599. SchemaBuilder([collection]);
  600. }
  601. );
  602. });
  603. it('should not allow type json attributes to have an allowNull flag set to true', function() {
  604. var collection = function() {};
  605. collection.prototype = {
  606. identity: 'foo',
  607. primaryKey: 'id',
  608. attributes: {
  609. id: {
  610. type: 'number'
  611. },
  612. data: {
  613. type: 'json',
  614. allowNull: true
  615. }
  616. }
  617. };
  618. assert.throws(
  619. function() {
  620. SchemaBuilder([collection]);
  621. }
  622. );
  623. });
  624. it('should allow type json attributes to have an allowNull flag set to false', function() {
  625. var collection = function() {};
  626. collection.prototype = {
  627. identity: 'foo',
  628. primaryKey: 'id',
  629. attributes: {
  630. id: {
  631. type: 'number'
  632. },
  633. data: {
  634. type: 'json',
  635. allowNull: false
  636. }
  637. }
  638. };
  639. assert.doesNotThrow(
  640. function() {
  641. SchemaBuilder([collection]);
  642. }
  643. );
  644. });
  645. it('should not allow type ref attributes to have an allowNull flag set to true', function() {
  646. var collection = function() {};
  647. collection.prototype = {
  648. identity: 'foo',
  649. primaryKey: 'id',
  650. attributes: {
  651. id: {
  652. type: 'number'
  653. },
  654. data: {
  655. type: 'ref',
  656. allowNull: true
  657. }
  658. }
  659. };
  660. assert.throws(
  661. function() {
  662. SchemaBuilder([collection]);
  663. }
  664. );
  665. });
  666. it('should allow type ref attributes to have an allowNull flag set to false', function() {
  667. var collection = function() {};
  668. collection.prototype = {
  669. identity: 'foo',
  670. primaryKey: 'id',
  671. attributes: {
  672. id: {
  673. type: 'number'
  674. },
  675. data: {
  676. type: 'ref',
  677. allowNull: false
  678. }
  679. }
  680. };
  681. assert.doesNotThrow(
  682. function() {
  683. SchemaBuilder([collection]);
  684. }
  685. );
  686. });
  687. it('should allow type string attributes to have an allowNull flag set to true', function() {
  688. var collection = function() {};
  689. collection.prototype = {
  690. identity: 'foo',
  691. primaryKey: 'id',
  692. attributes: {
  693. id: {
  694. type: 'number'
  695. },
  696. data: {
  697. type: 'string',
  698. allowNull: true
  699. }
  700. }
  701. };
  702. assert.doesNotThrow(
  703. function() {
  704. SchemaBuilder([collection]);
  705. }
  706. );
  707. });
  708. it('should prevent singular associations from having an allowNull flag set to true', function() {
  709. var collection = function() {};
  710. collection.prototype = {
  711. identity: 'foo',
  712. primaryKey: 'id',
  713. attributes: {
  714. id: {
  715. type: 'number'
  716. },
  717. data: {
  718. model: 'foo',
  719. allowNull: true
  720. }
  721. }
  722. };
  723. assert.throws(
  724. function() {
  725. SchemaBuilder([collection]);
  726. }
  727. );
  728. });
  729. it('should prevent plural associations from having an allowNull flag set to true', function() {
  730. var collection = function() {};
  731. collection.prototype = {
  732. identity: 'foo',
  733. primaryKey: 'id',
  734. attributes: {
  735. id: {
  736. type: 'number'
  737. },
  738. data: {
  739. collection: 'foo',
  740. allowNull: true
  741. }
  742. }
  743. };
  744. assert.throws(
  745. function() {
  746. SchemaBuilder([collection]);
  747. }
  748. );
  749. });
  750. it('should prevent the primary key from having an allowNull flag set to true', function() {
  751. var collection = function() {};
  752. collection.prototype = {
  753. identity: 'foo',
  754. primaryKey: 'id',
  755. attributes: {
  756. id: {
  757. type: 'number',
  758. allowNull: true
  759. }
  760. }
  761. };
  762. assert.throws(
  763. function() {
  764. SchemaBuilder([collection]);
  765. }
  766. );
  767. });
  768. it('should prevent an autoCreatedAt timestamp from having an allowNull flag set to true', function() {
  769. var collection = function() {};
  770. collection.prototype = {
  771. identity: 'foo',
  772. primaryKey: 'id',
  773. attributes: {
  774. id: {
  775. type: 'number'
  776. },
  777. createdAt: {
  778. type: 'number',
  779. autoCreatedAt: true,
  780. allowNull: true
  781. }
  782. }
  783. };
  784. assert.throws(
  785. function() {
  786. SchemaBuilder([collection]);
  787. }
  788. );
  789. });
  790. it('should prevent an autoUpdatedAt timestamp from having an allowNull flag set to true', function() {
  791. var collection = function() {};
  792. collection.prototype = {
  793. identity: 'foo',
  794. primaryKey: 'id',
  795. attributes: {
  796. id: {
  797. type: 'number',
  798. },
  799. updatedAt: {
  800. type: 'number',
  801. autoUpdatedAt: true,
  802. allowNull: true
  803. }
  804. }
  805. };
  806. assert.throws(
  807. function() {
  808. SchemaBuilder([collection]);
  809. }
  810. );
  811. });
  812. it('should prevent a required attribute from having an allowNull flag set to true', function() {
  813. var collection = function() {};
  814. collection.prototype = {
  815. identity: 'foo',
  816. primaryKey: 'id',
  817. attributes: {
  818. id: {
  819. type: 'number',
  820. },
  821. importantData: {
  822. type: 'number',
  823. required: true,
  824. allowNull: true
  825. }
  826. }
  827. };
  828. assert.throws(
  829. function() {
  830. SchemaBuilder([collection]);
  831. }
  832. );
  833. });
  834. it('should prevent an attribute with both defaultsTo set to null and an allowNull flag set to true', function() {
  835. var collection = function() {};
  836. collection.prototype = {
  837. identity: 'foo',
  838. primaryKey: 'id',
  839. attributes: {
  840. id: {
  841. type: 'number',
  842. },
  843. importantData: {
  844. type: 'number',
  845. allowNull: true,
  846. defaultsTo: null
  847. }
  848. }
  849. };
  850. assert.throws(
  851. function() {
  852. SchemaBuilder([collection]);
  853. }
  854. );
  855. });
  856. });
  857. describe('Validating Attribute Names', function() {
  858. it('should not allow dots in attribute names', function() {
  859. var collection = function() {};
  860. collection.prototype = {
  861. identity: 'foo',
  862. primaryKey: 'id',
  863. attributes: {
  864. id: {
  865. type: 'number'
  866. },
  867. 'name.foo': {
  868. type: 'number'
  869. }
  870. }
  871. };
  872. assert.throws(
  873. function() {
  874. SchemaBuilder([collection]);
  875. }
  876. );
  877. });
  878. });
  879. describe('Validating Dominant Collection Flag', function() {
  880. it('should error when dominant is used on a via-less association', function() {
  881. var fixtures = [
  882. {
  883. identity: 'user',
  884. primaryKey: 'id',
  885. attributes: {
  886. id: {
  887. type: 'string',
  888. columnName: '_id'
  889. },
  890. favoritePets: {
  891. collection: 'pet',
  892. dominant: true
  893. }
  894. }
  895. },
  896. {
  897. identity: 'pet',
  898. primaryKey: 'id',
  899. attributes: {
  900. id: {
  901. type: 'string',
  902. columnName: '_id'
  903. }
  904. }
  905. },
  906. ];
  907. var collections = _.map(fixtures, function(obj) {
  908. var collection = function() {};
  909. collection.prototype = obj;
  910. return collection;
  911. });
  912. assert.throws(
  913. function() {
  914. SchemaBuilder(collections);
  915. }
  916. );
  917. });
  918. it('should error when dominant is used on a non many-to-many association', function() {
  919. var fixtures = [
  920. {
  921. identity: 'user',
  922. primaryKey: 'id',
  923. attributes: {
  924. id: {
  925. type: 'string',
  926. columnName: '_id'
  927. },
  928. favoritePets: {
  929. collection: 'pet',
  930. via: 'owner',
  931. dominant: true
  932. }
  933. }
  934. },
  935. {
  936. identity: 'pet',
  937. primaryKey: 'id',
  938. attributes: {
  939. id: {
  940. type: 'string',
  941. columnName: '_id'
  942. },
  943. owner: {
  944. model: 'user'
  945. }
  946. }
  947. },
  948. ];
  949. var collections = _.map(fixtures, function(obj) {
  950. var collection = function() {};
  951. collection.prototype = obj;
  952. return collection;
  953. });
  954. assert.throws(
  955. function() {
  956. SchemaBuilder(collections);
  957. }
  958. );
  959. });
  960. it('should NOT error when dominant is used on a many-to-many association', function() {
  961. var fixtures = [
  962. {
  963. identity: 'user',
  964. primaryKey: 'id',
  965. attributes: {
  966. id: {
  967. type: 'string',
  968. columnName: '_id'
  969. },
  970. favoritePets: {
  971. collection: 'pet',
  972. via: 'owner',
  973. dominant: true
  974. }
  975. }
  976. },
  977. {
  978. identity: 'pet',
  979. primaryKey: 'id',
  980. attributes: {
  981. id: {
  982. type: 'string',
  983. columnName: '_id'
  984. },
  985. owners: {
  986. collection: 'user',
  987. via: 'favoritePets'
  988. }
  989. }
  990. },
  991. ];
  992. var collections = _.map(fixtures, function(obj) {
  993. var collection = function() {};
  994. collection.prototype = obj;
  995. return collection;
  996. });
  997. assert.doesNotThrow(
  998. function() {
  999. SchemaBuilder(collections);
  1000. }
  1001. );
  1002. });
  1003. });
  1004. });