model-definition-utils.spec.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. import {expect} from 'chai';
  2. import {chai} from '../../chai.js';
  3. import {Schema} from '../../schema.js';
  4. import {format} from '@e22m4u/js-format';
  5. import {DataType} from './properties/index.js';
  6. import {RelationType} from './relations/index.js';
  7. import {EmptyValuesService} from '@e22m4u/js-empty-values';
  8. import {InvalidArgumentError} from '../../errors/index.js';
  9. import {ModelDefinitionUtils} from './model-definition-utils.js';
  10. import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from './model-definition-utils.js';
  11. const sandbox = chai.spy.sandbox();
  12. describe('ModelDefinitionUtils', function () {
  13. afterEach(function () {
  14. sandbox.restore();
  15. });
  16. describe('getPrimaryKeyAsPropertyName', function () {
  17. it('returns a default property name if no primary key defined', function () {
  18. const schema = new Schema();
  19. schema.defineModel({
  20. name: 'model',
  21. });
  22. const result = schema
  23. .getService(ModelDefinitionUtils)
  24. .getPrimaryKeyAsPropertyName('model');
  25. expect(result).to.be.eq(DEF_PK);
  26. });
  27. it('throws an error if a property name of a default primary key already in use as a regular property', function () {
  28. const schema = new Schema();
  29. const S = schema.getService(ModelDefinitionUtils);
  30. sandbox.on(
  31. S,
  32. 'getPropertiesDefinitionInBaseModelHierarchy',
  33. function (modelName) {
  34. expect(modelName).to.be.eq('model');
  35. return {[DEF_PK]: DataType.NUMBER};
  36. },
  37. );
  38. const throwable = () => S.getPrimaryKeyAsPropertyName('model');
  39. expect(throwable).to.throw(
  40. format(
  41. 'The property name %v of the model "model" is defined as a regular property. ' +
  42. 'In this case, a primary key should be defined explicitly. ' +
  43. 'Do use the option "primaryKey" to specify the primary key.',
  44. DEF_PK,
  45. ),
  46. );
  47. });
  48. it('returns a property name if a primary key has a custom name and a default primary key is used as a regular property', function () {
  49. const schema = new Schema();
  50. schema.defineModel({
  51. name: 'model',
  52. properties: {
  53. myId: {
  54. type: DataType.NUMBER,
  55. primaryKey: true,
  56. },
  57. [DEF_PK]: DataType.NUMBER,
  58. },
  59. });
  60. const result = schema
  61. .getService(ModelDefinitionUtils)
  62. .getPrimaryKeyAsPropertyName('model');
  63. expect(result).to.be.eql('myId');
  64. });
  65. it('returns a property name of a primary key', function () {
  66. const schema = new Schema();
  67. schema.defineModel({
  68. name: 'model',
  69. properties: {
  70. foo: {
  71. type: DataType.NUMBER,
  72. primaryKey: true,
  73. },
  74. bar: {
  75. type: DataType.NUMBER,
  76. },
  77. },
  78. });
  79. const result = schema
  80. .getService(ModelDefinitionUtils)
  81. .getPrimaryKeyAsPropertyName('model');
  82. expect(result).to.be.eq('foo');
  83. });
  84. it('uses a base model hierarchy to get a property name of a primary key', function () {
  85. const schema = new Schema();
  86. schema.defineModel({
  87. name: 'modelA',
  88. properties: {
  89. foo: {
  90. type: DataType.STRING,
  91. primaryKey: true,
  92. },
  93. },
  94. });
  95. schema.defineModel({
  96. name: 'modelB',
  97. base: 'modelA',
  98. });
  99. const result = schema
  100. .getService(ModelDefinitionUtils)
  101. .getPrimaryKeyAsPropertyName('modelB');
  102. expect(result).to.be.eq('foo');
  103. });
  104. });
  105. describe('getPrimaryKeyAsColumnName', function () {
  106. it('returns a property name of a primary key if a column name is not specified', function () {
  107. const schema = new Schema();
  108. schema.defineModel({
  109. name: 'model',
  110. properties: {
  111. foo: {
  112. type: DataType.NUMBER,
  113. primaryKey: true,
  114. },
  115. bar: {
  116. type: DataType.NUMBER,
  117. },
  118. },
  119. });
  120. const result = schema
  121. .getService(ModelDefinitionUtils)
  122. .getPrimaryKeyAsColumnName('model');
  123. expect(result).to.be.eq('foo');
  124. });
  125. it('returns a column name of a primary key if specified', function () {
  126. const schema = new Schema();
  127. schema.defineModel({
  128. name: 'model',
  129. properties: {
  130. foo: {
  131. type: DataType.NUMBER,
  132. primaryKey: true,
  133. columnName: 'fooColumn',
  134. },
  135. bar: {
  136. type: DataType.NUMBER,
  137. },
  138. },
  139. });
  140. const result = schema
  141. .getService(ModelDefinitionUtils)
  142. .getPrimaryKeyAsColumnName('model');
  143. expect(result).to.be.eq('fooColumn');
  144. });
  145. it('returns a default property name if a primary key is not defined', function () {
  146. const schema = new Schema();
  147. schema.defineModel({
  148. name: 'model',
  149. });
  150. const result = schema
  151. .getService(ModelDefinitionUtils)
  152. .getPrimaryKeyAsColumnName('model');
  153. expect(result).to.be.eq(DEF_PK);
  154. });
  155. it('throws an error if a property name of a default primary key already in use as a regular property', function () {
  156. const schema = new Schema();
  157. const S = schema.getService(ModelDefinitionUtils);
  158. sandbox.on(
  159. S,
  160. 'getPropertiesDefinitionInBaseModelHierarchy',
  161. function (modelName) {
  162. expect(modelName).to.be.eq('model');
  163. return {[DEF_PK]: DataType.NUMBER};
  164. },
  165. );
  166. const throwable = () => S.getPrimaryKeyAsColumnName('model');
  167. expect(throwable).to.throw(
  168. format(
  169. 'The property name %v of the model "model" is defined as a regular property. ' +
  170. 'In this case, a primary key should be defined explicitly. ' +
  171. 'Do use the option "primaryKey" to specify the primary key.',
  172. DEF_PK,
  173. ),
  174. );
  175. });
  176. it('returns a property name of a custom primary key when a default primary key is used as a regular property', function () {
  177. const schema = new Schema();
  178. schema.defineModel({
  179. name: 'model',
  180. properties: {
  181. myId: {
  182. type: DataType.NUMBER,
  183. primaryKey: true,
  184. },
  185. [DEF_PK]: DataType.NUMBER,
  186. },
  187. });
  188. const result = schema
  189. .getService(ModelDefinitionUtils)
  190. .getPrimaryKeyAsColumnName('model');
  191. expect(result).to.be.eql('myId');
  192. });
  193. it('uses a base model hierarchy to get a column name of a primary key', function () {
  194. const schema = new Schema();
  195. schema.defineModel({
  196. name: 'modelA',
  197. properties: {
  198. foo: {
  199. type: DataType.STRING,
  200. primaryKey: true,
  201. },
  202. },
  203. });
  204. schema.defineModel({
  205. name: 'modelB',
  206. base: 'modelA',
  207. });
  208. const result = schema
  209. .getService(ModelDefinitionUtils)
  210. .getPrimaryKeyAsPropertyName('modelB');
  211. expect(result).to.be.eq('foo');
  212. });
  213. });
  214. describe('getTableNameByModelName', function () {
  215. it('returns a model name if no table name specified', function () {
  216. const schema = new Schema();
  217. schema.defineModel({
  218. name: 'model',
  219. });
  220. const result = schema
  221. .getService(ModelDefinitionUtils)
  222. .getTableNameByModelName('model');
  223. expect(result).to.be.eq('model');
  224. });
  225. it('returns a table name from a model definition', function () {
  226. const schema = new Schema();
  227. schema.defineModel({
  228. name: 'model',
  229. tableName: 'table',
  230. });
  231. const result = schema
  232. .getService(ModelDefinitionUtils)
  233. .getTableNameByModelName('model');
  234. expect(result).to.be.eq('table');
  235. });
  236. });
  237. describe('getColumnNameByPropertyName', function () {
  238. it('returns a property name if a column name is not defined', function () {
  239. const schema = new Schema();
  240. schema.defineModel({
  241. name: 'model',
  242. properties: {
  243. foo: DataType.STRING,
  244. },
  245. });
  246. const result = schema
  247. .getService(ModelDefinitionUtils)
  248. .getColumnNameByPropertyName('model', 'foo');
  249. expect(result).to.be.eq('foo');
  250. });
  251. it('returns a specified column name', function () {
  252. const schema = new Schema();
  253. schema.defineModel({
  254. name: 'model',
  255. properties: {
  256. foo: {
  257. type: DataType.STRING,
  258. columnName: 'bar',
  259. },
  260. },
  261. });
  262. const result = schema
  263. .getService(ModelDefinitionUtils)
  264. .getColumnNameByPropertyName('model', 'foo');
  265. expect(result).to.be.eq('bar');
  266. });
  267. it('throws an error if a given property name does not exist', function () {
  268. const schema = new Schema();
  269. schema.defineModel({
  270. name: 'model',
  271. });
  272. const throwable = () =>
  273. schema
  274. .getService(ModelDefinitionUtils)
  275. .getColumnNameByPropertyName('model', 'foo');
  276. expect(throwable).to.throw(InvalidArgumentError);
  277. });
  278. it('uses a base model hierarchy to get a specified column name', function () {
  279. const schema = new Schema();
  280. schema.defineModel({
  281. name: 'modelA',
  282. properties: {
  283. foo: {
  284. type: DataType.STRING,
  285. columnName: 'fooColumn',
  286. },
  287. },
  288. });
  289. schema.defineModel({
  290. name: 'modelB',
  291. base: 'modelA',
  292. });
  293. const result = schema
  294. .getService(ModelDefinitionUtils)
  295. .getColumnNameByPropertyName('modelB', 'foo');
  296. expect(result).to.be.eq('fooColumn');
  297. });
  298. });
  299. describe('getDefaultPropertyValue', function () {
  300. it('returns undefined if no default value specified', function () {
  301. const schema = new Schema();
  302. schema.defineModel({
  303. name: 'model',
  304. properties: {
  305. foo: DataType.STRING,
  306. },
  307. });
  308. const result = schema
  309. .getService(ModelDefinitionUtils)
  310. .getDefaultPropertyValue('model', 'foo');
  311. expect(result).to.be.undefined;
  312. });
  313. it('returns a default value from a property definition', function () {
  314. const schema = new Schema();
  315. schema.defineModel({
  316. name: 'model',
  317. properties: {
  318. foo: {
  319. type: DataType.STRING,
  320. default: 'default',
  321. },
  322. },
  323. });
  324. const result = schema
  325. .getService(ModelDefinitionUtils)
  326. .getDefaultPropertyValue('model', 'foo');
  327. expect(result).to.be.eq('default');
  328. });
  329. it('returns a value from a factory function', function () {
  330. const schema = new Schema();
  331. schema.defineModel({
  332. name: 'model',
  333. properties: {
  334. foo: {
  335. type: DataType.STRING,
  336. default: () => 'default',
  337. },
  338. },
  339. });
  340. const result = schema
  341. .getService(ModelDefinitionUtils)
  342. .getDefaultPropertyValue('model', 'foo');
  343. expect(result).to.be.eq('default');
  344. });
  345. it('throws an error if a given property name does not exist', function () {
  346. const schema = new Schema();
  347. schema.defineModel({
  348. name: 'model',
  349. });
  350. const throwable = () =>
  351. schema
  352. .getService(ModelDefinitionUtils)
  353. .getDefaultPropertyValue('model', 'foo');
  354. expect(throwable).to.throw(InvalidArgumentError);
  355. });
  356. it('uses a base model hierarchy to get a specified default value', function () {
  357. const schema = new Schema();
  358. schema.defineModel({
  359. name: 'modelA',
  360. properties: {
  361. foo: {
  362. type: DataType.STRING,
  363. default: 'default',
  364. },
  365. },
  366. });
  367. schema.defineModel({
  368. name: 'modelB',
  369. base: 'modelA',
  370. });
  371. const result = schema
  372. .getService(ModelDefinitionUtils)
  373. .getDefaultPropertyValue('modelB', 'foo');
  374. expect(result).to.be.eq('default');
  375. });
  376. });
  377. describe('setDefaultValuesToEmptyProperties', function () {
  378. it('does nothing if no property definitions', function () {
  379. const schema = new Schema();
  380. schema.defineModel({
  381. name: 'model',
  382. });
  383. const result = schema
  384. .getService(ModelDefinitionUtils)
  385. .setDefaultValuesToEmptyProperties('model', {foo: 'string'});
  386. expect(result).to.be.eql({foo: 'string'});
  387. });
  388. it('does nothing if no "default" option in property definition', function () {
  389. const schema = new Schema();
  390. schema.defineModel({
  391. name: 'model',
  392. properties: {
  393. baz: DataType.STRING,
  394. qux: DataType.NUMBER,
  395. },
  396. });
  397. const result = schema
  398. .getService(ModelDefinitionUtils)
  399. .setDefaultValuesToEmptyProperties('model', {foo: 'string'});
  400. expect(result).to.be.eql({foo: 'string'});
  401. });
  402. it('sets a default value if a property does not exist', function () {
  403. const schema = new Schema();
  404. schema.defineModel({
  405. name: 'model',
  406. properties: {
  407. foo: {
  408. type: DataType.STRING,
  409. default: 'string',
  410. },
  411. },
  412. });
  413. const result = schema
  414. .getService(ModelDefinitionUtils)
  415. .setDefaultValuesToEmptyProperties('model', {});
  416. expect(result).to.be.eql({foo: 'string'});
  417. });
  418. it('sets a default value if a property is undefined', function () {
  419. const schema = new Schema();
  420. schema.defineModel({
  421. name: 'model',
  422. properties: {
  423. foo: {
  424. type: DataType.STRING,
  425. default: 'string',
  426. },
  427. },
  428. });
  429. const result = schema
  430. .getService(ModelDefinitionUtils)
  431. .setDefaultValuesToEmptyProperties('model', {foo: undefined});
  432. expect(result).to.be.eql({foo: 'string'});
  433. });
  434. it('sets a default value if a property is null', function () {
  435. const schema = new Schema();
  436. schema.defineModel({
  437. name: 'model',
  438. properties: {
  439. foo: {
  440. type: DataType.STRING,
  441. default: 'string',
  442. },
  443. },
  444. });
  445. const result = schema
  446. .getService(ModelDefinitionUtils)
  447. .setDefaultValuesToEmptyProperties('model', {foo: null});
  448. expect(result).to.be.eql({foo: 'string'});
  449. });
  450. it('sets a default value if a property has an empty value', function () {
  451. const schema = new Schema();
  452. schema.defineModel({
  453. name: 'model',
  454. properties: {
  455. foo: {
  456. type: DataType.STRING,
  457. default: 'placeholder',
  458. },
  459. },
  460. });
  461. schema
  462. .getService(EmptyValuesService)
  463. .setEmptyValuesOf(DataType.STRING, ['empty']);
  464. const result = schema
  465. .getService(ModelDefinitionUtils)
  466. .setDefaultValuesToEmptyProperties('model', {foo: 'empty'});
  467. expect(result).to.be.eql({foo: 'placeholder'});
  468. });
  469. it('sets a value from a factory function', function () {
  470. const schema = new Schema();
  471. schema.defineModel({
  472. name: 'model',
  473. properties: {
  474. foo: {
  475. type: DataType.STRING,
  476. default: () => 'string',
  477. },
  478. bar: {
  479. type: DataType.NUMBER,
  480. default: () => 10,
  481. },
  482. baz: {
  483. type: DataType.STRING,
  484. default: () => null,
  485. },
  486. qux: {
  487. type: DataType.STRING,
  488. default: () => undefined,
  489. },
  490. },
  491. });
  492. const result = schema
  493. .getService(ModelDefinitionUtils)
  494. .setDefaultValuesToEmptyProperties('model', {});
  495. expect(result).to.be.eql({
  496. foo: 'string',
  497. bar: 10,
  498. baz: null,
  499. qux: undefined,
  500. });
  501. });
  502. it('uses a base model hierarchy to set a default values', function () {
  503. const schema = new Schema();
  504. schema.defineModel({
  505. name: 'modelA',
  506. properties: {
  507. foo: {
  508. type: DataType.STRING,
  509. default: 'string',
  510. },
  511. bar: {
  512. type: DataType.NUMBER,
  513. default: 10,
  514. },
  515. baz: {
  516. type: DataType.STRING,
  517. default: null,
  518. },
  519. },
  520. });
  521. schema.defineModel({
  522. name: 'modelB',
  523. base: 'modelA',
  524. });
  525. const result = schema
  526. .getService(ModelDefinitionUtils)
  527. .setDefaultValuesToEmptyProperties('modelB', {});
  528. expect(result).to.be.eql({
  529. foo: 'string',
  530. bar: 10,
  531. baz: null,
  532. });
  533. });
  534. describe('an option "onlyProvidedProperties" is true', function () {
  535. it('does not set a default value if a property does not exist', function () {
  536. const schema = new Schema();
  537. schema.defineModel({
  538. name: 'model',
  539. properties: {
  540. foo: {
  541. type: DataType.STRING,
  542. default: 'string',
  543. },
  544. },
  545. });
  546. const result = schema
  547. .getService(ModelDefinitionUtils)
  548. .setDefaultValuesToEmptyProperties('model', {}, true);
  549. expect(result).to.be.eql({});
  550. });
  551. it('sets a default value if a property is undefined', function () {
  552. const schema = new Schema();
  553. schema.defineModel({
  554. name: 'model',
  555. properties: {
  556. foo: {
  557. type: DataType.STRING,
  558. default: 'string',
  559. },
  560. },
  561. });
  562. const result = schema
  563. .getService(ModelDefinitionUtils)
  564. .setDefaultValuesToEmptyProperties('model', {foo: undefined}, true);
  565. expect(result).to.be.eql({foo: 'string'});
  566. });
  567. it('sets a default value if a property is null', function () {
  568. const schema = new Schema();
  569. schema.defineModel({
  570. name: 'model',
  571. properties: {
  572. foo: {
  573. type: DataType.STRING,
  574. default: 'string',
  575. },
  576. },
  577. });
  578. const result = schema
  579. .getService(ModelDefinitionUtils)
  580. .setDefaultValuesToEmptyProperties('model', {foo: null}, true);
  581. expect(result).to.be.eql({foo: 'string'});
  582. });
  583. });
  584. });
  585. describe('convertPropertyNamesToColumnNames', function () {
  586. it('does nothing if no property definitions', function () {
  587. const schema = new Schema();
  588. schema.defineModel({
  589. name: 'model',
  590. });
  591. const result = schema
  592. .getService(ModelDefinitionUtils)
  593. .convertPropertyNamesToColumnNames('model', {foo: 'string'});
  594. expect(result).to.be.eql({foo: 'string'});
  595. });
  596. it('does nothing if no column name specified', function () {
  597. const schema = new Schema();
  598. schema.defineModel({
  599. name: 'model',
  600. properties: {
  601. foo: DataType.STRING,
  602. bar: DataType.NUMBER,
  603. },
  604. });
  605. const result = schema
  606. .getService(ModelDefinitionUtils)
  607. .convertPropertyNamesToColumnNames('model', {foo: 'string'});
  608. expect(result).to.be.eql({foo: 'string'});
  609. });
  610. it('replaces property names by column names', function () {
  611. const schema = new Schema();
  612. schema.defineModel({
  613. name: 'model',
  614. properties: {
  615. foo: {
  616. type: DataType.STRING,
  617. columnName: 'fooColumn',
  618. },
  619. bar: {
  620. type: DataType.NUMBER,
  621. columnName: 'barColumn',
  622. },
  623. },
  624. });
  625. const result = schema
  626. .getService(ModelDefinitionUtils)
  627. .convertPropertyNamesToColumnNames('model', {foo: 'string'});
  628. expect(result).to.be.eql({fooColumn: 'string'});
  629. });
  630. it('uses a base model hierarchy to replace property names by column names', function () {
  631. const schema = new Schema();
  632. schema.defineModel({
  633. name: 'modelA',
  634. properties: {
  635. foo: {
  636. type: DataType.STRING,
  637. columnName: 'fooColumn',
  638. },
  639. bar: {
  640. type: DataType.NUMBER,
  641. columnName: 'barColumn',
  642. },
  643. },
  644. });
  645. schema.defineModel({
  646. name: 'modelB',
  647. base: 'modelA',
  648. });
  649. const result = schema
  650. .getService(ModelDefinitionUtils)
  651. .convertPropertyNamesToColumnNames('modelB', {foo: 'string'});
  652. expect(result).to.be.eql({fooColumn: 'string'});
  653. });
  654. describe('embedded object with model', function () {
  655. it('does nothing if no property definitions', function () {
  656. const schema = new Schema();
  657. schema.defineModel({
  658. name: 'modelA',
  659. properties: {
  660. embedded: {
  661. type: DataType.OBJECT,
  662. model: 'modelB',
  663. },
  664. },
  665. });
  666. schema.defineModel({
  667. name: 'modelB',
  668. });
  669. const result = schema
  670. .getService(ModelDefinitionUtils)
  671. .convertPropertyNamesToColumnNames('modelA', {
  672. embedded: {foo: 'string'},
  673. });
  674. expect(result).to.be.eql({embedded: {foo: 'string'}});
  675. });
  676. it('does nothing if no column name specified', function () {
  677. const schema = new Schema();
  678. schema.defineModel({
  679. name: 'modelA',
  680. properties: {
  681. embedded: {
  682. type: DataType.OBJECT,
  683. model: 'modelB',
  684. },
  685. },
  686. });
  687. schema.defineModel({
  688. name: 'modelB',
  689. properties: {
  690. foo: DataType.STRING,
  691. bar: DataType.NUMBER,
  692. },
  693. });
  694. const result = schema
  695. .getService(ModelDefinitionUtils)
  696. .convertPropertyNamesToColumnNames('modelA', {
  697. embedded: {foo: 'string', bar: 10},
  698. });
  699. expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
  700. });
  701. it('replaces property names by column names', function () {
  702. const schema = new Schema();
  703. schema.defineModel({
  704. name: 'modelA',
  705. properties: {
  706. embedded: {
  707. type: DataType.OBJECT,
  708. model: 'modelB',
  709. },
  710. },
  711. });
  712. schema.defineModel({
  713. name: 'modelB',
  714. properties: {
  715. foo: {
  716. type: DataType.STRING,
  717. columnName: 'fooColumn',
  718. },
  719. bar: {
  720. type: DataType.NUMBER,
  721. columnName: 'barColumn',
  722. },
  723. },
  724. });
  725. const result = schema
  726. .getService(ModelDefinitionUtils)
  727. .convertPropertyNamesToColumnNames('modelA', {
  728. embedded: {foo: 'string', bar: 10},
  729. });
  730. expect(result).to.be.eql({
  731. embedded: {fooColumn: 'string', barColumn: 10},
  732. });
  733. });
  734. });
  735. describe('embedded array with items model', function () {
  736. it('does nothing if no property definitions', function () {
  737. const schema = new Schema();
  738. schema.defineModel({
  739. name: 'modelA',
  740. properties: {
  741. embedded: {
  742. type: DataType.ARRAY,
  743. itemType: DataType.OBJECT,
  744. itemModel: 'modelB',
  745. },
  746. },
  747. });
  748. schema.defineModel({
  749. name: 'modelB',
  750. });
  751. const result = schema
  752. .getService(ModelDefinitionUtils)
  753. .convertPropertyNamesToColumnNames('modelA', {
  754. embedded: [{foo: 'val'}, {bar: 10}],
  755. });
  756. expect(result).to.be.eql({embedded: [{foo: 'val'}, {bar: 10}]});
  757. });
  758. it('does nothing if no column name specified', function () {
  759. const schema = new Schema();
  760. schema.defineModel({
  761. name: 'modelA',
  762. properties: {
  763. embedded: {
  764. type: DataType.ARRAY,
  765. itemType: DataType.OBJECT,
  766. itemModel: 'modelB',
  767. },
  768. },
  769. });
  770. schema.defineModel({
  771. name: 'modelB',
  772. properties: {
  773. foo: DataType.STRING,
  774. bar: DataType.NUMBER,
  775. },
  776. });
  777. const result = schema
  778. .getService(ModelDefinitionUtils)
  779. .convertPropertyNamesToColumnNames('modelA', {
  780. embedded: [
  781. {foo: 'val1', bar: 10},
  782. {foo: 'val2', bar: 20},
  783. ],
  784. });
  785. expect(result).to.be.eql({
  786. embedded: [
  787. {foo: 'val1', bar: 10},
  788. {foo: 'val2', bar: 20},
  789. ],
  790. });
  791. });
  792. it('replaces property names by column names', function () {
  793. const schema = new Schema();
  794. schema.defineModel({
  795. name: 'modelA',
  796. properties: {
  797. embedded: {
  798. type: DataType.ARRAY,
  799. itemType: DataType.OBJECT,
  800. itemModel: 'modelB',
  801. },
  802. },
  803. });
  804. schema.defineModel({
  805. name: 'modelB',
  806. properties: {
  807. foo: {
  808. type: DataType.STRING,
  809. columnName: 'fooColumn',
  810. },
  811. bar: {
  812. type: DataType.NUMBER,
  813. columnName: 'barColumn',
  814. },
  815. },
  816. });
  817. const result = schema
  818. .getService(ModelDefinitionUtils)
  819. .convertPropertyNamesToColumnNames('modelA', {
  820. embedded: [
  821. {foo: 'val1', bar: 10},
  822. {foo: 'val2', bar: 20},
  823. ],
  824. });
  825. expect(result).to.be.eql({
  826. embedded: [
  827. {fooColumn: 'val1', barColumn: 10},
  828. {fooColumn: 'val2', barColumn: 20},
  829. ],
  830. });
  831. });
  832. });
  833. });
  834. describe('convertColumnNamesToPropertyNames', function () {
  835. it('does nothing if no property definitions', function () {
  836. const schema = new Schema();
  837. schema.defineModel({
  838. name: 'model',
  839. });
  840. const result = schema
  841. .getService(ModelDefinitionUtils)
  842. .convertColumnNamesToPropertyNames('model', {foo: 'string'});
  843. expect(result).to.be.eql({foo: 'string'});
  844. });
  845. it('does nothing if no column name specified', function () {
  846. const schema = new Schema();
  847. schema.defineModel({
  848. name: 'model',
  849. properties: {
  850. foo: DataType.STRING,
  851. bar: DataType.NUMBER,
  852. },
  853. });
  854. const result = schema
  855. .getService(ModelDefinitionUtils)
  856. .convertColumnNamesToPropertyNames('model', {foo: 'string'});
  857. expect(result).to.be.eql({foo: 'string'});
  858. });
  859. it('replaces column names by property names', function () {
  860. const schema = new Schema();
  861. schema.defineModel({
  862. name: 'model',
  863. properties: {
  864. foo: {
  865. type: DataType.STRING,
  866. columnName: 'fooColumn',
  867. },
  868. bar: {
  869. type: DataType.NUMBER,
  870. columnName: 'barColumn',
  871. },
  872. },
  873. });
  874. const result = schema
  875. .getService(ModelDefinitionUtils)
  876. .convertColumnNamesToPropertyNames('model', {fooColumn: 'string'});
  877. expect(result).to.be.eql({foo: 'string'});
  878. });
  879. it('uses a base model hierarchy to replace column names by property names', function () {
  880. const schema = new Schema();
  881. schema.defineModel({
  882. name: 'modelA',
  883. properties: {
  884. foo: {
  885. type: DataType.STRING,
  886. columnName: 'fooColumn',
  887. },
  888. bar: {
  889. type: DataType.NUMBER,
  890. columnName: 'barColumn',
  891. },
  892. },
  893. });
  894. schema.defineModel({
  895. name: 'modelB',
  896. base: 'modelA',
  897. });
  898. const result = schema
  899. .getService(ModelDefinitionUtils)
  900. .convertColumnNamesToPropertyNames('modelA', {fooColumn: 'string'});
  901. expect(result).to.be.eql({foo: 'string'});
  902. });
  903. describe('embedded object with model', function () {
  904. it('does nothing if no property definitions', function () {
  905. const schema = new Schema();
  906. schema.defineModel({
  907. name: 'modelA',
  908. properties: {
  909. embedded: {
  910. type: DataType.OBJECT,
  911. model: 'modelB',
  912. },
  913. },
  914. });
  915. schema.defineModel({
  916. name: 'modelB',
  917. });
  918. const result = schema
  919. .getService(ModelDefinitionUtils)
  920. .convertColumnNamesToPropertyNames('modelA', {
  921. embedded: {foo: 'string'},
  922. });
  923. expect(result).to.be.eql({embedded: {foo: 'string'}});
  924. });
  925. it('does nothing if no column name specified', function () {
  926. const schema = new Schema();
  927. schema.defineModel({
  928. name: 'modelA',
  929. properties: {
  930. embedded: {
  931. type: DataType.OBJECT,
  932. model: 'modelB',
  933. },
  934. },
  935. });
  936. schema.defineModel({
  937. name: 'modelB',
  938. properties: {
  939. foo: DataType.STRING,
  940. bar: DataType.NUMBER,
  941. },
  942. });
  943. const result = schema
  944. .getService(ModelDefinitionUtils)
  945. .convertColumnNamesToPropertyNames('modelA', {
  946. embedded: {foo: 'string', bar: 10},
  947. });
  948. expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
  949. });
  950. it('replaces property names by column names', function () {
  951. const schema = new Schema();
  952. schema.defineModel({
  953. name: 'modelA',
  954. properties: {
  955. embedded: {
  956. type: DataType.OBJECT,
  957. model: 'modelB',
  958. },
  959. },
  960. });
  961. schema.defineModel({
  962. name: 'modelB',
  963. properties: {
  964. foo: {
  965. type: DataType.STRING,
  966. columnName: 'fooColumn',
  967. },
  968. bar: {
  969. type: DataType.NUMBER,
  970. columnName: 'barColumn',
  971. },
  972. },
  973. });
  974. const result = schema
  975. .getService(ModelDefinitionUtils)
  976. .convertColumnNamesToPropertyNames('modelA', {
  977. embedded: {fooColumn: 'string', barColumn: 10},
  978. });
  979. expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
  980. });
  981. });
  982. describe('embedded array with items model', function () {
  983. it('does nothing if no property definitions', function () {
  984. const schema = new Schema();
  985. schema.defineModel({
  986. name: 'modelA',
  987. properties: {
  988. embedded: {
  989. type: DataType.ARRAY,
  990. itemType: DataType.OBJECT,
  991. itemModel: 'modelB',
  992. },
  993. },
  994. });
  995. schema.defineModel({
  996. name: 'modelB',
  997. });
  998. const result = schema
  999. .getService(ModelDefinitionUtils)
  1000. .convertColumnNamesToPropertyNames('modelA', {
  1001. embedded: [{foo: 'val'}, {bar: 10}],
  1002. });
  1003. expect(result).to.be.eql({embedded: [{foo: 'val'}, {bar: 10}]});
  1004. });
  1005. it('does nothing if no column name specified', function () {
  1006. const schema = new Schema();
  1007. schema.defineModel({
  1008. name: 'modelA',
  1009. properties: {
  1010. embedded: {
  1011. type: DataType.ARRAY,
  1012. itemType: DataType.OBJECT,
  1013. itemModel: 'modelB',
  1014. },
  1015. },
  1016. });
  1017. schema.defineModel({
  1018. name: 'modelB',
  1019. properties: {
  1020. foo: DataType.STRING,
  1021. bar: DataType.NUMBER,
  1022. },
  1023. });
  1024. const result = schema
  1025. .getService(ModelDefinitionUtils)
  1026. .convertColumnNamesToPropertyNames('modelA', {
  1027. embedded: [
  1028. {foo: 'val1', bar: 10},
  1029. {foo: 'val2', bar: 20},
  1030. ],
  1031. });
  1032. expect(result).to.be.eql({
  1033. embedded: [
  1034. {foo: 'val1', bar: 10},
  1035. {foo: 'val2', bar: 20},
  1036. ],
  1037. });
  1038. });
  1039. it('replaces property names by column names', function () {
  1040. const schema = new Schema();
  1041. schema.defineModel({
  1042. name: 'modelA',
  1043. properties: {
  1044. embedded: {
  1045. type: DataType.ARRAY,
  1046. itemType: DataType.OBJECT,
  1047. itemModel: 'modelB',
  1048. },
  1049. },
  1050. });
  1051. schema.defineModel({
  1052. name: 'modelB',
  1053. properties: {
  1054. foo: {
  1055. type: DataType.STRING,
  1056. columnName: 'fooColumn',
  1057. },
  1058. bar: {
  1059. type: DataType.NUMBER,
  1060. columnName: 'barColumn',
  1061. },
  1062. },
  1063. });
  1064. const result = schema
  1065. .getService(ModelDefinitionUtils)
  1066. .convertColumnNamesToPropertyNames('modelA', {
  1067. embedded: [
  1068. {fooColumn: 'val1', barColumn: 10},
  1069. {fooColumn: 'val2', barColumn: 20},
  1070. ],
  1071. });
  1072. expect(result).to.be.eql({
  1073. embedded: [
  1074. {foo: 'val1', bar: 10},
  1075. {foo: 'val2', bar: 20},
  1076. ],
  1077. });
  1078. });
  1079. });
  1080. });
  1081. describe('getDataTypeByPropertyName', function () {
  1082. it('returns a property type of a short property definition', function () {
  1083. const schema = new Schema();
  1084. schema.defineModel({
  1085. name: 'model',
  1086. properties: {
  1087. foo: DataType.STRING,
  1088. },
  1089. });
  1090. const result = schema
  1091. .getService(ModelDefinitionUtils)
  1092. .getDataTypeByPropertyName('model', 'foo');
  1093. expect(result).to.be.eq(DataType.STRING);
  1094. });
  1095. it('returns a property type of a full property definition', function () {
  1096. const schema = new Schema();
  1097. schema.defineModel({
  1098. name: 'model',
  1099. properties: {
  1100. foo: {
  1101. type: DataType.STRING,
  1102. },
  1103. },
  1104. });
  1105. const result = schema
  1106. .getService(ModelDefinitionUtils)
  1107. .getDataTypeByPropertyName('model', 'foo');
  1108. expect(result).to.be.eq(DataType.STRING);
  1109. });
  1110. it('throws an error if a property name does not exist', function () {
  1111. const schema = new Schema();
  1112. schema.defineModel({
  1113. name: 'model',
  1114. });
  1115. const throwable = () =>
  1116. schema
  1117. .getService(ModelDefinitionUtils)
  1118. .getDataTypeByPropertyName('model', 'property');
  1119. expect(throwable).to.throw(InvalidArgumentError);
  1120. });
  1121. it('uses a base model hierarchy to get a type from a short property definition', function () {
  1122. const schema = new Schema();
  1123. schema.defineModel({
  1124. name: 'modelA',
  1125. properties: {
  1126. foo: DataType.STRING,
  1127. },
  1128. });
  1129. schema.defineModel({
  1130. name: 'modelB',
  1131. base: 'modelA',
  1132. });
  1133. const result = schema
  1134. .getService(ModelDefinitionUtils)
  1135. .getDataTypeByPropertyName('modelB', 'foo');
  1136. expect(result).to.be.eq(DataType.STRING);
  1137. });
  1138. it('uses a base model hierarchy to get a type from a full property definition', function () {
  1139. const schema = new Schema();
  1140. schema.defineModel({
  1141. name: 'modelA',
  1142. properties: {
  1143. foo: {
  1144. type: DataType.STRING,
  1145. },
  1146. },
  1147. });
  1148. schema.defineModel({
  1149. name: 'modelB',
  1150. base: 'modelA',
  1151. });
  1152. const result = schema
  1153. .getService(ModelDefinitionUtils)
  1154. .getDataTypeByPropertyName('modelB', 'foo');
  1155. expect(result).to.be.eq(DataType.STRING);
  1156. });
  1157. });
  1158. describe('getDataTypeFromPropertyDefinition', function () {
  1159. it('requires the given argument "propDef" must be an Object or DataType', function () {
  1160. const schema = new Schema();
  1161. const S = schema.getService(ModelDefinitionUtils);
  1162. const throwable = v => () => S.getDataTypeFromPropertyDefinition(v);
  1163. const error = v =>
  1164. format(
  1165. 'The argument "propDef" of the ModelDefinitionUtils.getDataTypeFromPropertyDefinition ' +
  1166. 'should be an Object or the DataType enum, but %s given.',
  1167. v,
  1168. );
  1169. expect(throwable('str')).to.throw(error('"str"'));
  1170. expect(throwable('')).to.throw(error('""'));
  1171. expect(throwable(10)).to.throw(error('10'));
  1172. expect(throwable(0)).to.throw(error('0'));
  1173. expect(throwable(true)).to.throw(error('true'));
  1174. expect(throwable(false)).to.throw(error('false'));
  1175. expect(throwable(undefined)).to.throw(error('undefined'));
  1176. expect(throwable(null)).to.throw(error('null'));
  1177. throwable(DataType.ANY)();
  1178. throwable({type: DataType.ANY})();
  1179. });
  1180. it('requires the given Object to have the "type" property with the DataType enum', function () {
  1181. const schema = new Schema();
  1182. const S = schema.getService(ModelDefinitionUtils);
  1183. const throwable = v => () =>
  1184. S.getDataTypeFromPropertyDefinition({type: v});
  1185. const error = v =>
  1186. format(
  1187. 'The given Object to the ModelDefinitionUtils.getDataTypeFromPropertyDefinition ' +
  1188. 'should have the "type" property with one of values: %l, but %s given.',
  1189. Object.values(DataType),
  1190. v,
  1191. );
  1192. expect(throwable('str')).to.throw(error('"str"'));
  1193. expect(throwable('')).to.throw(error('""'));
  1194. expect(throwable(10)).to.throw(error('10'));
  1195. expect(throwable(0)).to.throw(error('0'));
  1196. expect(throwable(true)).to.throw(error('true'));
  1197. expect(throwable(false)).to.throw(error('false'));
  1198. expect(throwable(undefined)).to.throw(error('undefined'));
  1199. expect(throwable(null)).to.throw(error('null'));
  1200. throwable(DataType.ANY)();
  1201. });
  1202. it('returns the DataType from the given DataType enum', function () {
  1203. const schema = new Schema();
  1204. const S = schema.getService(ModelDefinitionUtils);
  1205. const res = S.getDataTypeFromPropertyDefinition(DataType.STRING);
  1206. expect(res).to.be.eq(DataType.STRING);
  1207. });
  1208. it('returns the DataType from the given PropertyDefinition', function () {
  1209. const schema = new Schema();
  1210. const S = schema.getService(ModelDefinitionUtils);
  1211. const res = S.getDataTypeFromPropertyDefinition({type: DataType.STRING});
  1212. expect(res).to.be.eq(DataType.STRING);
  1213. });
  1214. });
  1215. describe('getOwnPropertiesDefinitionWithoutPrimaryKeys', function () {
  1216. it('returns an empty object if a model does not have properties', function () {
  1217. const schema = new Schema();
  1218. schema.defineModel({
  1219. name: 'model',
  1220. });
  1221. const result = schema
  1222. .getService(ModelDefinitionUtils)
  1223. .getOwnPropertiesDefinitionWithoutPrimaryKeys('model');
  1224. expect(result).to.be.eql({});
  1225. });
  1226. it('returns a properties definition without primary keys', function () {
  1227. const schema = new Schema();
  1228. schema.defineModel({
  1229. name: 'model',
  1230. properties: {
  1231. id: {
  1232. type: DataType.STRING,
  1233. primaryKey: true,
  1234. },
  1235. foo: DataType.STRING,
  1236. bar: DataType.STRING,
  1237. },
  1238. });
  1239. const result = schema
  1240. .getService(ModelDefinitionUtils)
  1241. .getOwnPropertiesDefinitionWithoutPrimaryKeys('model');
  1242. expect(result).to.be.eql({
  1243. foo: DataType.STRING,
  1244. bar: DataType.STRING,
  1245. });
  1246. });
  1247. it('returns its own properties definition even it has a base model properties', function () {
  1248. const schema = new Schema();
  1249. schema.defineModel({
  1250. name: 'modelA',
  1251. properties: {
  1252. id: {
  1253. type: DataType.STRING,
  1254. primaryKey: true,
  1255. },
  1256. foo: DataType.STRING,
  1257. bar: DataType.STRING,
  1258. },
  1259. });
  1260. schema.defineModel({
  1261. name: 'modelB',
  1262. base: 'modelA',
  1263. properties: {
  1264. id: {
  1265. type: DataType.NUMBER,
  1266. primaryKey: true,
  1267. },
  1268. foo: DataType.NUMBER,
  1269. },
  1270. });
  1271. const result = schema
  1272. .getService(ModelDefinitionUtils)
  1273. .getOwnPropertiesDefinitionWithoutPrimaryKeys('modelB');
  1274. expect(result).to.be.eql({
  1275. foo: DataType.NUMBER,
  1276. });
  1277. });
  1278. });
  1279. describe('getOwnPropertiesDefinitionOfPrimaryKeys', function () {
  1280. it('returns an empty object if a model does not have properties', function () {
  1281. const schema = new Schema();
  1282. schema.defineModel({
  1283. name: 'model',
  1284. });
  1285. const result = schema
  1286. .getService(ModelDefinitionUtils)
  1287. .getOwnPropertiesDefinitionOfPrimaryKeys('model');
  1288. expect(result).to.be.eql({});
  1289. });
  1290. it('returns a properties definition of primary keys', function () {
  1291. const schema = new Schema();
  1292. schema.defineModel({
  1293. name: 'model',
  1294. properties: {
  1295. id: {
  1296. type: DataType.STRING,
  1297. primaryKey: true,
  1298. },
  1299. foo: DataType.STRING,
  1300. bar: DataType.NUMBER,
  1301. },
  1302. });
  1303. const result = schema
  1304. .getService(ModelDefinitionUtils)
  1305. .getOwnPropertiesDefinitionOfPrimaryKeys('model');
  1306. expect(result).to.be.eql({
  1307. id: {
  1308. type: DataType.STRING,
  1309. primaryKey: true,
  1310. },
  1311. });
  1312. });
  1313. it('returns its own properties definition even it has a base model properties', function () {
  1314. const schema = new Schema();
  1315. schema.defineModel({
  1316. name: 'modelA',
  1317. properties: {
  1318. id: {
  1319. type: DataType.STRING,
  1320. primaryKey: true,
  1321. },
  1322. foo: DataType.STRING,
  1323. bar: DataType.STRING,
  1324. },
  1325. });
  1326. schema.defineModel({
  1327. name: 'modelB',
  1328. base: 'modelA',
  1329. properties: {
  1330. id: {
  1331. type: DataType.NUMBER,
  1332. primaryKey: true,
  1333. },
  1334. foo: DataType.NUMBER,
  1335. },
  1336. });
  1337. const result = schema
  1338. .getService(ModelDefinitionUtils)
  1339. .getOwnPropertiesDefinitionOfPrimaryKeys('modelB');
  1340. expect(result).to.be.eql({
  1341. id: {
  1342. type: DataType.NUMBER,
  1343. primaryKey: true,
  1344. },
  1345. });
  1346. });
  1347. });
  1348. describe('getPropertiesDefinitionInBaseModelHierarchy', function () {
  1349. it('returns an empty object if a model does not have properties', function () {
  1350. const schema = new Schema();
  1351. schema.defineModel({
  1352. name: 'model',
  1353. });
  1354. const result = schema
  1355. .getService(ModelDefinitionUtils)
  1356. .getPropertiesDefinitionInBaseModelHierarchy('model');
  1357. expect(result).to.be.eql({});
  1358. });
  1359. it('returns a properties definition of a model', function () {
  1360. const schema = new Schema();
  1361. schema.defineModel({
  1362. name: 'model',
  1363. properties: {
  1364. id: {
  1365. type: DataType.STRING,
  1366. primaryKey: true,
  1367. },
  1368. foo: DataType.STRING,
  1369. bar: DataType.NUMBER,
  1370. },
  1371. });
  1372. const result = schema
  1373. .getService(ModelDefinitionUtils)
  1374. .getPropertiesDefinitionInBaseModelHierarchy('model');
  1375. expect(result).to.be.eql({
  1376. id: {
  1377. type: DataType.STRING,
  1378. primaryKey: true,
  1379. },
  1380. foo: DataType.STRING,
  1381. bar: DataType.NUMBER,
  1382. });
  1383. });
  1384. it('returns a properties definition of an extended model', function () {
  1385. const schema = new Schema();
  1386. schema.defineModel({
  1387. name: 'modelA',
  1388. properties: {
  1389. foo: DataType.STRING,
  1390. },
  1391. });
  1392. schema.defineModel({
  1393. name: 'modelB',
  1394. base: 'modelA',
  1395. properties: {
  1396. bar: DataType.NUMBER,
  1397. },
  1398. });
  1399. const result = schema
  1400. .getService(ModelDefinitionUtils)
  1401. .getPropertiesDefinitionInBaseModelHierarchy('modelB');
  1402. expect(result).to.be.eql({
  1403. foo: DataType.STRING,
  1404. bar: DataType.NUMBER,
  1405. });
  1406. });
  1407. it('uses child properties in priority over a base model properties', function () {
  1408. const schema = new Schema();
  1409. schema.defineModel({
  1410. name: 'modelA',
  1411. properties: {
  1412. id: {
  1413. type: DataType.STRING,
  1414. primaryKey: true,
  1415. },
  1416. foo: DataType.STRING,
  1417. bar: DataType.STRING,
  1418. },
  1419. });
  1420. schema.defineModel({
  1421. name: 'modelB',
  1422. base: 'modelA',
  1423. properties: {
  1424. id: {
  1425. type: DataType.NUMBER,
  1426. primaryKey: true,
  1427. },
  1428. foo: DataType.NUMBER,
  1429. },
  1430. });
  1431. const result = schema
  1432. .getService(ModelDefinitionUtils)
  1433. .getPropertiesDefinitionInBaseModelHierarchy('modelB');
  1434. expect(result).to.be.eql({
  1435. id: {
  1436. type: DataType.NUMBER,
  1437. primaryKey: true,
  1438. },
  1439. foo: DataType.NUMBER,
  1440. bar: DataType.STRING,
  1441. });
  1442. });
  1443. it('uses primary keys from a model closest to child model', function () {
  1444. const schema = new Schema();
  1445. schema.defineModel({
  1446. name: 'modelA',
  1447. properties: {
  1448. id1: {
  1449. type: DataType.STRING,
  1450. primaryKey: true,
  1451. },
  1452. foo: DataType.STRING,
  1453. },
  1454. });
  1455. schema.defineModel({
  1456. name: 'modelB',
  1457. base: 'modelA',
  1458. properties: {
  1459. id2: {
  1460. type: DataType.STRING,
  1461. primaryKey: true,
  1462. },
  1463. bar: DataType.NUMBER,
  1464. },
  1465. });
  1466. const result = schema
  1467. .getService(ModelDefinitionUtils)
  1468. .getPropertiesDefinitionInBaseModelHierarchy('modelB');
  1469. expect(result).to.be.eql({
  1470. id2: {
  1471. type: DataType.STRING,
  1472. primaryKey: true,
  1473. },
  1474. foo: DataType.STRING,
  1475. bar: DataType.NUMBER,
  1476. });
  1477. });
  1478. it('throws an error for a circular reference', function () {
  1479. const schema = new Schema();
  1480. schema.defineModel({
  1481. name: 'model',
  1482. base: 'model',
  1483. });
  1484. const throwable = () =>
  1485. schema
  1486. .getService(ModelDefinitionUtils)
  1487. .getPropertiesDefinitionInBaseModelHierarchy('model');
  1488. expect(throwable).to.throw(
  1489. 'The model "model" has a circular inheritance.',
  1490. );
  1491. });
  1492. });
  1493. describe('getOwnRelationsDefinition', function () {
  1494. it('returns an empty object if a model does not have relations', function () {
  1495. const schema = new Schema();
  1496. schema.defineModel({
  1497. name: 'model',
  1498. });
  1499. const result = schema
  1500. .getService(ModelDefinitionUtils)
  1501. .getOwnRelationsDefinition('model');
  1502. expect(result).to.be.eql({});
  1503. });
  1504. it('returns a relations definition by a given model', function () {
  1505. const schema = new Schema();
  1506. schema.defineModel({
  1507. name: 'model',
  1508. relations: {
  1509. foo: {
  1510. type: RelationType.BELONGS_TO,
  1511. model: 'model',
  1512. },
  1513. },
  1514. });
  1515. const result = schema
  1516. .getService(ModelDefinitionUtils)
  1517. .getOwnRelationsDefinition('model');
  1518. expect(result).to.be.eql({
  1519. foo: {
  1520. type: RelationType.BELONGS_TO,
  1521. model: 'model',
  1522. },
  1523. });
  1524. });
  1525. it('returns its own relations definition even it has a base model relations', function () {
  1526. const schema = new Schema();
  1527. schema.defineModel({
  1528. name: 'modelA',
  1529. relations: {
  1530. foo: {
  1531. type: RelationType.BELONGS_TO,
  1532. model: 'modelA',
  1533. },
  1534. },
  1535. });
  1536. schema.defineModel({
  1537. name: 'modelB',
  1538. base: 'modelA',
  1539. relations: {
  1540. bar: {
  1541. type: RelationType.BELONGS_TO,
  1542. model: 'modelB',
  1543. },
  1544. },
  1545. });
  1546. const result = schema
  1547. .getService(ModelDefinitionUtils)
  1548. .getOwnRelationsDefinition('modelB');
  1549. expect(result).to.be.eql({
  1550. bar: {
  1551. type: RelationType.BELONGS_TO,
  1552. model: 'modelB',
  1553. },
  1554. });
  1555. });
  1556. });
  1557. describe('getRelationsDefinitionInBaseModelHierarchy', function () {
  1558. it('returns an empty object if a model does not have relations', function () {
  1559. const schema = new Schema();
  1560. schema.defineModel({
  1561. name: 'model',
  1562. });
  1563. const result = schema
  1564. .getService(ModelDefinitionUtils)
  1565. .getRelationsDefinitionInBaseModelHierarchy('model');
  1566. expect(result).to.be.eql({});
  1567. });
  1568. it('returns a relations definition of a model', function () {
  1569. const schema = new Schema();
  1570. schema.defineModel({
  1571. name: 'model',
  1572. relations: {
  1573. foo: {
  1574. type: RelationType.BELONGS_TO,
  1575. model: 'model',
  1576. },
  1577. bar: {
  1578. type: RelationType.BELONGS_TO,
  1579. model: 'model',
  1580. },
  1581. },
  1582. });
  1583. const result = schema
  1584. .getService(ModelDefinitionUtils)
  1585. .getRelationsDefinitionInBaseModelHierarchy('model');
  1586. expect(result).to.be.eql({
  1587. foo: {
  1588. type: RelationType.BELONGS_TO,
  1589. model: 'model',
  1590. },
  1591. bar: {
  1592. type: RelationType.BELONGS_TO,
  1593. model: 'model',
  1594. },
  1595. });
  1596. });
  1597. it('returns a relations definition of an extended model', function () {
  1598. const schema = new Schema();
  1599. schema.defineModel({
  1600. name: 'modelA',
  1601. relations: {
  1602. foo: {
  1603. type: RelationType.BELONGS_TO,
  1604. model: 'modelA',
  1605. },
  1606. },
  1607. });
  1608. schema.defineModel({
  1609. name: 'modelB',
  1610. base: 'modelA',
  1611. relations: {
  1612. bar: {
  1613. type: RelationType.BELONGS_TO,
  1614. model: 'modelB',
  1615. },
  1616. },
  1617. });
  1618. const result = schema
  1619. .getService(ModelDefinitionUtils)
  1620. .getRelationsDefinitionInBaseModelHierarchy('modelB');
  1621. expect(result).to.be.eql({
  1622. foo: {
  1623. type: RelationType.BELONGS_TO,
  1624. model: 'modelA',
  1625. },
  1626. bar: {
  1627. type: RelationType.BELONGS_TO,
  1628. model: 'modelB',
  1629. },
  1630. });
  1631. });
  1632. it('uses child relations in priority over a base model relations', function () {
  1633. const schema = new Schema();
  1634. schema.defineModel({
  1635. name: 'modelA',
  1636. relations: {
  1637. foo: {
  1638. type: RelationType.BELONGS_TO,
  1639. model: 'modelA',
  1640. },
  1641. bar: {
  1642. type: RelationType.BELONGS_TO,
  1643. model: 'modelA',
  1644. },
  1645. },
  1646. });
  1647. schema.defineModel({
  1648. name: 'modelB',
  1649. base: 'modelA',
  1650. relations: {
  1651. foo: {
  1652. type: RelationType.REFERENCES_MANY,
  1653. model: 'modelB',
  1654. },
  1655. },
  1656. });
  1657. const result = schema
  1658. .getService(ModelDefinitionUtils)
  1659. .getRelationsDefinitionInBaseModelHierarchy('modelB');
  1660. expect(result).to.be.eql({
  1661. foo: {
  1662. type: RelationType.REFERENCES_MANY,
  1663. model: 'modelB',
  1664. },
  1665. bar: {
  1666. type: RelationType.BELONGS_TO,
  1667. model: 'modelA',
  1668. },
  1669. });
  1670. });
  1671. it('throws an error for a circular reference', function () {
  1672. const schema = new Schema();
  1673. schema.defineModel({
  1674. name: 'model',
  1675. base: 'model',
  1676. });
  1677. const throwable = () =>
  1678. schema
  1679. .getService(ModelDefinitionUtils)
  1680. .getRelationsDefinitionInBaseModelHierarchy('model');
  1681. expect(throwable).to.throw(
  1682. 'The model "model" has a circular inheritance.',
  1683. );
  1684. });
  1685. });
  1686. describe('getRelationDefinitionByName', function () {
  1687. it('throws an error if a given model is not found', function () {
  1688. const schema = new Schema();
  1689. const throwable = () =>
  1690. schema
  1691. .getService(ModelDefinitionUtils)
  1692. .getRelationDefinitionByName('model', 'myRelation');
  1693. expect(throwable).to.throw('The model "model" is not defined.');
  1694. });
  1695. it('throws an error if a given relation is not found', function () {
  1696. const schema = new Schema();
  1697. schema.defineModel({
  1698. name: 'model',
  1699. });
  1700. const throwable = () =>
  1701. schema
  1702. .getService(ModelDefinitionUtils)
  1703. .getRelationDefinitionByName('model', 'myRelation');
  1704. expect(throwable).to.throw(
  1705. 'The model "model" does not have relation name "myRelation".',
  1706. );
  1707. });
  1708. it('returns a relation definition by a given name', function () {
  1709. const schema = new Schema();
  1710. schema.defineModel({
  1711. name: 'model',
  1712. relations: {
  1713. myRelation: {
  1714. type: RelationType.BELONGS_TO,
  1715. model: 'model',
  1716. },
  1717. },
  1718. });
  1719. const result = schema
  1720. .getService(ModelDefinitionUtils)
  1721. .getRelationDefinitionByName('model', 'myRelation');
  1722. expect(result).to.be.eql({
  1723. type: RelationType.BELONGS_TO,
  1724. model: 'model',
  1725. });
  1726. });
  1727. it('uses a child relations in priority over a base model relations', function () {
  1728. const schema = new Schema();
  1729. schema.defineModel({
  1730. name: 'modelA',
  1731. relations: {
  1732. myRelation: {
  1733. type: RelationType.BELONGS_TO,
  1734. model: 'modelA',
  1735. },
  1736. },
  1737. });
  1738. schema.defineModel({
  1739. name: 'modelB',
  1740. base: 'modelA',
  1741. relations: {
  1742. myRelation: {
  1743. type: RelationType.REFERENCES_MANY,
  1744. model: 'modelA',
  1745. },
  1746. },
  1747. });
  1748. const result = schema
  1749. .getService(ModelDefinitionUtils)
  1750. .getRelationDefinitionByName('modelB', 'myRelation');
  1751. expect(result).to.be.eql({
  1752. type: RelationType.REFERENCES_MANY,
  1753. model: 'modelA',
  1754. });
  1755. });
  1756. it('returns a base model relation if a given relation name is not found in a child model', function () {
  1757. const schema = new Schema();
  1758. schema.defineModel({
  1759. name: 'modelA',
  1760. relations: {
  1761. myRelation: {
  1762. type: RelationType.BELONGS_TO,
  1763. model: 'modelA',
  1764. },
  1765. },
  1766. });
  1767. schema.defineModel({
  1768. name: 'modelB',
  1769. base: 'modelA',
  1770. });
  1771. const result = schema
  1772. .getService(ModelDefinitionUtils)
  1773. .getRelationDefinitionByName('modelB', 'myRelation');
  1774. expect(result).to.be.eql({
  1775. type: RelationType.BELONGS_TO,
  1776. model: 'modelA',
  1777. });
  1778. });
  1779. });
  1780. describe('excludeObjectKeysByRelationNames', function () {
  1781. it('excludes object keys by relation names', function () {
  1782. const schema = new Schema();
  1783. schema.defineModel({
  1784. name: 'model',
  1785. relations: {
  1786. baz: {
  1787. type: RelationType.BELONGS_TO,
  1788. model: 'model',
  1789. },
  1790. qux: {
  1791. type: RelationType.BELONGS_TO,
  1792. model: 'model',
  1793. },
  1794. },
  1795. });
  1796. const input = {
  1797. foo: 'fooVal',
  1798. bar: {val: 'barVal'},
  1799. baz: 'bazVal',
  1800. qux: {val: 'quxVal'},
  1801. };
  1802. const result = schema
  1803. .getService(ModelDefinitionUtils)
  1804. .excludeObjectKeysByRelationNames('model', input);
  1805. expect(result).to.be.eql({foo: 'fooVal', bar: {val: 'barVal'}});
  1806. expect(result).to.be.not.eq(input);
  1807. });
  1808. it('requires a given object as an object', function () {
  1809. const schema = new Schema();
  1810. schema.defineModel({name: 'model'});
  1811. const throwable = v => () =>
  1812. schema
  1813. .getService(ModelDefinitionUtils)
  1814. .excludeObjectKeysByRelationNames('model', v);
  1815. const error = v =>
  1816. format(
  1817. 'The second argument of ModelDefinitionUtils.excludeObjectKeysByRelationNames ' +
  1818. 'should be an Object, but %s given.',
  1819. v,
  1820. );
  1821. expect(throwable('')).to.throw(error('""'));
  1822. expect(throwable('str')).to.throw(error('"str"'));
  1823. expect(throwable(10)).to.throw(error('10'));
  1824. expect(throwable(true)).to.throw(error('true'));
  1825. expect(throwable(false)).to.throw(error('false'));
  1826. expect(throwable([])).to.throw(error('Array'));
  1827. expect(throwable(undefined)).to.throw(error('undefined'));
  1828. expect(throwable(null)).to.throw(error('null'));
  1829. throwable({})();
  1830. throwable({foo: 'bar'})();
  1831. });
  1832. });
  1833. describe('getModelNameOfPropertyValueIfDefined', function () {
  1834. it('returns undefined if a given property does not exist in the model', function () {
  1835. const schema = new Schema();
  1836. schema.defineModel({name: 'model'});
  1837. const S = schema.getService(ModelDefinitionUtils);
  1838. const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  1839. expect(res).to.be.undefined;
  1840. });
  1841. describe('short property definition', function () {
  1842. it('requires parameter "modelName" to be a non-empty String', function () {
  1843. const schema = new Schema();
  1844. schema.defineModel({
  1845. name: 'model',
  1846. properties: {
  1847. foo: DataType.OBJECT,
  1848. },
  1849. });
  1850. const S = schema.getService(ModelDefinitionUtils);
  1851. const throwable = v => () =>
  1852. S.getModelNameOfPropertyValueIfDefined(v, 'foo');
  1853. const error = v =>
  1854. format(
  1855. 'Parameter "modelName" of ' +
  1856. 'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
  1857. 'requires a non-empty String, but %s given.',
  1858. v,
  1859. );
  1860. expect(throwable('')).to.throw(error('""'));
  1861. expect(throwable(10)).to.throw(error('10'));
  1862. expect(throwable(true)).to.throw(error('true'));
  1863. expect(throwable(false)).to.throw(error('false'));
  1864. expect(throwable([])).to.throw(error('Array'));
  1865. expect(throwable({})).to.throw(error('Object'));
  1866. expect(throwable(undefined)).to.throw(error('undefined'));
  1867. expect(throwable(null)).to.throw(error('null'));
  1868. throwable('model')();
  1869. });
  1870. it('requires parameter "propertyName" to be a non-empty String', function () {
  1871. const schema = new Schema();
  1872. schema.defineModel({
  1873. name: 'model',
  1874. properties: {
  1875. foo: DataType.OBJECT,
  1876. },
  1877. });
  1878. const S = schema.getService(ModelDefinitionUtils);
  1879. const throwable = v => () =>
  1880. S.getModelNameOfPropertyValueIfDefined('model', v);
  1881. const error = v =>
  1882. format(
  1883. 'Parameter "propertyName" of ' +
  1884. 'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
  1885. 'requires a non-empty String, but %s given.',
  1886. v,
  1887. );
  1888. expect(throwable('')).to.throw(error('""'));
  1889. expect(throwable(10)).to.throw(error('10'));
  1890. expect(throwable(true)).to.throw(error('true'));
  1891. expect(throwable(false)).to.throw(error('false'));
  1892. expect(throwable([])).to.throw(error('Array'));
  1893. expect(throwable({})).to.throw(error('Object'));
  1894. expect(throwable(undefined)).to.throw(error('undefined'));
  1895. expect(throwable(null)).to.throw(error('null'));
  1896. throwable('foo')();
  1897. });
  1898. it('returns undefined if the property definition is DataType', function () {
  1899. const fn = v => {
  1900. const schema = new Schema();
  1901. schema.defineModel({
  1902. name: 'model',
  1903. properties: {
  1904. foo: v,
  1905. },
  1906. });
  1907. const S = schema.getService(ModelDefinitionUtils);
  1908. return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  1909. };
  1910. expect(fn(DataType.ANY)).to.be.undefined;
  1911. expect(fn(DataType.STRING)).to.be.undefined;
  1912. expect(fn(DataType.NUMBER)).to.be.undefined;
  1913. expect(fn(DataType.BOOLEAN)).to.be.undefined;
  1914. expect(fn(DataType.ARRAY)).to.be.undefined;
  1915. expect(fn(DataType.OBJECT)).to.be.undefined;
  1916. });
  1917. });
  1918. describe('full property definition', function () {
  1919. it('requires parameter "modelName" to be a non-empty String', function () {
  1920. const schema = new Schema();
  1921. schema.defineModel({
  1922. name: 'model',
  1923. properties: {
  1924. foo: {
  1925. type: DataType.OBJECT,
  1926. },
  1927. },
  1928. });
  1929. const S = schema.getService(ModelDefinitionUtils);
  1930. const throwable = v => () =>
  1931. S.getModelNameOfPropertyValueIfDefined(v, 'foo');
  1932. const error = v =>
  1933. format(
  1934. 'Parameter "modelName" of ' +
  1935. 'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
  1936. 'requires a non-empty String, but %s given.',
  1937. v,
  1938. );
  1939. expect(throwable('')).to.throw(error('""'));
  1940. expect(throwable(10)).to.throw(error('10'));
  1941. expect(throwable(true)).to.throw(error('true'));
  1942. expect(throwable(false)).to.throw(error('false'));
  1943. expect(throwable([])).to.throw(error('Array'));
  1944. expect(throwable({})).to.throw(error('Object'));
  1945. expect(throwable(undefined)).to.throw(error('undefined'));
  1946. expect(throwable(null)).to.throw(error('null'));
  1947. throwable('model')();
  1948. });
  1949. it('requires parameter "propertyName" to be a non-empty String', function () {
  1950. const schema = new Schema();
  1951. schema.defineModel({
  1952. name: 'model',
  1953. properties: {
  1954. foo: {
  1955. type: DataType.OBJECT,
  1956. },
  1957. },
  1958. });
  1959. const S = schema.getService(ModelDefinitionUtils);
  1960. const throwable = v => () =>
  1961. S.getModelNameOfPropertyValueIfDefined('model', v);
  1962. const error = v =>
  1963. format(
  1964. 'Parameter "propertyName" of ' +
  1965. 'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
  1966. 'requires a non-empty String, but %s given.',
  1967. v,
  1968. );
  1969. expect(throwable('')).to.throw(error('""'));
  1970. expect(throwable(10)).to.throw(error('10'));
  1971. expect(throwable(true)).to.throw(error('true'));
  1972. expect(throwable(false)).to.throw(error('false'));
  1973. expect(throwable([])).to.throw(error('Array'));
  1974. expect(throwable({})).to.throw(error('Object'));
  1975. expect(throwable(undefined)).to.throw(error('undefined'));
  1976. expect(throwable(null)).to.throw(error('null'));
  1977. throwable('foo')();
  1978. });
  1979. it('return undefined if no model name specified', function () {
  1980. const fn = v => {
  1981. const schema = new Schema();
  1982. schema.defineModel({
  1983. name: 'model',
  1984. properties: {
  1985. foo: {
  1986. type: v,
  1987. },
  1988. },
  1989. });
  1990. const S = schema.getService(ModelDefinitionUtils);
  1991. return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  1992. };
  1993. expect(fn(DataType.ANY)).to.be.undefined;
  1994. expect(fn(DataType.STRING)).to.be.undefined;
  1995. expect(fn(DataType.NUMBER)).to.be.undefined;
  1996. expect(fn(DataType.BOOLEAN)).to.be.undefined;
  1997. expect(fn(DataType.ARRAY)).to.be.undefined;
  1998. expect(fn(DataType.OBJECT)).to.be.undefined;
  1999. });
  2000. it('return undefined if no model name specified in case of Array property', function () {
  2001. const fn = v => {
  2002. const schema = new Schema();
  2003. schema.defineModel({
  2004. name: 'model',
  2005. properties: {
  2006. foo: {
  2007. type: DataType.ARRAY,
  2008. itemType: v,
  2009. },
  2010. },
  2011. });
  2012. const S = schema.getService(ModelDefinitionUtils);
  2013. return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  2014. };
  2015. expect(fn(DataType.ANY)).to.be.undefined;
  2016. expect(fn(DataType.STRING)).to.be.undefined;
  2017. expect(fn(DataType.NUMBER)).to.be.undefined;
  2018. expect(fn(DataType.BOOLEAN)).to.be.undefined;
  2019. expect(fn(DataType.ARRAY)).to.be.undefined;
  2020. expect(fn(DataType.OBJECT)).to.be.undefined;
  2021. });
  2022. it('returns a model name from the option "model" in case of Object property', function () {
  2023. const schema = new Schema();
  2024. schema.defineModel({
  2025. name: 'model',
  2026. properties: {
  2027. foo: {
  2028. type: DataType.OBJECT,
  2029. model: 'myModel',
  2030. },
  2031. },
  2032. });
  2033. const S = schema.getService(ModelDefinitionUtils);
  2034. const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  2035. expect(res).to.be.eq('myModel');
  2036. });
  2037. it('returns a model name from the option "itemModel" in case of Array property', function () {
  2038. const schema = new Schema();
  2039. schema.defineModel({
  2040. name: 'model',
  2041. properties: {
  2042. foo: {
  2043. type: DataType.ARRAY,
  2044. itemType: DataType.OBJECT,
  2045. itemModel: 'myModel',
  2046. },
  2047. },
  2048. });
  2049. const S = schema.getService(ModelDefinitionUtils);
  2050. const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
  2051. expect(res).to.be.eq('myModel');
  2052. });
  2053. });
  2054. });
  2055. });