property-uniqueness-validator.spec.js 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390
  1. import {expect} from 'chai';
  2. import {DataType} from './data-type.js';
  3. import {format} from '@e22m4u/js-format';
  4. import {DatabaseSchema} from '../../../database-schema.js';
  5. import {PropertyUniqueness} from './property-uniqueness.js';
  6. import {PropertyUniquenessValidator} from './property-uniqueness-validator.js';
  7. import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '../model-definition-utils.js';
  8. describe('PropertyUniquenessValidator', function () {
  9. describe('validate', function () {
  10. it('requires the parameter "countMethod" to be a Function', async function () {
  11. const dbs = new DatabaseSchema();
  12. dbs.defineModel({
  13. name: 'model',
  14. properties: {
  15. foo: {
  16. type: DataType.ANY,
  17. unique: true,
  18. },
  19. },
  20. });
  21. const puv = dbs.getService(PropertyUniquenessValidator);
  22. const throwable = v => puv.validate(v, 'create', 'model', {});
  23. const error = v =>
  24. format(
  25. 'The parameter "countMethod" of the PropertyUniquenessValidator ' +
  26. 'must be a Function, but %s was given.',
  27. v,
  28. );
  29. await expect(throwable('str')).to.be.rejectedWith(error('"str"'));
  30. await expect(throwable('')).to.be.rejectedWith(error('""'));
  31. await expect(throwable(10)).to.be.rejectedWith(error('10'));
  32. await expect(throwable(0)).to.be.rejectedWith(error('0'));
  33. await expect(throwable(true)).to.be.rejectedWith(error('true'));
  34. await expect(throwable(false)).to.be.rejectedWith(error('false'));
  35. await expect(throwable(undefined)).to.be.rejectedWith(error('undefined'));
  36. await expect(throwable(null)).to.be.rejectedWith(error('null'));
  37. await expect(throwable(new Date())).to.be.rejectedWith(error('Date'));
  38. await expect(throwable([1, 2, 3])).to.be.rejectedWith(error('Array'));
  39. await expect(throwable([])).to.be.rejectedWith(error('Array'));
  40. await expect(throwable({foo: 'bar'})).to.be.rejectedWith(error('Object'));
  41. await expect(throwable({})).to.be.rejectedWith(error('Object'));
  42. await throwable(() => 0);
  43. });
  44. it('requires the parameter "methodName" to be a non-empty String', async function () {
  45. const dbs = new DatabaseSchema();
  46. dbs.defineModel({
  47. name: 'model',
  48. properties: {
  49. foo: {
  50. type: DataType.ANY,
  51. unique: true,
  52. },
  53. },
  54. });
  55. const puv = dbs.getService(PropertyUniquenessValidator);
  56. const throwable = v => puv.validate(() => 0, v, 'model', {});
  57. const error = v =>
  58. format(
  59. 'The parameter "methodName" of the PropertyUniquenessValidator ' +
  60. 'must be a non-empty String, but %s was given.',
  61. v,
  62. );
  63. await expect(throwable('')).to.be.rejectedWith(error('""'));
  64. await expect(throwable(10)).to.be.rejectedWith(error('10'));
  65. await expect(throwable(0)).to.be.rejectedWith(error('0'));
  66. await expect(throwable(true)).to.be.rejectedWith(error('true'));
  67. await expect(throwable(false)).to.be.rejectedWith(error('false'));
  68. await expect(throwable(undefined)).to.be.rejectedWith(error('undefined'));
  69. await expect(throwable(null)).to.be.rejectedWith(error('null'));
  70. await expect(throwable(new Date())).to.be.rejectedWith(error('Date'));
  71. await expect(throwable([1, 2, 3])).to.be.rejectedWith(error('Array'));
  72. await expect(throwable([])).to.be.rejectedWith(error('Array'));
  73. await expect(throwable({foo: 'bar'})).to.be.rejectedWith(error('Object'));
  74. await expect(throwable({})).to.be.rejectedWith(error('Object'));
  75. await expect(throwable(() => 0)).to.be.rejectedWith(error('Function'));
  76. await throwable('create');
  77. });
  78. it('requires the parameter "modelName" to be a non-empty String', async function () {
  79. const dbs = new DatabaseSchema();
  80. dbs.defineModel({
  81. name: 'model',
  82. properties: {
  83. foo: {
  84. type: DataType.ANY,
  85. unique: true,
  86. },
  87. },
  88. });
  89. const puv = dbs.getService(PropertyUniquenessValidator);
  90. const throwable = v => puv.validate(() => 0, 'create', v, {});
  91. const error = v =>
  92. format(
  93. 'The parameter "modelName" of the PropertyUniquenessValidator ' +
  94. 'must be a non-empty String, but %s was given.',
  95. v,
  96. );
  97. await expect(throwable('')).to.be.rejectedWith(error('""'));
  98. await expect(throwable(10)).to.be.rejectedWith(error('10'));
  99. await expect(throwable(0)).to.be.rejectedWith(error('0'));
  100. await expect(throwable(true)).to.be.rejectedWith(error('true'));
  101. await expect(throwable(false)).to.be.rejectedWith(error('false'));
  102. await expect(throwable(undefined)).to.be.rejectedWith(error('undefined'));
  103. await expect(throwable(null)).to.be.rejectedWith(error('null'));
  104. await expect(throwable(new Date())).to.be.rejectedWith(error('Date'));
  105. await expect(throwable([1, 2, 3])).to.be.rejectedWith(error('Array'));
  106. await expect(throwable([])).to.be.rejectedWith(error('Array'));
  107. await expect(throwable({foo: 'bar'})).to.be.rejectedWith(error('Object'));
  108. await expect(throwable({})).to.be.rejectedWith(error('Object'));
  109. await expect(throwable(() => 0)).to.be.rejectedWith(error('Function'));
  110. await throwable('model');
  111. });
  112. it('requires the parameter "modelData" to be a pure Object', async function () {
  113. const dbs = new DatabaseSchema();
  114. dbs.defineModel({
  115. name: 'model',
  116. properties: {
  117. foo: {
  118. type: DataType.ANY,
  119. unique: true,
  120. },
  121. },
  122. });
  123. const puv = dbs.getService(PropertyUniquenessValidator);
  124. const throwable = v => puv.validate(() => 0, 'create', 'model', v);
  125. const error = v =>
  126. format(
  127. 'The data of the model "model" should be an Object, but %s was given.',
  128. v,
  129. );
  130. await expect(throwable('str')).to.be.rejectedWith(error('"str"'));
  131. await expect(throwable('')).to.be.rejectedWith(error('""'));
  132. await expect(throwable(10)).to.be.rejectedWith(error('10'));
  133. await expect(throwable(0)).to.be.rejectedWith(error('0'));
  134. await expect(throwable(true)).to.be.rejectedWith(error('true'));
  135. await expect(throwable(false)).to.be.rejectedWith(error('false'));
  136. await expect(throwable(undefined)).to.be.rejectedWith(error('undefined'));
  137. await expect(throwable(null)).to.be.rejectedWith(error('null'));
  138. await expect(throwable([1, 2, 3])).to.be.rejectedWith(error('Array'));
  139. await expect(throwable([])).to.be.rejectedWith(error('Array'));
  140. await expect(throwable(new Date())).to.be.rejectedWith(error('Date'));
  141. await expect(throwable(() => 0)).to.be.rejectedWith(error('Function'));
  142. await throwable({foo: 'bar'});
  143. await throwable({});
  144. });
  145. it('skips checking if the option "unique" is not provided', async function () {
  146. const dbs = new DatabaseSchema();
  147. dbs.defineModel({
  148. name: 'model',
  149. properties: {
  150. foo: DataType.ANY,
  151. bar: {
  152. type: DataType.ANY,
  153. },
  154. },
  155. });
  156. const puv = dbs.getService(PropertyUniquenessValidator);
  157. const promise = puv.validate(() => 1, 'create', 'model', {});
  158. await expect(promise).not.to.be.rejected;
  159. });
  160. it('skips checking if the option "unique" is undefined', async function () {
  161. const dbs = new DatabaseSchema();
  162. dbs.defineModel({
  163. name: 'model',
  164. properties: {
  165. foo: {
  166. type: DataType.ANY,
  167. unique: undefined,
  168. },
  169. },
  170. });
  171. const puv = dbs.getService(PropertyUniquenessValidator);
  172. const promise = puv.validate(() => 1, 'create', 'model', {});
  173. await expect(promise).not.to.be.rejected;
  174. });
  175. it('skips checking if the option "unique" is null', async function () {
  176. const dbs = new DatabaseSchema();
  177. dbs.defineModel({
  178. name: 'model',
  179. properties: {
  180. foo: {
  181. type: DataType.ANY,
  182. unique: null,
  183. },
  184. },
  185. });
  186. const puv = dbs.getService(PropertyUniquenessValidator);
  187. const promise = puv.validate(() => 1, 'create', 'model', {});
  188. await expect(promise).not.to.be.rejected;
  189. });
  190. it('skips checking if the option "unique" is false', async function () {
  191. const dbs = new DatabaseSchema();
  192. dbs.defineModel({
  193. name: 'model',
  194. properties: {
  195. foo: {
  196. type: DataType.ANY,
  197. unique: false,
  198. },
  199. },
  200. });
  201. const puv = dbs.getService(PropertyUniquenessValidator);
  202. const promise = puv.validate(() => 1, 'create', 'model', {});
  203. await expect(promise).not.to.be.rejected;
  204. });
  205. it('skips checking if the option "unique" is "nonUnique"', async function () {
  206. const dbs = new DatabaseSchema();
  207. dbs.defineModel({
  208. name: 'model',
  209. properties: {
  210. foo: {
  211. type: DataType.ANY,
  212. unique: PropertyUniqueness.NON_UNIQUE,
  213. },
  214. },
  215. });
  216. const puv = dbs.getService(PropertyUniquenessValidator);
  217. const promise = puv.validate(() => 1, 'create', 'model', {});
  218. await expect(promise).not.to.be.rejected;
  219. });
  220. it('throws an error for unsupported method', async function () {
  221. const dbs = new DatabaseSchema();
  222. dbs.defineModel({
  223. name: 'model',
  224. properties: {
  225. foo: {
  226. type: DataType.ANY,
  227. unique: true,
  228. },
  229. },
  230. });
  231. const puv = dbs.getService(PropertyUniquenessValidator);
  232. const promise = puv.validate(() => 1, 'unsupported', 'model', {});
  233. await expect(promise).to.be.rejectedWith(
  234. 'The PropertyUniquenessValidator does not ' +
  235. 'support the adapter method "unsupported".',
  236. );
  237. });
  238. describe('the "unique" option is true', function () {
  239. describe('create', function () {
  240. it('throws an error if the "countMethod" returns a positive number', async function () {
  241. const dbs = new DatabaseSchema();
  242. dbs.defineModel({
  243. name: 'model',
  244. properties: {
  245. foo: {
  246. type: DataType.ANY,
  247. unique: true,
  248. },
  249. },
  250. });
  251. const puv = dbs.getService(PropertyUniquenessValidator);
  252. const promise = puv.validate(() => 1, 'create', 'model', {
  253. foo: 'bar',
  254. });
  255. await expect(promise).to.be.rejectedWith(
  256. 'An existing document of the model "model" already has ' +
  257. 'the property "foo" with the value "bar" and should be unique.',
  258. );
  259. });
  260. it('passes validation if the "countMethod" returns zero', async function () {
  261. const dbs = new DatabaseSchema();
  262. dbs.defineModel({
  263. name: 'model',
  264. properties: {
  265. foo: {
  266. type: DataType.ANY,
  267. unique: true,
  268. },
  269. },
  270. });
  271. const puv = dbs.getService(PropertyUniquenessValidator);
  272. await puv.validate(() => 0, 'create', 'model', {foo: 'bar'});
  273. });
  274. it('invokes the "countMethod" for each unique property of the model', async function () {
  275. const dbs = new DatabaseSchema();
  276. dbs.defineModel({
  277. name: 'model',
  278. properties: {
  279. foo: {
  280. type: DataType.ANY,
  281. unique: true,
  282. },
  283. bar: {
  284. type: DataType.ANY,
  285. unique: false,
  286. },
  287. baz: {
  288. type: DataType.ANY,
  289. unique: true,
  290. },
  291. },
  292. });
  293. const puv = dbs.getService(PropertyUniquenessValidator);
  294. let invoked = 0;
  295. const modelData = {foo: 'val1', bar: 'val2'};
  296. const countMethod = where => {
  297. if (invoked === 0) {
  298. expect(where).to.be.eql({foo: 'val1'});
  299. } else if (invoked === 1) {
  300. expect(where).to.be.eql({baz: undefined});
  301. }
  302. invoked++;
  303. return 0;
  304. };
  305. await puv.validate(countMethod, 'create', 'model', modelData);
  306. expect(invoked).to.be.eq(2);
  307. });
  308. });
  309. describe('replaceById', function () {
  310. it('throws an error if the "countMethod" returns a positive number', async function () {
  311. const dbs = new DatabaseSchema();
  312. dbs.defineModel({
  313. name: 'model',
  314. properties: {
  315. foo: {
  316. type: DataType.ANY,
  317. unique: true,
  318. },
  319. },
  320. });
  321. const puv = dbs.getService(PropertyUniquenessValidator);
  322. const promise = puv.validate(
  323. () => 1,
  324. 'replaceById',
  325. 'model',
  326. {foo: 'bar'},
  327. 1,
  328. );
  329. await expect(promise).to.be.rejectedWith(
  330. 'An existing document of the model "model" already has ' +
  331. 'the property "foo" with the value "bar" and should be unique.',
  332. );
  333. });
  334. it('passes validation if the "countMethod" returns zero', async function () {
  335. const dbs = new DatabaseSchema();
  336. dbs.defineModel({
  337. name: 'model',
  338. properties: {
  339. foo: {
  340. type: DataType.ANY,
  341. unique: true,
  342. },
  343. },
  344. });
  345. const puv = dbs.getService(PropertyUniquenessValidator);
  346. await puv.validate(() => 0, 'replaceById', 'model', {foo: 'bar'}, 1);
  347. });
  348. it('invokes the "countMethod" for each unique property of the model', async function () {
  349. const dbs = new DatabaseSchema();
  350. dbs.defineModel({
  351. name: 'model',
  352. properties: {
  353. foo: {
  354. type: DataType.ANY,
  355. unique: true,
  356. },
  357. bar: {
  358. type: DataType.ANY,
  359. unique: false,
  360. },
  361. baz: {
  362. type: DataType.ANY,
  363. unique: true,
  364. },
  365. },
  366. });
  367. const puv = dbs.getService(PropertyUniquenessValidator);
  368. let invoked = 0;
  369. const idValue = 1;
  370. const modelData = {foo: 'val1', bar: 'val2'};
  371. const countMethod = where => {
  372. if (invoked === 0) {
  373. expect(where).to.be.eql({
  374. [DEF_PK]: {neq: idValue},
  375. foo: 'val1',
  376. });
  377. } else if (invoked === 1) {
  378. expect(where).to.be.eql({
  379. [DEF_PK]: {neq: idValue},
  380. baz: undefined,
  381. });
  382. }
  383. invoked++;
  384. return 0;
  385. };
  386. await puv.validate(
  387. countMethod,
  388. 'replaceById',
  389. 'model',
  390. modelData,
  391. idValue,
  392. );
  393. expect(invoked).to.be.eq(2);
  394. });
  395. it('can use a custom primary key', async function () {
  396. const dbs = new DatabaseSchema();
  397. dbs.defineModel({
  398. name: 'model',
  399. properties: {
  400. myId: {
  401. type: DataType.NUMBER,
  402. primaryKey: true,
  403. },
  404. foo: {
  405. type: DataType.ANY,
  406. unique: true,
  407. },
  408. },
  409. });
  410. const puv = dbs.getService(PropertyUniquenessValidator);
  411. let invoked = 0;
  412. const idValue = 1;
  413. const modelData = {foo: 'bar'};
  414. const countMethod = where => {
  415. if (invoked === 0)
  416. expect(where).to.be.eql({
  417. myId: {neq: idValue},
  418. foo: 'bar',
  419. });
  420. invoked++;
  421. return 0;
  422. };
  423. await puv.validate(
  424. countMethod,
  425. 'replaceById',
  426. 'model',
  427. modelData,
  428. idValue,
  429. );
  430. expect(invoked).to.be.eq(1);
  431. });
  432. });
  433. describe('replaceOrCreate', function () {
  434. it('throws an error if the "countMethod" returns a positive number', async function () {
  435. const dbs = new DatabaseSchema();
  436. dbs.defineModel({
  437. name: 'model',
  438. properties: {
  439. foo: {
  440. type: DataType.ANY,
  441. unique: true,
  442. },
  443. },
  444. });
  445. const puv = dbs.getService(PropertyUniquenessValidator);
  446. const promise = puv.validate(() => 1, 'replaceOrCreate', 'model', {
  447. foo: 'bar',
  448. });
  449. await expect(promise).to.be.rejectedWith(
  450. 'An existing document of the model "model" already has ' +
  451. 'the property "foo" with the value "bar" and should be unique.',
  452. );
  453. });
  454. it('passes validation if the "countMethod" returns zero', async function () {
  455. const dbs = new DatabaseSchema();
  456. dbs.defineModel({
  457. name: 'model',
  458. properties: {
  459. foo: {
  460. type: DataType.ANY,
  461. unique: true,
  462. },
  463. },
  464. });
  465. const puv = dbs.getService(PropertyUniquenessValidator);
  466. await puv.validate(() => 0, 'replaceOrCreate', 'model', {foo: 'bar'});
  467. });
  468. it('invokes the "countMethod" for each unique property of the model', async function () {
  469. const dbs = new DatabaseSchema();
  470. dbs.defineModel({
  471. name: 'model',
  472. properties: {
  473. foo: {
  474. type: DataType.ANY,
  475. unique: true,
  476. },
  477. bar: {
  478. type: DataType.ANY,
  479. unique: false,
  480. },
  481. baz: {
  482. type: DataType.ANY,
  483. unique: true,
  484. },
  485. },
  486. });
  487. const puv = dbs.getService(PropertyUniquenessValidator);
  488. let invoked = 0;
  489. const modelData = {foo: 'val1', bar: 'val2'};
  490. const countMethod = where => {
  491. if (invoked === 0) {
  492. expect(where).to.be.eql({foo: 'val1'});
  493. } else if (invoked === 1) {
  494. expect(where).to.be.eql({baz: undefined});
  495. }
  496. invoked++;
  497. return 0;
  498. };
  499. await puv.validate(
  500. countMethod,
  501. 'replaceOrCreate',
  502. 'model',
  503. modelData,
  504. );
  505. expect(invoked).to.be.eq(2);
  506. });
  507. describe('in case that the given model has a document identifier', function () {
  508. describe('a document of the given identifier does not exist', function () {
  509. it('uses the default primary key to check existence of the given identifier', async function () {
  510. const dbs = new DatabaseSchema();
  511. dbs.defineModel({
  512. name: 'model',
  513. properties: {
  514. foo: {
  515. type: DataType.ANY,
  516. unique: true,
  517. },
  518. },
  519. });
  520. const puv = dbs.getService(PropertyUniquenessValidator);
  521. let invoked = 0;
  522. const idValue = 1;
  523. const modelData = {[DEF_PK]: idValue, foo: 'bar'};
  524. const countMethod = where => {
  525. if (invoked === 0) {
  526. expect(where).to.be.eql({[DEF_PK]: idValue});
  527. } else if (invoked === 1) {
  528. expect(where).to.be.eql({foo: 'bar'});
  529. }
  530. invoked++;
  531. return 0;
  532. };
  533. await puv.validate(
  534. countMethod,
  535. 'replaceOrCreate',
  536. 'model',
  537. modelData,
  538. );
  539. expect(invoked).to.be.eq(2);
  540. });
  541. it('uses a custom primary key to check existence of the given identifier', async function () {
  542. const dbs = new DatabaseSchema();
  543. dbs.defineModel({
  544. name: 'model',
  545. properties: {
  546. myId: {
  547. type: DataType.NUMBER,
  548. primaryKey: true,
  549. },
  550. foo: {
  551. type: DataType.ANY,
  552. unique: true,
  553. },
  554. },
  555. });
  556. const puv = dbs.getService(PropertyUniquenessValidator);
  557. let invoked = 0;
  558. const idValue = 1;
  559. const modelData = {myId: idValue, foo: 'bar'};
  560. const countMethod = where => {
  561. if (invoked === 0) {
  562. expect(where).to.be.eql({myId: idValue});
  563. } else if (invoked === 1) {
  564. expect(where).to.be.eql({foo: 'bar'});
  565. }
  566. invoked++;
  567. return 0;
  568. };
  569. await puv.validate(
  570. countMethod,
  571. 'replaceOrCreate',
  572. 'model',
  573. modelData,
  574. idValue,
  575. );
  576. expect(invoked).to.be.eq(2);
  577. });
  578. it('checks the given identifier only once', async function () {
  579. const dbs = new DatabaseSchema();
  580. dbs.defineModel({
  581. name: 'model',
  582. properties: {
  583. foo: {
  584. type: DataType.ANY,
  585. unique: true,
  586. },
  587. bar: {
  588. type: DataType.ANY,
  589. unique: true,
  590. },
  591. },
  592. });
  593. const puv = dbs.getService(PropertyUniquenessValidator);
  594. let invoked = 0;
  595. const idValue = 1;
  596. const modelData = {[DEF_PK]: idValue, foo: 'val1', bar: 'val2'};
  597. const countMethod = where => {
  598. if (invoked === 0) {
  599. expect(where).to.be.eql({[DEF_PK]: idValue});
  600. } else if (invoked === 1) {
  601. expect(where).to.be.eql({foo: 'val1'});
  602. } else if (invoked === 2) {
  603. expect(where).to.be.eql({bar: 'val2'});
  604. }
  605. invoked++;
  606. return 0;
  607. };
  608. await puv.validate(
  609. countMethod,
  610. 'replaceOrCreate',
  611. 'model',
  612. modelData,
  613. );
  614. expect(invoked).to.be.eq(3);
  615. });
  616. });
  617. describe('a document of the given identifier already exist', function () {
  618. it('uses the default primary key to check existence of the given identifier', async function () {
  619. const dbs = new DatabaseSchema();
  620. dbs.defineModel({
  621. name: 'model',
  622. properties: {
  623. foo: {
  624. type: DataType.ANY,
  625. unique: true,
  626. },
  627. },
  628. });
  629. const puv = dbs.getService(PropertyUniquenessValidator);
  630. let invoked = 0;
  631. const idValue = 1;
  632. const modelData = {
  633. [DEF_PK]: idValue,
  634. foo: 'bar',
  635. };
  636. const countMethod = where => {
  637. invoked++;
  638. if (invoked === 1) {
  639. expect(where).to.be.eql({
  640. [DEF_PK]: idValue,
  641. });
  642. return 1;
  643. } else if (invoked === 2) {
  644. expect(where).to.be.eql({
  645. [DEF_PK]: {neq: idValue},
  646. foo: 'bar',
  647. });
  648. return 0;
  649. }
  650. };
  651. await puv.validate(
  652. countMethod,
  653. 'replaceOrCreate',
  654. 'model',
  655. modelData,
  656. );
  657. expect(invoked).to.be.eq(2);
  658. });
  659. it('uses a custom primary key to check existence of the given identifier', async function () {
  660. const dbs = new DatabaseSchema();
  661. dbs.defineModel({
  662. name: 'model',
  663. properties: {
  664. myId: {
  665. type: DataType.NUMBER,
  666. primaryKey: true,
  667. },
  668. foo: {
  669. type: DataType.ANY,
  670. unique: true,
  671. },
  672. },
  673. });
  674. const puv = dbs.getService(PropertyUniquenessValidator);
  675. let invoked = 0;
  676. const idValue = 1;
  677. const modelData = {myId: idValue, foo: 'bar'};
  678. const countMethod = where => {
  679. invoked++;
  680. if (invoked === 1) {
  681. expect(where).to.be.eql({
  682. myId: idValue,
  683. });
  684. return 1;
  685. } else if (invoked === 2) {
  686. expect(where).to.be.eql({
  687. myId: {neq: idValue},
  688. foo: 'bar',
  689. });
  690. return 0;
  691. }
  692. };
  693. await puv.validate(
  694. countMethod,
  695. 'replaceOrCreate',
  696. 'model',
  697. modelData,
  698. idValue,
  699. );
  700. expect(invoked).to.be.eq(2);
  701. });
  702. it('checks the given identifier only once', async function () {
  703. const dbs = new DatabaseSchema();
  704. dbs.defineModel({
  705. name: 'model',
  706. properties: {
  707. foo: {
  708. type: DataType.ANY,
  709. unique: true,
  710. },
  711. bar: {
  712. type: DataType.ANY,
  713. unique: true,
  714. },
  715. },
  716. });
  717. const puv = dbs.getService(PropertyUniquenessValidator);
  718. let invoked = 0;
  719. const idValue = 1;
  720. const modelData = {
  721. [DEF_PK]: idValue,
  722. foo: 'val1',
  723. bar: 'val2',
  724. };
  725. const countMethod = where => {
  726. invoked++;
  727. if (invoked === 1) {
  728. expect(where).to.be.eql({[DEF_PK]: idValue});
  729. return 1;
  730. } else if (invoked === 2) {
  731. expect(where).to.be.eql({
  732. [DEF_PK]: {neq: idValue},
  733. foo: 'val1',
  734. });
  735. return 0;
  736. } else if (invoked === 3) {
  737. expect(where).to.be.eql({
  738. [DEF_PK]: {neq: idValue},
  739. bar: 'val2',
  740. });
  741. return 0;
  742. }
  743. };
  744. await puv.validate(
  745. countMethod,
  746. 'replaceOrCreate',
  747. 'model',
  748. modelData,
  749. );
  750. expect(invoked).to.be.eq(3);
  751. });
  752. });
  753. });
  754. });
  755. describe('patch', function () {
  756. it('throws an error if the "countMethod" returns a positive number', async function () {
  757. const dbs = new DatabaseSchema();
  758. dbs.defineModel({
  759. name: 'model',
  760. properties: {
  761. foo: {
  762. type: DataType.ANY,
  763. unique: true,
  764. },
  765. },
  766. });
  767. const puv = dbs.getService(PropertyUniquenessValidator);
  768. const promise = puv.validate(() => 1, 'patch', 'model', {foo: 'bar'});
  769. await expect(promise).to.be.rejectedWith(
  770. 'An existing document of the model "model" already has ' +
  771. 'the property "foo" with the value "bar" and should be unique.',
  772. );
  773. });
  774. it('passes validation if the "countMethod" returns zero', async function () {
  775. const dbs = new DatabaseSchema();
  776. dbs.defineModel({
  777. name: 'model',
  778. properties: {
  779. foo: {
  780. type: DataType.ANY,
  781. unique: true,
  782. },
  783. },
  784. });
  785. const puv = dbs.getService(PropertyUniquenessValidator);
  786. await puv.validate(() => 0, 'patch', 'model', {foo: 'bar'});
  787. });
  788. it('invokes the "countMethod" for given properties which should be unique', async function () {
  789. const dbs = new DatabaseSchema();
  790. dbs.defineModel({
  791. name: 'model',
  792. properties: {
  793. foo: {
  794. type: DataType.ANY,
  795. unique: true,
  796. },
  797. bar: {
  798. type: DataType.ANY,
  799. unique: false,
  800. },
  801. baz: {
  802. type: DataType.ANY,
  803. unique: true,
  804. },
  805. },
  806. });
  807. const puv = dbs.getService(PropertyUniquenessValidator);
  808. let invoked = 0;
  809. const modelData = {foo: 'val1', bar: 'val2'};
  810. const countMethod = where => {
  811. if (invoked === 0) expect(where).to.be.eql({foo: 'val1'});
  812. invoked++;
  813. return 0;
  814. };
  815. await puv.validate(countMethod, 'patch', 'model', modelData);
  816. expect(invoked).to.be.eq(1);
  817. });
  818. it('skips uniqueness checking for non-provided fields', async function () {
  819. const dbs = new DatabaseSchema();
  820. dbs.defineModel({
  821. name: 'model',
  822. properties: {
  823. foo: {
  824. type: DataType.ANY,
  825. unique: true,
  826. },
  827. },
  828. });
  829. const puv = dbs.getService(PropertyUniquenessValidator);
  830. const promise1 = puv.validate(() => 1, 'patch', 'model', {
  831. foo: 'bar',
  832. });
  833. const promise2 = puv.validate(() => 1, 'patch', 'model', {
  834. baz: 'qux',
  835. });
  836. await expect(promise1).to.be.rejectedWith(
  837. 'An existing document of the model "model" already has ' +
  838. 'the property "foo" with the value "bar" and should be unique.',
  839. );
  840. await expect(promise2).not.to.be.rejected;
  841. });
  842. });
  843. describe('patchById', function () {
  844. it('throws an error if the "countMethod" returns a positive number', async function () {
  845. const dbs = new DatabaseSchema();
  846. dbs.defineModel({
  847. name: 'model',
  848. properties: {
  849. foo: {
  850. type: DataType.ANY,
  851. unique: true,
  852. },
  853. },
  854. });
  855. const puv = dbs.getService(PropertyUniquenessValidator);
  856. const promise = puv.validate(
  857. () => 1,
  858. 'patchById',
  859. 'model',
  860. {foo: 'bar'},
  861. 1,
  862. );
  863. await expect(promise).to.be.rejectedWith(
  864. 'An existing document of the model "model" already has ' +
  865. 'the property "foo" with the value "bar" and should be unique.',
  866. );
  867. });
  868. it('passes validation if the "countMethod" returns zero', async function () {
  869. const dbs = new DatabaseSchema();
  870. dbs.defineModel({
  871. name: 'model',
  872. properties: {
  873. foo: {
  874. type: DataType.ANY,
  875. unique: true,
  876. },
  877. },
  878. });
  879. const puv = dbs.getService(PropertyUniquenessValidator);
  880. await puv.validate(() => 0, 'patchById', 'model', {foo: 'bar'}, 1);
  881. });
  882. it('invokes the "countMethod" for given properties which should be unique', async function () {
  883. const dbs = new DatabaseSchema();
  884. dbs.defineModel({
  885. name: 'model',
  886. properties: {
  887. foo: {
  888. type: DataType.ANY,
  889. unique: true,
  890. },
  891. bar: {
  892. type: DataType.ANY,
  893. unique: false,
  894. },
  895. baz: {
  896. type: DataType.ANY,
  897. unique: true,
  898. },
  899. },
  900. });
  901. const puv = dbs.getService(PropertyUniquenessValidator);
  902. let invoked = 0;
  903. const idValue = 1;
  904. const modelData = {foo: 'val1', bar: 'val2'};
  905. const countMethod = where => {
  906. if (invoked === 0)
  907. expect(where).to.be.eql({
  908. [DEF_PK]: {neq: idValue},
  909. foo: 'val1',
  910. });
  911. invoked++;
  912. return 0;
  913. };
  914. await puv.validate(
  915. countMethod,
  916. 'patchById',
  917. 'model',
  918. modelData,
  919. idValue,
  920. );
  921. expect(invoked).to.be.eq(1);
  922. });
  923. it('skips uniqueness checking for non-provided fields', async function () {
  924. const dbs = new DatabaseSchema();
  925. dbs.defineModel({
  926. name: 'model',
  927. properties: {
  928. foo: {
  929. type: DataType.ANY,
  930. unique: true,
  931. },
  932. },
  933. });
  934. const puv = dbs.getService(PropertyUniquenessValidator);
  935. const promise1 = puv.validate(() => 1, 'patchById', 'model', {
  936. foo: 'bar',
  937. });
  938. const promise2 = puv.validate(() => 1, 'patchById', 'model', {
  939. baz: 'qux',
  940. });
  941. await expect(promise1).to.be.rejectedWith(
  942. 'An existing document of the model "model" already has ' +
  943. 'the property "foo" with the value "bar" and should be unique.',
  944. );
  945. await expect(promise2).not.to.be.rejected;
  946. });
  947. it('uses a custom primary key to check existence of the given identifier', async function () {
  948. const dbs = new DatabaseSchema();
  949. dbs.defineModel({
  950. name: 'model',
  951. properties: {
  952. myId: {
  953. type: DataType.NUMBER,
  954. primaryKey: true,
  955. },
  956. foo: {
  957. type: DataType.ANY,
  958. unique: true,
  959. },
  960. },
  961. });
  962. const puv = dbs.getService(PropertyUniquenessValidator);
  963. let invoked = 0;
  964. const idValue = 1;
  965. const modelData = {foo: 'bar'};
  966. const countMethod = where => {
  967. if (invoked === 0)
  968. expect(where).to.be.eql({
  969. myId: {neq: idValue},
  970. foo: 'bar',
  971. });
  972. invoked++;
  973. return 0;
  974. };
  975. await puv.validate(
  976. countMethod,
  977. 'patchById',
  978. 'model',
  979. modelData,
  980. idValue,
  981. );
  982. expect(invoked).to.be.eq(1);
  983. });
  984. });
  985. });
  986. describe('the "unique" option is "strict"', function () {
  987. describe('create', function () {
  988. it('throws an error if the "countMethod" returns a positive number', async function () {
  989. const dbs = new DatabaseSchema();
  990. dbs.defineModel({
  991. name: 'model',
  992. properties: {
  993. foo: {
  994. type: DataType.ANY,
  995. unique: PropertyUniqueness.STRICT,
  996. },
  997. },
  998. });
  999. const puv = dbs.getService(PropertyUniquenessValidator);
  1000. const promise = puv.validate(() => 1, 'create', 'model', {
  1001. foo: 'bar',
  1002. });
  1003. await expect(promise).to.be.rejectedWith(
  1004. 'An existing document of the model "model" already has ' +
  1005. 'the property "foo" with the value "bar" and should be unique.',
  1006. );
  1007. });
  1008. it('passes validation if the "countMethod" returns zero', async function () {
  1009. const dbs = new DatabaseSchema();
  1010. dbs.defineModel({
  1011. name: 'model',
  1012. properties: {
  1013. foo: {
  1014. type: DataType.ANY,
  1015. unique: PropertyUniqueness.STRICT,
  1016. },
  1017. },
  1018. });
  1019. const puv = dbs.getService(PropertyUniquenessValidator);
  1020. await puv.validate(() => 0, 'create', 'model', {foo: 'bar'});
  1021. });
  1022. it('invokes the "countMethod" for each unique property of the model', async function () {
  1023. const dbs = new DatabaseSchema();
  1024. dbs.defineModel({
  1025. name: 'model',
  1026. properties: {
  1027. foo: {
  1028. type: DataType.ANY,
  1029. unique: PropertyUniqueness.STRICT,
  1030. },
  1031. bar: {
  1032. type: DataType.ANY,
  1033. unique: false,
  1034. },
  1035. baz: {
  1036. type: DataType.ANY,
  1037. unique: PropertyUniqueness.STRICT,
  1038. },
  1039. },
  1040. });
  1041. const puv = dbs.getService(PropertyUniquenessValidator);
  1042. let invoked = 0;
  1043. const modelData = {foo: 'val1', bar: 'val2'};
  1044. const countMethod = where => {
  1045. if (invoked === 0) {
  1046. expect(where).to.be.eql({foo: 'val1'});
  1047. } else if (invoked === 1) {
  1048. expect(where).to.be.eql({baz: undefined});
  1049. }
  1050. invoked++;
  1051. return 0;
  1052. };
  1053. await puv.validate(countMethod, 'create', 'model', modelData);
  1054. expect(invoked).to.be.eq(2);
  1055. });
  1056. });
  1057. describe('replaceById', function () {
  1058. it('throws an error if the "countMethod" returns a positive number', async function () {
  1059. const dbs = new DatabaseSchema();
  1060. dbs.defineModel({
  1061. name: 'model',
  1062. properties: {
  1063. foo: {
  1064. type: DataType.ANY,
  1065. unique: PropertyUniqueness.STRICT,
  1066. },
  1067. },
  1068. });
  1069. const puv = dbs.getService(PropertyUniquenessValidator);
  1070. const promise = puv.validate(
  1071. () => 1,
  1072. 'replaceById',
  1073. 'model',
  1074. {foo: 'bar'},
  1075. 1,
  1076. );
  1077. await expect(promise).to.be.rejectedWith(
  1078. 'An existing document of the model "model" already has ' +
  1079. 'the property "foo" with the value "bar" and should be unique.',
  1080. );
  1081. });
  1082. it('passes validation if the "countMethod" returns zero', async function () {
  1083. const dbs = new DatabaseSchema();
  1084. dbs.defineModel({
  1085. name: 'model',
  1086. properties: {
  1087. foo: {
  1088. type: DataType.ANY,
  1089. unique: PropertyUniqueness.STRICT,
  1090. },
  1091. },
  1092. });
  1093. const puv = dbs.getService(PropertyUniquenessValidator);
  1094. await puv.validate(() => 0, 'replaceById', 'model', {foo: 'bar'}, 1);
  1095. });
  1096. it('invokes the "countMethod" for each unique property of the model', async function () {
  1097. const dbs = new DatabaseSchema();
  1098. dbs.defineModel({
  1099. name: 'model',
  1100. properties: {
  1101. foo: {
  1102. type: DataType.ANY,
  1103. unique: PropertyUniqueness.STRICT,
  1104. },
  1105. bar: {
  1106. type: DataType.ANY,
  1107. unique: false,
  1108. },
  1109. baz: {
  1110. type: DataType.ANY,
  1111. unique: PropertyUniqueness.STRICT,
  1112. },
  1113. },
  1114. });
  1115. const puv = dbs.getService(PropertyUniquenessValidator);
  1116. let invoked = 0;
  1117. const idValue = 1;
  1118. const modelData = {foo: 'val1', bar: 'val2'};
  1119. const countMethod = where => {
  1120. if (invoked === 0) {
  1121. expect(where).to.be.eql({
  1122. [DEF_PK]: {neq: idValue},
  1123. foo: 'val1',
  1124. });
  1125. } else if (invoked === 1) {
  1126. expect(where).to.be.eql({
  1127. [DEF_PK]: {neq: idValue},
  1128. baz: undefined,
  1129. });
  1130. }
  1131. invoked++;
  1132. return 0;
  1133. };
  1134. await puv.validate(
  1135. countMethod,
  1136. 'replaceById',
  1137. 'model',
  1138. modelData,
  1139. idValue,
  1140. );
  1141. expect(invoked).to.be.eq(2);
  1142. });
  1143. it('can use a custom primary key', async function () {
  1144. const dbs = new DatabaseSchema();
  1145. dbs.defineModel({
  1146. name: 'model',
  1147. properties: {
  1148. myId: {
  1149. type: DataType.NUMBER,
  1150. primaryKey: true,
  1151. },
  1152. foo: {
  1153. type: DataType.ANY,
  1154. unique: PropertyUniqueness.STRICT,
  1155. },
  1156. },
  1157. });
  1158. const puv = dbs.getService(PropertyUniquenessValidator);
  1159. let invoked = 0;
  1160. const idValue = 1;
  1161. const modelData = {foo: 'bar'};
  1162. const countMethod = where => {
  1163. if (invoked === 0)
  1164. expect(where).to.be.eql({
  1165. myId: {neq: idValue},
  1166. foo: 'bar',
  1167. });
  1168. invoked++;
  1169. return 0;
  1170. };
  1171. await puv.validate(
  1172. countMethod,
  1173. 'replaceById',
  1174. 'model',
  1175. modelData,
  1176. idValue,
  1177. );
  1178. expect(invoked).to.be.eq(1);
  1179. });
  1180. });
  1181. describe('replaceOrCreate', function () {
  1182. it('throws an error if the "countMethod" returns a positive number', async function () {
  1183. const dbs = new DatabaseSchema();
  1184. dbs.defineModel({
  1185. name: 'model',
  1186. properties: {
  1187. foo: {
  1188. type: DataType.ANY,
  1189. unique: PropertyUniqueness.STRICT,
  1190. },
  1191. },
  1192. });
  1193. const puv = dbs.getService(PropertyUniquenessValidator);
  1194. const promise = puv.validate(() => 1, 'replaceOrCreate', 'model', {
  1195. foo: 'bar',
  1196. });
  1197. await expect(promise).to.be.rejectedWith(
  1198. 'An existing document of the model "model" already has ' +
  1199. 'the property "foo" with the value "bar" and should be unique.',
  1200. );
  1201. });
  1202. it('passes validation if the "countMethod" returns zero', async function () {
  1203. const dbs = new DatabaseSchema();
  1204. dbs.defineModel({
  1205. name: 'model',
  1206. properties: {
  1207. foo: {
  1208. type: DataType.ANY,
  1209. unique: PropertyUniqueness.STRICT,
  1210. },
  1211. },
  1212. });
  1213. const puv = dbs.getService(PropertyUniquenessValidator);
  1214. await puv.validate(() => 0, 'replaceOrCreate', 'model', {foo: 'bar'});
  1215. });
  1216. it('invokes the "countMethod" for each unique property of the model', async function () {
  1217. const dbs = new DatabaseSchema();
  1218. dbs.defineModel({
  1219. name: 'model',
  1220. properties: {
  1221. foo: {
  1222. type: DataType.ANY,
  1223. unique: PropertyUniqueness.STRICT,
  1224. },
  1225. bar: {
  1226. type: DataType.ANY,
  1227. unique: false,
  1228. },
  1229. baz: {
  1230. type: DataType.ANY,
  1231. unique: PropertyUniqueness.STRICT,
  1232. },
  1233. },
  1234. });
  1235. const puv = dbs.getService(PropertyUniquenessValidator);
  1236. let invoked = 0;
  1237. const modelData = {foo: 'val1', bar: 'val2'};
  1238. const countMethod = where => {
  1239. if (invoked === 0) {
  1240. expect(where).to.be.eql({foo: 'val1'});
  1241. } else if (invoked === 1) {
  1242. expect(where).to.be.eql({baz: undefined});
  1243. }
  1244. invoked++;
  1245. return 0;
  1246. };
  1247. await puv.validate(
  1248. countMethod,
  1249. 'replaceOrCreate',
  1250. 'model',
  1251. modelData,
  1252. );
  1253. expect(invoked).to.be.eq(2);
  1254. });
  1255. describe('in case that the given model has a document identifier', function () {
  1256. describe('a document of the given identifier does not exist', function () {
  1257. it('uses the default primary key to check existence of the given identifier', async function () {
  1258. const dbs = new DatabaseSchema();
  1259. dbs.defineModel({
  1260. name: 'model',
  1261. properties: {
  1262. foo: {
  1263. type: DataType.ANY,
  1264. unique: PropertyUniqueness.STRICT,
  1265. },
  1266. },
  1267. });
  1268. const puv = dbs.getService(PropertyUniquenessValidator);
  1269. let invoked = 0;
  1270. const idValue = 1;
  1271. const modelData = {[DEF_PK]: idValue, foo: 'bar'};
  1272. const countMethod = where => {
  1273. if (invoked === 0) {
  1274. expect(where).to.be.eql({[DEF_PK]: idValue});
  1275. } else if (invoked === 1) {
  1276. expect(where).to.be.eql({foo: 'bar'});
  1277. }
  1278. invoked++;
  1279. return 0;
  1280. };
  1281. await puv.validate(
  1282. countMethod,
  1283. 'replaceOrCreate',
  1284. 'model',
  1285. modelData,
  1286. );
  1287. expect(invoked).to.be.eq(2);
  1288. });
  1289. it('uses a custom primary key to check existence of the given identifier', async function () {
  1290. const dbs = new DatabaseSchema();
  1291. dbs.defineModel({
  1292. name: 'model',
  1293. properties: {
  1294. myId: {
  1295. type: DataType.NUMBER,
  1296. primaryKey: true,
  1297. },
  1298. foo: {
  1299. type: DataType.ANY,
  1300. unique: PropertyUniqueness.STRICT,
  1301. },
  1302. },
  1303. });
  1304. const puv = dbs.getService(PropertyUniquenessValidator);
  1305. let invoked = 0;
  1306. const idValue = 1;
  1307. const modelData = {myId: idValue, foo: 'bar'};
  1308. const countMethod = where => {
  1309. if (invoked === 0) {
  1310. expect(where).to.be.eql({myId: idValue});
  1311. } else if (invoked === 1) {
  1312. expect(where).to.be.eql({foo: 'bar'});
  1313. }
  1314. invoked++;
  1315. return 0;
  1316. };
  1317. await puv.validate(
  1318. countMethod,
  1319. 'replaceOrCreate',
  1320. 'model',
  1321. modelData,
  1322. idValue,
  1323. );
  1324. expect(invoked).to.be.eq(2);
  1325. });
  1326. it('checks the given identifier only once', async function () {
  1327. const dbs = new DatabaseSchema();
  1328. dbs.defineModel({
  1329. name: 'model',
  1330. properties: {
  1331. foo: {
  1332. type: DataType.ANY,
  1333. unique: PropertyUniqueness.STRICT,
  1334. },
  1335. bar: {
  1336. type: DataType.ANY,
  1337. unique: PropertyUniqueness.STRICT,
  1338. },
  1339. },
  1340. });
  1341. const puv = dbs.getService(PropertyUniquenessValidator);
  1342. let invoked = 0;
  1343. const idValue = 1;
  1344. const modelData = {[DEF_PK]: idValue, foo: 'val1', bar: 'val2'};
  1345. const countMethod = where => {
  1346. if (invoked === 0) {
  1347. expect(where).to.be.eql({[DEF_PK]: idValue});
  1348. } else if (invoked === 1) {
  1349. expect(where).to.be.eql({foo: 'val1'});
  1350. } else if (invoked === 2) {
  1351. expect(where).to.be.eql({bar: 'val2'});
  1352. }
  1353. invoked++;
  1354. return 0;
  1355. };
  1356. await puv.validate(
  1357. countMethod,
  1358. 'replaceOrCreate',
  1359. 'model',
  1360. modelData,
  1361. );
  1362. expect(invoked).to.be.eq(3);
  1363. });
  1364. });
  1365. describe('a document of the given identifier already exist', function () {
  1366. it('uses the default primary key to check existence of the given identifier', async function () {
  1367. const dbs = new DatabaseSchema();
  1368. dbs.defineModel({
  1369. name: 'model',
  1370. properties: {
  1371. foo: {
  1372. type: DataType.ANY,
  1373. unique: PropertyUniqueness.STRICT,
  1374. },
  1375. },
  1376. });
  1377. const puv = dbs.getService(PropertyUniquenessValidator);
  1378. let invoked = 0;
  1379. const idValue = 1;
  1380. const modelData = {
  1381. [DEF_PK]: idValue,
  1382. foo: 'bar',
  1383. };
  1384. const countMethod = where => {
  1385. invoked++;
  1386. if (invoked === 1) {
  1387. expect(where).to.be.eql({
  1388. [DEF_PK]: idValue,
  1389. });
  1390. return 1;
  1391. } else if (invoked === 2) {
  1392. expect(where).to.be.eql({
  1393. [DEF_PK]: {neq: idValue},
  1394. foo: 'bar',
  1395. });
  1396. return 0;
  1397. }
  1398. };
  1399. await puv.validate(
  1400. countMethod,
  1401. 'replaceOrCreate',
  1402. 'model',
  1403. modelData,
  1404. );
  1405. expect(invoked).to.be.eq(2);
  1406. });
  1407. it('uses a custom primary key to check existence of the given identifier', async function () {
  1408. const dbs = new DatabaseSchema();
  1409. dbs.defineModel({
  1410. name: 'model',
  1411. properties: {
  1412. myId: {
  1413. type: DataType.NUMBER,
  1414. primaryKey: true,
  1415. },
  1416. foo: {
  1417. type: DataType.ANY,
  1418. unique: PropertyUniqueness.STRICT,
  1419. },
  1420. },
  1421. });
  1422. const puv = dbs.getService(PropertyUniquenessValidator);
  1423. let invoked = 0;
  1424. const idValue = 1;
  1425. const modelData = {myId: idValue, foo: 'bar'};
  1426. const countMethod = where => {
  1427. invoked++;
  1428. if (invoked === 1) {
  1429. expect(where).to.be.eql({
  1430. myId: idValue,
  1431. });
  1432. return 1;
  1433. } else if (invoked === 2) {
  1434. expect(where).to.be.eql({
  1435. myId: {neq: idValue},
  1436. foo: 'bar',
  1437. });
  1438. return 0;
  1439. }
  1440. };
  1441. await puv.validate(
  1442. countMethod,
  1443. 'replaceOrCreate',
  1444. 'model',
  1445. modelData,
  1446. idValue,
  1447. );
  1448. expect(invoked).to.be.eq(2);
  1449. });
  1450. it('checks the given identifier only once', async function () {
  1451. const dbs = new DatabaseSchema();
  1452. dbs.defineModel({
  1453. name: 'model',
  1454. properties: {
  1455. foo: {
  1456. type: DataType.ANY,
  1457. unique: PropertyUniqueness.STRICT,
  1458. },
  1459. bar: {
  1460. type: DataType.ANY,
  1461. unique: PropertyUniqueness.STRICT,
  1462. },
  1463. },
  1464. });
  1465. const puv = dbs.getService(PropertyUniquenessValidator);
  1466. let invoked = 0;
  1467. const idValue = 1;
  1468. const modelData = {
  1469. [DEF_PK]: idValue,
  1470. foo: 'val1',
  1471. bar: 'val2',
  1472. };
  1473. const countMethod = where => {
  1474. invoked++;
  1475. if (invoked === 1) {
  1476. expect(where).to.be.eql({[DEF_PK]: idValue});
  1477. return 1;
  1478. } else if (invoked === 2) {
  1479. expect(where).to.be.eql({
  1480. [DEF_PK]: {neq: idValue},
  1481. foo: 'val1',
  1482. });
  1483. return 0;
  1484. } else if (invoked === 3) {
  1485. expect(where).to.be.eql({
  1486. [DEF_PK]: {neq: idValue},
  1487. bar: 'val2',
  1488. });
  1489. return 0;
  1490. }
  1491. };
  1492. await puv.validate(
  1493. countMethod,
  1494. 'replaceOrCreate',
  1495. 'model',
  1496. modelData,
  1497. );
  1498. expect(invoked).to.be.eq(3);
  1499. });
  1500. });
  1501. });
  1502. });
  1503. describe('patch', function () {
  1504. it('throws an error if the "countMethod" returns a positive number', async function () {
  1505. const dbs = new DatabaseSchema();
  1506. dbs.defineModel({
  1507. name: 'model',
  1508. properties: {
  1509. foo: {
  1510. type: DataType.ANY,
  1511. unique: PropertyUniqueness.STRICT,
  1512. },
  1513. },
  1514. });
  1515. const puv = dbs.getService(PropertyUniquenessValidator);
  1516. const promise = puv.validate(() => 1, 'patch', 'model', {foo: 'bar'});
  1517. await expect(promise).to.be.rejectedWith(
  1518. 'An existing document of the model "model" already has ' +
  1519. 'the property "foo" with the value "bar" and should be unique.',
  1520. );
  1521. });
  1522. it('passes validation if the "countMethod" returns zero', async function () {
  1523. const dbs = new DatabaseSchema();
  1524. dbs.defineModel({
  1525. name: 'model',
  1526. properties: {
  1527. foo: {
  1528. type: DataType.ANY,
  1529. unique: PropertyUniqueness.STRICT,
  1530. },
  1531. },
  1532. });
  1533. const puv = dbs.getService(PropertyUniquenessValidator);
  1534. await puv.validate(() => 0, 'patch', 'model', {foo: 'bar'});
  1535. });
  1536. it('invokes the "countMethod" for given properties which should be unique', async function () {
  1537. const dbs = new DatabaseSchema();
  1538. dbs.defineModel({
  1539. name: 'model',
  1540. properties: {
  1541. foo: {
  1542. type: DataType.ANY,
  1543. unique: PropertyUniqueness.STRICT,
  1544. },
  1545. bar: {
  1546. type: DataType.ANY,
  1547. unique: false,
  1548. },
  1549. baz: {
  1550. type: DataType.ANY,
  1551. unique: PropertyUniqueness.STRICT,
  1552. },
  1553. },
  1554. });
  1555. const puv = dbs.getService(PropertyUniquenessValidator);
  1556. let invoked = 0;
  1557. const modelData = {foo: 'val1', bar: 'val2'};
  1558. const countMethod = where => {
  1559. if (invoked === 0) expect(where).to.be.eql({foo: 'val1'});
  1560. invoked++;
  1561. return 0;
  1562. };
  1563. await puv.validate(countMethod, 'patch', 'model', modelData);
  1564. expect(invoked).to.be.eq(1);
  1565. });
  1566. it('skips uniqueness checking for non-provided fields', async function () {
  1567. const dbs = new DatabaseSchema();
  1568. dbs.defineModel({
  1569. name: 'model',
  1570. properties: {
  1571. foo: {
  1572. type: DataType.ANY,
  1573. unique: PropertyUniqueness.STRICT,
  1574. },
  1575. },
  1576. });
  1577. const puv = dbs.getService(PropertyUniquenessValidator);
  1578. const promise1 = puv.validate(() => 1, 'patch', 'model', {
  1579. foo: 'bar',
  1580. });
  1581. const promise2 = puv.validate(() => 1, 'patch', 'model', {
  1582. baz: 'qux',
  1583. });
  1584. await expect(promise1).to.be.rejectedWith(
  1585. 'An existing document of the model "model" already has ' +
  1586. 'the property "foo" with the value "bar" and should be unique.',
  1587. );
  1588. await expect(promise2).not.to.be.rejected;
  1589. });
  1590. });
  1591. describe('patchById', function () {
  1592. it('throws an error if the "countMethod" returns a positive number', async function () {
  1593. const dbs = new DatabaseSchema();
  1594. dbs.defineModel({
  1595. name: 'model',
  1596. properties: {
  1597. foo: {
  1598. type: DataType.ANY,
  1599. unique: PropertyUniqueness.STRICT,
  1600. },
  1601. },
  1602. });
  1603. const puv = dbs.getService(PropertyUniquenessValidator);
  1604. const promise = puv.validate(
  1605. () => 1,
  1606. 'patchById',
  1607. 'model',
  1608. {foo: 'bar'},
  1609. 1,
  1610. );
  1611. await expect(promise).to.be.rejectedWith(
  1612. 'An existing document of the model "model" already has ' +
  1613. 'the property "foo" with the value "bar" and should be unique.',
  1614. );
  1615. });
  1616. it('passes validation if the "countMethod" returns zero', async function () {
  1617. const dbs = new DatabaseSchema();
  1618. dbs.defineModel({
  1619. name: 'model',
  1620. properties: {
  1621. foo: {
  1622. type: DataType.ANY,
  1623. unique: PropertyUniqueness.STRICT,
  1624. },
  1625. },
  1626. });
  1627. const puv = dbs.getService(PropertyUniquenessValidator);
  1628. await puv.validate(() => 0, 'patchById', 'model', {foo: 'bar'}, 1);
  1629. });
  1630. it('invokes the "countMethod" for given properties which should be unique', async function () {
  1631. const dbs = new DatabaseSchema();
  1632. dbs.defineModel({
  1633. name: 'model',
  1634. properties: {
  1635. foo: {
  1636. type: DataType.ANY,
  1637. unique: PropertyUniqueness.STRICT,
  1638. },
  1639. bar: {
  1640. type: DataType.ANY,
  1641. unique: false,
  1642. },
  1643. baz: {
  1644. type: DataType.ANY,
  1645. unique: PropertyUniqueness.STRICT,
  1646. },
  1647. },
  1648. });
  1649. const puv = dbs.getService(PropertyUniquenessValidator);
  1650. let invoked = 0;
  1651. const idValue = 1;
  1652. const modelData = {foo: 'val1', bar: 'val2'};
  1653. const countMethod = where => {
  1654. if (invoked === 0)
  1655. expect(where).to.be.eql({
  1656. [DEF_PK]: {neq: idValue},
  1657. foo: 'val1',
  1658. });
  1659. invoked++;
  1660. return 0;
  1661. };
  1662. await puv.validate(
  1663. countMethod,
  1664. 'patchById',
  1665. 'model',
  1666. modelData,
  1667. idValue,
  1668. );
  1669. expect(invoked).to.be.eq(1);
  1670. });
  1671. it('skips uniqueness checking for non-provided fields', async function () {
  1672. const dbs = new DatabaseSchema();
  1673. dbs.defineModel({
  1674. name: 'model',
  1675. properties: {
  1676. foo: {
  1677. type: DataType.ANY,
  1678. unique: PropertyUniqueness.STRICT,
  1679. },
  1680. },
  1681. });
  1682. const puv = dbs.getService(PropertyUniquenessValidator);
  1683. const promise1 = puv.validate(() => 1, 'patchById', 'model', {
  1684. foo: 'bar',
  1685. });
  1686. const promise2 = puv.validate(() => 1, 'patchById', 'model', {
  1687. baz: 'qux',
  1688. });
  1689. await expect(promise1).to.be.rejectedWith(
  1690. 'An existing document of the model "model" already has ' +
  1691. 'the property "foo" with the value "bar" and should be unique.',
  1692. );
  1693. await expect(promise2).not.to.be.rejected;
  1694. });
  1695. it('uses a custom primary key to check existence of the given identifier', async function () {
  1696. const dbs = new DatabaseSchema();
  1697. dbs.defineModel({
  1698. name: 'model',
  1699. properties: {
  1700. myId: {
  1701. type: DataType.NUMBER,
  1702. primaryKey: true,
  1703. },
  1704. foo: {
  1705. type: DataType.ANY,
  1706. unique: PropertyUniqueness.STRICT,
  1707. },
  1708. },
  1709. });
  1710. const puv = dbs.getService(PropertyUniquenessValidator);
  1711. let invoked = 0;
  1712. const idValue = 1;
  1713. const modelData = {foo: 'bar'};
  1714. const countMethod = where => {
  1715. if (invoked === 0)
  1716. expect(where).to.be.eql({
  1717. myId: {neq: idValue},
  1718. foo: 'bar',
  1719. });
  1720. invoked++;
  1721. return 0;
  1722. };
  1723. await puv.validate(
  1724. countMethod,
  1725. 'patchById',
  1726. 'model',
  1727. modelData,
  1728. idValue,
  1729. );
  1730. expect(invoked).to.be.eq(1);
  1731. });
  1732. });
  1733. });
  1734. describe('the "unique" option is "sparse"', function () {
  1735. describe('create', function () {
  1736. it('throws an error if the "countMethod" returns a positive number', async function () {
  1737. const dbs = new DatabaseSchema();
  1738. dbs.defineModel({
  1739. name: 'model',
  1740. properties: {
  1741. foo: {
  1742. type: DataType.ANY,
  1743. unique: PropertyUniqueness.SPARSE,
  1744. },
  1745. },
  1746. });
  1747. const puv = dbs.getService(PropertyUniquenessValidator);
  1748. const promise = puv.validate(() => 1, 'create', 'model', {
  1749. foo: 'bar',
  1750. });
  1751. await expect(promise).to.be.rejectedWith(
  1752. 'An existing document of the model "model" already has ' +
  1753. 'the property "foo" with the value "bar" and should be unique.',
  1754. );
  1755. });
  1756. it('passes validation if the "countMethod" returns zero', async function () {
  1757. const dbs = new DatabaseSchema();
  1758. dbs.defineModel({
  1759. name: 'model',
  1760. properties: {
  1761. foo: {
  1762. type: DataType.ANY,
  1763. unique: PropertyUniqueness.SPARSE,
  1764. },
  1765. },
  1766. });
  1767. const puv = dbs.getService(PropertyUniquenessValidator);
  1768. await puv.validate(() => 0, 'create', 'model', {foo: 'bar'});
  1769. });
  1770. it('invokes the "countMethod" for given properties which should be unique', async function () {
  1771. const dbs = new DatabaseSchema();
  1772. dbs.defineModel({
  1773. name: 'model',
  1774. properties: {
  1775. foo: {
  1776. type: DataType.ANY,
  1777. unique: PropertyUniqueness.SPARSE,
  1778. },
  1779. bar: {
  1780. type: DataType.ANY,
  1781. unique: false,
  1782. },
  1783. baz: {
  1784. type: DataType.ANY,
  1785. unique: PropertyUniqueness.SPARSE,
  1786. },
  1787. },
  1788. });
  1789. const puv = dbs.getService(PropertyUniquenessValidator);
  1790. let invoked = 0;
  1791. const modelData = {foo: 'val1', bar: 'val2'};
  1792. const countMethod = where => {
  1793. if (invoked === 0) expect(where).to.be.eql({foo: 'val1'});
  1794. invoked++;
  1795. return 0;
  1796. };
  1797. await puv.validate(countMethod, 'create', 'model', modelData);
  1798. expect(invoked).to.be.eq(1);
  1799. });
  1800. it('skips uniqueness checking for an undefined value', async function () {
  1801. const dbs = new DatabaseSchema();
  1802. dbs.defineModel({
  1803. name: 'model',
  1804. properties: {
  1805. foo: {
  1806. type: DataType.STRING,
  1807. unique: PropertyUniqueness.SPARSE,
  1808. },
  1809. bar: {
  1810. type: DataType.STRING,
  1811. unique: PropertyUniqueness.SPARSE,
  1812. },
  1813. },
  1814. });
  1815. const puv = dbs.getService(PropertyUniquenessValidator);
  1816. let invoked = 0;
  1817. const modelData = {foo: 'val1', bar: undefined};
  1818. const countMethod = where => {
  1819. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  1820. invoked++;
  1821. return 0;
  1822. };
  1823. await puv.validate(countMethod, 'create', 'model', modelData);
  1824. expect(invoked).to.be.eql(1);
  1825. });
  1826. it('skips uniqueness checking for an empty string', async function () {
  1827. const dbs = new DatabaseSchema();
  1828. dbs.defineModel({
  1829. name: 'model',
  1830. properties: {
  1831. foo: {
  1832. type: DataType.STRING,
  1833. unique: PropertyUniqueness.SPARSE,
  1834. },
  1835. bar: {
  1836. type: DataType.STRING,
  1837. unique: PropertyUniqueness.SPARSE,
  1838. },
  1839. },
  1840. });
  1841. const puv = dbs.getService(PropertyUniquenessValidator);
  1842. let invoked = 0;
  1843. const modelData = {foo: 'val1', bar: ''};
  1844. const countMethod = where => {
  1845. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  1846. invoked++;
  1847. return 0;
  1848. };
  1849. await puv.validate(countMethod, 'create', 'model', modelData);
  1850. expect(invoked).to.be.eql(1);
  1851. });
  1852. it('skips uniqueness checking for a zero number', async function () {
  1853. const dbs = new DatabaseSchema();
  1854. dbs.defineModel({
  1855. name: 'model',
  1856. properties: {
  1857. foo: {
  1858. type: DataType.NUMBER,
  1859. unique: PropertyUniqueness.SPARSE,
  1860. },
  1861. bar: {
  1862. type: DataType.NUMBER,
  1863. unique: PropertyUniqueness.SPARSE,
  1864. },
  1865. },
  1866. });
  1867. const puv = dbs.getService(PropertyUniquenessValidator);
  1868. let invoked = 0;
  1869. const modelData = {foo: 10, bar: 0};
  1870. const countMethod = where => {
  1871. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  1872. invoked++;
  1873. return 0;
  1874. };
  1875. await puv.validate(countMethod, 'create', 'model', modelData);
  1876. expect(invoked).to.be.eql(1);
  1877. });
  1878. it('skips uniqueness checking for a false value', async function () {
  1879. const dbs = new DatabaseSchema();
  1880. dbs.defineModel({
  1881. name: 'model',
  1882. properties: {
  1883. foo: {
  1884. type: DataType.BOOLEAN,
  1885. unique: PropertyUniqueness.SPARSE,
  1886. },
  1887. bar: {
  1888. type: DataType.BOOLEAN,
  1889. unique: PropertyUniqueness.SPARSE,
  1890. },
  1891. },
  1892. });
  1893. const puv = dbs.getService(PropertyUniquenessValidator);
  1894. let invoked = 0;
  1895. const modelData = {foo: true, bar: false};
  1896. const countMethod = where => {
  1897. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  1898. invoked++;
  1899. return 0;
  1900. };
  1901. await puv.validate(countMethod, 'create', 'model', modelData);
  1902. expect(invoked).to.be.eql(1);
  1903. });
  1904. });
  1905. describe('replaceById', function () {
  1906. it('throws an error if the "countMethod" returns a positive number', async function () {
  1907. const dbs = new DatabaseSchema();
  1908. dbs.defineModel({
  1909. name: 'model',
  1910. properties: {
  1911. foo: {
  1912. type: DataType.ANY,
  1913. unique: PropertyUniqueness.SPARSE,
  1914. },
  1915. },
  1916. });
  1917. const puv = dbs.getService(PropertyUniquenessValidator);
  1918. const promise = puv.validate(
  1919. () => 1,
  1920. 'replaceById',
  1921. 'model',
  1922. {foo: 'bar'},
  1923. 1,
  1924. );
  1925. await expect(promise).to.be.rejectedWith(
  1926. 'An existing document of the model "model" already has ' +
  1927. 'the property "foo" with the value "bar" and should be unique.',
  1928. );
  1929. });
  1930. it('passes validation if the "countMethod" returns zero', async function () {
  1931. const dbs = new DatabaseSchema();
  1932. dbs.defineModel({
  1933. name: 'model',
  1934. properties: {
  1935. foo: {
  1936. type: DataType.ANY,
  1937. unique: PropertyUniqueness.SPARSE,
  1938. },
  1939. },
  1940. });
  1941. const puv = dbs.getService(PropertyUniquenessValidator);
  1942. await puv.validate(() => 0, 'replaceById', 'model', {foo: 'bar'}, 1);
  1943. });
  1944. it('invokes the "countMethod" for given properties which should be unique', async function () {
  1945. const dbs = new DatabaseSchema();
  1946. dbs.defineModel({
  1947. name: 'model',
  1948. properties: {
  1949. foo: {
  1950. type: DataType.ANY,
  1951. unique: PropertyUniqueness.SPARSE,
  1952. },
  1953. bar: {
  1954. type: DataType.ANY,
  1955. unique: false,
  1956. },
  1957. baz: {
  1958. type: DataType.ANY,
  1959. unique: PropertyUniqueness.SPARSE,
  1960. },
  1961. },
  1962. });
  1963. const puv = dbs.getService(PropertyUniquenessValidator);
  1964. let invoked = 0;
  1965. const idValue = 1;
  1966. const modelData = {foo: 'val1', bar: 'val2'};
  1967. const countMethod = where => {
  1968. if (invoked === 0)
  1969. expect(where).to.be.eql({
  1970. [DEF_PK]: {neq: idValue},
  1971. foo: 'val1',
  1972. });
  1973. invoked++;
  1974. return 0;
  1975. };
  1976. await puv.validate(
  1977. countMethod,
  1978. 'replaceById',
  1979. 'model',
  1980. modelData,
  1981. idValue,
  1982. );
  1983. expect(invoked).to.be.eq(1);
  1984. });
  1985. it('skips uniqueness checking for an undefined value', async function () {
  1986. const dbs = new DatabaseSchema();
  1987. dbs.defineModel({
  1988. name: 'model',
  1989. properties: {
  1990. foo: {
  1991. type: DataType.STRING,
  1992. unique: PropertyUniqueness.SPARSE,
  1993. },
  1994. bar: {
  1995. type: DataType.STRING,
  1996. unique: PropertyUniqueness.SPARSE,
  1997. },
  1998. },
  1999. });
  2000. const puv = dbs.getService(PropertyUniquenessValidator);
  2001. let invoked = 0;
  2002. const idValue = 1;
  2003. const modelData = {foo: 'val1', bar: undefined};
  2004. const countMethod = where => {
  2005. if (invoked === 0)
  2006. expect(where).to.be.eql({
  2007. [DEF_PK]: {neq: idValue},
  2008. foo: modelData.foo,
  2009. });
  2010. invoked++;
  2011. return 0;
  2012. };
  2013. await puv.validate(
  2014. countMethod,
  2015. 'replaceById',
  2016. 'model',
  2017. modelData,
  2018. idValue,
  2019. );
  2020. expect(invoked).to.be.eql(1);
  2021. });
  2022. it('skips uniqueness checking for an empty string', async function () {
  2023. const dbs = new DatabaseSchema();
  2024. dbs.defineModel({
  2025. name: 'model',
  2026. properties: {
  2027. foo: {
  2028. type: DataType.STRING,
  2029. unique: PropertyUniqueness.SPARSE,
  2030. },
  2031. bar: {
  2032. type: DataType.STRING,
  2033. unique: PropertyUniqueness.SPARSE,
  2034. },
  2035. },
  2036. });
  2037. const puv = dbs.getService(PropertyUniquenessValidator);
  2038. let invoked = 0;
  2039. const idValue = 1;
  2040. const modelData = {foo: 'val1', bar: ''};
  2041. const countMethod = where => {
  2042. if (invoked === 0)
  2043. expect(where).to.be.eql({
  2044. [DEF_PK]: {neq: idValue},
  2045. foo: modelData.foo,
  2046. });
  2047. invoked++;
  2048. return 0;
  2049. };
  2050. await puv.validate(
  2051. countMethod,
  2052. 'replaceById',
  2053. 'model',
  2054. modelData,
  2055. idValue,
  2056. );
  2057. expect(invoked).to.be.eql(1);
  2058. });
  2059. it('skips uniqueness checking for a zero number', async function () {
  2060. const dbs = new DatabaseSchema();
  2061. dbs.defineModel({
  2062. name: 'model',
  2063. properties: {
  2064. foo: {
  2065. type: DataType.NUMBER,
  2066. unique: PropertyUniqueness.SPARSE,
  2067. },
  2068. bar: {
  2069. type: DataType.NUMBER,
  2070. unique: PropertyUniqueness.SPARSE,
  2071. },
  2072. },
  2073. });
  2074. const puv = dbs.getService(PropertyUniquenessValidator);
  2075. let invoked = 0;
  2076. const idValue = 1;
  2077. const modelData = {foo: 10, bar: 0};
  2078. const countMethod = where => {
  2079. if (invoked === 0)
  2080. expect(where).to.be.eql({
  2081. [DEF_PK]: {neq: idValue},
  2082. foo: modelData.foo,
  2083. });
  2084. invoked++;
  2085. return 0;
  2086. };
  2087. await puv.validate(
  2088. countMethod,
  2089. 'replaceById',
  2090. 'model',
  2091. modelData,
  2092. idValue,
  2093. );
  2094. expect(invoked).to.be.eql(1);
  2095. });
  2096. it('skips uniqueness checking for a false value', async function () {
  2097. const dbs = new DatabaseSchema();
  2098. dbs.defineModel({
  2099. name: 'model',
  2100. properties: {
  2101. foo: {
  2102. type: DataType.BOOLEAN,
  2103. unique: PropertyUniqueness.SPARSE,
  2104. },
  2105. bar: {
  2106. type: DataType.BOOLEAN,
  2107. unique: PropertyUniqueness.SPARSE,
  2108. },
  2109. },
  2110. });
  2111. const puv = dbs.getService(PropertyUniquenessValidator);
  2112. let invoked = 0;
  2113. const idValue = 1;
  2114. const modelData = {foo: true, bar: false};
  2115. const countMethod = where => {
  2116. if (invoked === 0)
  2117. expect(where).to.be.eql({
  2118. [DEF_PK]: {neq: idValue},
  2119. foo: modelData.foo,
  2120. });
  2121. invoked++;
  2122. return 0;
  2123. };
  2124. await puv.validate(
  2125. countMethod,
  2126. 'replaceById',
  2127. 'model',
  2128. modelData,
  2129. idValue,
  2130. );
  2131. expect(invoked).to.be.eql(1);
  2132. });
  2133. it('uses a custom primary key to check existence of the given identifier', async function () {
  2134. const dbs = new DatabaseSchema();
  2135. dbs.defineModel({
  2136. name: 'model',
  2137. properties: {
  2138. myId: {
  2139. type: DataType.NUMBER,
  2140. primaryKey: true,
  2141. },
  2142. foo: {
  2143. type: DataType.ANY,
  2144. unique: PropertyUniqueness.SPARSE,
  2145. },
  2146. },
  2147. });
  2148. const puv = dbs.getService(PropertyUniquenessValidator);
  2149. let invoked = 0;
  2150. const idValue = 1;
  2151. const modelData = {foo: 'bar'};
  2152. const countMethod = where => {
  2153. if (invoked === 0)
  2154. expect(where).to.be.eql({
  2155. myId: {neq: idValue},
  2156. foo: 'bar',
  2157. });
  2158. invoked++;
  2159. return 0;
  2160. };
  2161. await puv.validate(
  2162. countMethod,
  2163. 'replaceById',
  2164. 'model',
  2165. modelData,
  2166. idValue,
  2167. );
  2168. expect(invoked).to.be.eq(1);
  2169. });
  2170. });
  2171. describe('replaceOrCreate', function () {
  2172. it('throws an error if the "countMethod" returns a positive number', async function () {
  2173. const dbs = new DatabaseSchema();
  2174. dbs.defineModel({
  2175. name: 'model',
  2176. properties: {
  2177. foo: {
  2178. type: DataType.ANY,
  2179. unique: PropertyUniqueness.SPARSE,
  2180. },
  2181. },
  2182. });
  2183. const puv = dbs.getService(PropertyUniquenessValidator);
  2184. const promise = puv.validate(() => 1, 'replaceOrCreate', 'model', {
  2185. foo: 'bar',
  2186. });
  2187. await expect(promise).to.be.rejectedWith(
  2188. 'An existing document of the model "model" already has ' +
  2189. 'the property "foo" with the value "bar" and should be unique.',
  2190. );
  2191. });
  2192. it('passes validation if the "countMethod" returns zero', async function () {
  2193. const dbs = new DatabaseSchema();
  2194. dbs.defineModel({
  2195. name: 'model',
  2196. properties: {
  2197. foo: {
  2198. type: DataType.ANY,
  2199. unique: PropertyUniqueness.SPARSE,
  2200. },
  2201. },
  2202. });
  2203. const puv = dbs.getService(PropertyUniquenessValidator);
  2204. await puv.validate(() => 0, 'replaceOrCreate', 'model', {foo: 'bar'});
  2205. });
  2206. it('invokes the "countMethod" for given properties which should be unique', async function () {
  2207. const dbs = new DatabaseSchema();
  2208. dbs.defineModel({
  2209. name: 'model',
  2210. properties: {
  2211. foo: {
  2212. type: DataType.ANY,
  2213. unique: PropertyUniqueness.SPARSE,
  2214. },
  2215. bar: {
  2216. type: DataType.ANY,
  2217. unique: false,
  2218. },
  2219. baz: {
  2220. type: DataType.ANY,
  2221. unique: PropertyUniqueness.SPARSE,
  2222. },
  2223. },
  2224. });
  2225. const puv = dbs.getService(PropertyUniquenessValidator);
  2226. let invoked = 0;
  2227. const modelData = {foo: 'val1', bar: 'val2'};
  2228. const countMethod = where => {
  2229. if (invoked === 0) expect(where).to.be.eql({foo: 'val1'});
  2230. invoked++;
  2231. return 0;
  2232. };
  2233. await puv.validate(
  2234. countMethod,
  2235. 'replaceOrCreate',
  2236. 'model',
  2237. modelData,
  2238. );
  2239. expect(invoked).to.be.eq(1);
  2240. });
  2241. describe('in case that the given model has a document identifier', function () {
  2242. describe('a document of the given identifier does not exist', function () {
  2243. it('uses the default primary key to check existence of the given identifier', async function () {
  2244. const dbs = new DatabaseSchema();
  2245. dbs.defineModel({
  2246. name: 'model',
  2247. properties: {
  2248. foo: {
  2249. type: DataType.ANY,
  2250. unique: PropertyUniqueness.SPARSE,
  2251. },
  2252. },
  2253. });
  2254. const puv = dbs.getService(PropertyUniquenessValidator);
  2255. let invoked = 0;
  2256. const idValue = 1;
  2257. const modelData = {[DEF_PK]: idValue, foo: 'bar'};
  2258. const countMethod = where => {
  2259. if (invoked === 0) {
  2260. expect(where).to.be.eql({[DEF_PK]: idValue});
  2261. } else if (invoked === 1) {
  2262. expect(where).to.be.eql({foo: 'bar'});
  2263. }
  2264. invoked++;
  2265. return 0;
  2266. };
  2267. await puv.validate(
  2268. countMethod,
  2269. 'replaceOrCreate',
  2270. 'model',
  2271. modelData,
  2272. );
  2273. expect(invoked).to.be.eq(2);
  2274. });
  2275. it('uses a custom primary key to check existence of the given identifier', async function () {
  2276. const dbs = new DatabaseSchema();
  2277. dbs.defineModel({
  2278. name: 'model',
  2279. properties: {
  2280. myId: {
  2281. type: DataType.NUMBER,
  2282. primaryKey: true,
  2283. },
  2284. foo: {
  2285. type: DataType.ANY,
  2286. unique: PropertyUniqueness.SPARSE,
  2287. },
  2288. },
  2289. });
  2290. const puv = dbs.getService(PropertyUniquenessValidator);
  2291. let invoked = 0;
  2292. const idValue = 1;
  2293. const modelData = {myId: idValue, foo: 'bar'};
  2294. const countMethod = where => {
  2295. if (invoked === 0) {
  2296. expect(where).to.be.eql({myId: idValue});
  2297. } else if (invoked === 1) {
  2298. expect(where).to.be.eql({foo: 'bar'});
  2299. }
  2300. invoked++;
  2301. return 0;
  2302. };
  2303. await puv.validate(
  2304. countMethod,
  2305. 'replaceOrCreate',
  2306. 'model',
  2307. modelData,
  2308. idValue,
  2309. );
  2310. expect(invoked).to.be.eq(2);
  2311. });
  2312. it('checks the given identifier only once', async function () {
  2313. const dbs = new DatabaseSchema();
  2314. dbs.defineModel({
  2315. name: 'model',
  2316. properties: {
  2317. foo: {
  2318. type: DataType.ANY,
  2319. unique: PropertyUniqueness.SPARSE,
  2320. },
  2321. bar: {
  2322. type: DataType.ANY,
  2323. unique: PropertyUniqueness.SPARSE,
  2324. },
  2325. },
  2326. });
  2327. const puv = dbs.getService(PropertyUniquenessValidator);
  2328. let invoked = 0;
  2329. const idValue = 1;
  2330. const modelData = {[DEF_PK]: idValue, foo: 'val1', bar: 'val2'};
  2331. const countMethod = where => {
  2332. if (invoked === 0) {
  2333. expect(where).to.be.eql({[DEF_PK]: idValue});
  2334. } else if (invoked === 1) {
  2335. expect(where).to.be.eql({foo: 'val1'});
  2336. } else if (invoked === 2) {
  2337. expect(where).to.be.eql({bar: 'val2'});
  2338. }
  2339. invoked++;
  2340. return 0;
  2341. };
  2342. await puv.validate(
  2343. countMethod,
  2344. 'replaceOrCreate',
  2345. 'model',
  2346. modelData,
  2347. );
  2348. expect(invoked).to.be.eq(3);
  2349. });
  2350. it('skips uniqueness checking for an undefined value', async function () {
  2351. const dbs = new DatabaseSchema();
  2352. dbs.defineModel({
  2353. name: 'model',
  2354. properties: {
  2355. foo: {
  2356. type: DataType.STRING,
  2357. unique: PropertyUniqueness.SPARSE,
  2358. },
  2359. bar: {
  2360. type: DataType.STRING,
  2361. unique: PropertyUniqueness.SPARSE,
  2362. },
  2363. },
  2364. });
  2365. const puv = dbs.getService(PropertyUniquenessValidator);
  2366. let invoked = 0;
  2367. const idValue = 1;
  2368. const modelData = {
  2369. [DEF_PK]: idValue,
  2370. foo: 'val1',
  2371. bar: undefined,
  2372. };
  2373. const countMethod = where => {
  2374. if (invoked === 0) {
  2375. expect(where).to.be.eql({[DEF_PK]: idValue});
  2376. } else if (invoked === 1) {
  2377. expect(where).to.be.eql({foo: modelData.foo});
  2378. }
  2379. invoked++;
  2380. return 0;
  2381. };
  2382. await puv.validate(
  2383. countMethod,
  2384. 'replaceOrCreate',
  2385. 'model',
  2386. modelData,
  2387. );
  2388. expect(invoked).to.be.eql(2);
  2389. });
  2390. it('skips uniqueness checking for an empty string', async function () {
  2391. const dbs = new DatabaseSchema();
  2392. dbs.defineModel({
  2393. name: 'model',
  2394. properties: {
  2395. foo: {
  2396. type: DataType.STRING,
  2397. unique: PropertyUniqueness.SPARSE,
  2398. },
  2399. bar: {
  2400. type: DataType.STRING,
  2401. unique: PropertyUniqueness.SPARSE,
  2402. },
  2403. },
  2404. });
  2405. const puv = dbs.getService(PropertyUniquenessValidator);
  2406. let invoked = 0;
  2407. const idValue = 1;
  2408. const modelData = {
  2409. [DEF_PK]: idValue,
  2410. foo: 'val1',
  2411. bar: '',
  2412. };
  2413. const countMethod = where => {
  2414. if (invoked === 0) {
  2415. expect(where).to.be.eql({[DEF_PK]: idValue});
  2416. } else if (invoked === 1) {
  2417. expect(where).to.be.eql({foo: modelData.foo});
  2418. }
  2419. invoked++;
  2420. return 0;
  2421. };
  2422. await puv.validate(
  2423. countMethod,
  2424. 'replaceOrCreate',
  2425. 'model',
  2426. modelData,
  2427. );
  2428. expect(invoked).to.be.eql(2);
  2429. });
  2430. it('skips uniqueness checking for a zero number', async function () {
  2431. const dbs = new DatabaseSchema();
  2432. dbs.defineModel({
  2433. name: 'model',
  2434. properties: {
  2435. foo: {
  2436. type: DataType.NUMBER,
  2437. unique: PropertyUniqueness.SPARSE,
  2438. },
  2439. bar: {
  2440. type: DataType.NUMBER,
  2441. unique: PropertyUniqueness.SPARSE,
  2442. },
  2443. },
  2444. });
  2445. const puv = dbs.getService(PropertyUniquenessValidator);
  2446. let invoked = 0;
  2447. const idValue = 1;
  2448. const modelData = {
  2449. [DEF_PK]: idValue,
  2450. foo: 10,
  2451. bar: 0,
  2452. };
  2453. const countMethod = where => {
  2454. if (invoked === 0) {
  2455. expect(where).to.be.eql({[DEF_PK]: idValue});
  2456. } else if (invoked === 1) {
  2457. expect(where).to.be.eql({foo: modelData.foo});
  2458. }
  2459. invoked++;
  2460. return 0;
  2461. };
  2462. await puv.validate(
  2463. countMethod,
  2464. 'replaceOrCreate',
  2465. 'model',
  2466. modelData,
  2467. );
  2468. expect(invoked).to.be.eql(2);
  2469. });
  2470. it('skips uniqueness checking for a false value', async function () {
  2471. const dbs = new DatabaseSchema();
  2472. dbs.defineModel({
  2473. name: 'model',
  2474. properties: {
  2475. foo: {
  2476. type: DataType.BOOLEAN,
  2477. unique: PropertyUniqueness.SPARSE,
  2478. },
  2479. bar: {
  2480. type: DataType.BOOLEAN,
  2481. unique: PropertyUniqueness.SPARSE,
  2482. },
  2483. },
  2484. });
  2485. const puv = dbs.getService(PropertyUniquenessValidator);
  2486. let invoked = 0;
  2487. const idValue = 1;
  2488. const modelData = {
  2489. [DEF_PK]: idValue,
  2490. foo: true,
  2491. bar: false,
  2492. };
  2493. const countMethod = where => {
  2494. if (invoked === 0) {
  2495. expect(where).to.be.eql({[DEF_PK]: idValue});
  2496. } else if (invoked === 1) {
  2497. expect(where).to.be.eql({foo: modelData.foo});
  2498. }
  2499. invoked++;
  2500. return 0;
  2501. };
  2502. await puv.validate(
  2503. countMethod,
  2504. 'replaceOrCreate',
  2505. 'model',
  2506. modelData,
  2507. );
  2508. expect(invoked).to.be.eql(2);
  2509. });
  2510. });
  2511. describe('a document of the given identifier already exist', function () {
  2512. it('uses the default primary key to check existence of the given identifier', async function () {
  2513. const dbs = new DatabaseSchema();
  2514. dbs.defineModel({
  2515. name: 'model',
  2516. properties: {
  2517. foo: {
  2518. type: DataType.ANY,
  2519. unique: PropertyUniqueness.SPARSE,
  2520. },
  2521. },
  2522. });
  2523. const puv = dbs.getService(PropertyUniquenessValidator);
  2524. let invoked = 0;
  2525. const idValue = 1;
  2526. const modelData = {
  2527. [DEF_PK]: idValue,
  2528. foo: 'bar',
  2529. };
  2530. const countMethod = where => {
  2531. invoked++;
  2532. if (invoked === 1) {
  2533. expect(where).to.be.eql({
  2534. [DEF_PK]: idValue,
  2535. });
  2536. return 1;
  2537. } else if (invoked === 2) {
  2538. expect(where).to.be.eql({
  2539. [DEF_PK]: {neq: idValue},
  2540. foo: 'bar',
  2541. });
  2542. return 0;
  2543. }
  2544. };
  2545. await puv.validate(
  2546. countMethod,
  2547. 'replaceOrCreate',
  2548. 'model',
  2549. modelData,
  2550. );
  2551. expect(invoked).to.be.eq(2);
  2552. });
  2553. it('uses a custom primary key to check existence of the given identifier', async function () {
  2554. const dbs = new DatabaseSchema();
  2555. dbs.defineModel({
  2556. name: 'model',
  2557. properties: {
  2558. myId: {
  2559. type: DataType.NUMBER,
  2560. primaryKey: true,
  2561. },
  2562. foo: {
  2563. type: DataType.ANY,
  2564. unique: PropertyUniqueness.SPARSE,
  2565. },
  2566. },
  2567. });
  2568. const puv = dbs.getService(PropertyUniquenessValidator);
  2569. let invoked = 0;
  2570. const idValue = 1;
  2571. const modelData = {myId: idValue, foo: 'bar'};
  2572. const countMethod = where => {
  2573. invoked++;
  2574. if (invoked === 1) {
  2575. expect(where).to.be.eql({
  2576. myId: idValue,
  2577. });
  2578. return 1;
  2579. } else if (invoked === 2) {
  2580. expect(where).to.be.eql({
  2581. myId: {neq: idValue},
  2582. foo: 'bar',
  2583. });
  2584. return 0;
  2585. }
  2586. };
  2587. await puv.validate(
  2588. countMethod,
  2589. 'replaceOrCreate',
  2590. 'model',
  2591. modelData,
  2592. idValue,
  2593. );
  2594. expect(invoked).to.be.eq(2);
  2595. });
  2596. it('checks the given identifier only once', async function () {
  2597. const dbs = new DatabaseSchema();
  2598. dbs.defineModel({
  2599. name: 'model',
  2600. properties: {
  2601. foo: {
  2602. type: DataType.ANY,
  2603. unique: PropertyUniqueness.SPARSE,
  2604. },
  2605. bar: {
  2606. type: DataType.ANY,
  2607. unique: PropertyUniqueness.SPARSE,
  2608. },
  2609. },
  2610. });
  2611. const puv = dbs.getService(PropertyUniquenessValidator);
  2612. let invoked = 0;
  2613. const idValue = 1;
  2614. const modelData = {
  2615. [DEF_PK]: idValue,
  2616. foo: 'val1',
  2617. bar: 'val2',
  2618. };
  2619. const countMethod = where => {
  2620. invoked++;
  2621. if (invoked === 1) {
  2622. expect(where).to.be.eql({[DEF_PK]: idValue});
  2623. return 1;
  2624. } else if (invoked === 2) {
  2625. expect(where).to.be.eql({
  2626. [DEF_PK]: {neq: idValue},
  2627. foo: 'val1',
  2628. });
  2629. return 0;
  2630. } else if (invoked === 3) {
  2631. expect(where).to.be.eql({
  2632. [DEF_PK]: {neq: idValue},
  2633. bar: 'val2',
  2634. });
  2635. return 0;
  2636. }
  2637. };
  2638. await puv.validate(
  2639. countMethod,
  2640. 'replaceOrCreate',
  2641. 'model',
  2642. modelData,
  2643. );
  2644. expect(invoked).to.be.eq(3);
  2645. });
  2646. it('skips uniqueness checking for an undefined value', async function () {
  2647. const dbs = new DatabaseSchema();
  2648. dbs.defineModel({
  2649. name: 'model',
  2650. properties: {
  2651. foo: {
  2652. type: DataType.STRING,
  2653. unique: PropertyUniqueness.SPARSE,
  2654. },
  2655. bar: {
  2656. type: DataType.STRING,
  2657. unique: PropertyUniqueness.SPARSE,
  2658. },
  2659. },
  2660. });
  2661. const puv = dbs.getService(PropertyUniquenessValidator);
  2662. let invoked = 0;
  2663. const idValue = 1;
  2664. const modelData = {
  2665. [DEF_PK]: idValue,
  2666. foo: 'val1',
  2667. bar: undefined,
  2668. };
  2669. const countMethod = where => {
  2670. invoked++;
  2671. if (invoked === 1) {
  2672. expect(where).to.be.eql({[DEF_PK]: idValue});
  2673. return 1;
  2674. } else if (invoked === 2) {
  2675. expect(where).to.be.eql({
  2676. [DEF_PK]: {neq: idValue},
  2677. foo: modelData.foo,
  2678. });
  2679. return 0;
  2680. }
  2681. };
  2682. await puv.validate(
  2683. countMethod,
  2684. 'replaceOrCreate',
  2685. 'model',
  2686. modelData,
  2687. );
  2688. expect(invoked).to.be.eql(2);
  2689. });
  2690. it('skips uniqueness checking for an empty string', async function () {
  2691. const dbs = new DatabaseSchema();
  2692. dbs.defineModel({
  2693. name: 'model',
  2694. properties: {
  2695. foo: {
  2696. type: DataType.STRING,
  2697. unique: PropertyUniqueness.SPARSE,
  2698. },
  2699. bar: {
  2700. type: DataType.STRING,
  2701. unique: PropertyUniqueness.SPARSE,
  2702. },
  2703. },
  2704. });
  2705. const puv = dbs.getService(PropertyUniquenessValidator);
  2706. let invoked = 0;
  2707. const idValue = 1;
  2708. const modelData = {
  2709. [DEF_PK]: idValue,
  2710. foo: 'val1',
  2711. bar: '',
  2712. };
  2713. const countMethod = where => {
  2714. invoked++;
  2715. if (invoked === 1) {
  2716. expect(where).to.be.eql({[DEF_PK]: idValue});
  2717. return 1;
  2718. } else if (invoked === 2) {
  2719. expect(where).to.be.eql({
  2720. [DEF_PK]: {neq: idValue},
  2721. foo: modelData.foo,
  2722. });
  2723. return 0;
  2724. }
  2725. };
  2726. await puv.validate(
  2727. countMethod,
  2728. 'replaceOrCreate',
  2729. 'model',
  2730. modelData,
  2731. );
  2732. expect(invoked).to.be.eql(2);
  2733. });
  2734. it('skips uniqueness checking for a zero number', async function () {
  2735. const dbs = new DatabaseSchema();
  2736. dbs.defineModel({
  2737. name: 'model',
  2738. properties: {
  2739. foo: {
  2740. type: DataType.NUMBER,
  2741. unique: PropertyUniqueness.SPARSE,
  2742. },
  2743. bar: {
  2744. type: DataType.NUMBER,
  2745. unique: PropertyUniqueness.SPARSE,
  2746. },
  2747. },
  2748. });
  2749. const puv = dbs.getService(PropertyUniquenessValidator);
  2750. let invoked = 0;
  2751. const idValue = 1;
  2752. const modelData = {
  2753. [DEF_PK]: idValue,
  2754. foo: 10,
  2755. bar: 0,
  2756. };
  2757. const countMethod = where => {
  2758. invoked++;
  2759. if (invoked === 1) {
  2760. expect(where).to.be.eql({[DEF_PK]: idValue});
  2761. return 1;
  2762. } else if (invoked === 2) {
  2763. expect(where).to.be.eql({
  2764. [DEF_PK]: {neq: idValue},
  2765. foo: modelData.foo,
  2766. });
  2767. return 0;
  2768. }
  2769. };
  2770. await puv.validate(
  2771. countMethod,
  2772. 'replaceOrCreate',
  2773. 'model',
  2774. modelData,
  2775. );
  2776. expect(invoked).to.be.eql(2);
  2777. });
  2778. it('skips uniqueness checking for a false value', async function () {
  2779. const dbs = new DatabaseSchema();
  2780. dbs.defineModel({
  2781. name: 'model',
  2782. properties: {
  2783. foo: {
  2784. type: DataType.BOOLEAN,
  2785. unique: PropertyUniqueness.SPARSE,
  2786. },
  2787. bar: {
  2788. type: DataType.BOOLEAN,
  2789. unique: PropertyUniqueness.SPARSE,
  2790. },
  2791. },
  2792. });
  2793. const puv = dbs.getService(PropertyUniquenessValidator);
  2794. let invoked = 0;
  2795. const idValue = 1;
  2796. const modelData = {
  2797. [DEF_PK]: idValue,
  2798. foo: true,
  2799. bar: false,
  2800. };
  2801. const countMethod = where => {
  2802. invoked++;
  2803. if (invoked === 1) {
  2804. expect(where).to.be.eql({[DEF_PK]: idValue});
  2805. return 1;
  2806. } else if (invoked === 2) {
  2807. expect(where).to.be.eql({
  2808. [DEF_PK]: {neq: idValue},
  2809. foo: modelData.foo,
  2810. });
  2811. return 0;
  2812. }
  2813. };
  2814. await puv.validate(
  2815. countMethod,
  2816. 'replaceOrCreate',
  2817. 'model',
  2818. modelData,
  2819. );
  2820. expect(invoked).to.be.eql(2);
  2821. });
  2822. });
  2823. });
  2824. });
  2825. describe('patch', function () {
  2826. it('throws an error if the "countMethod" returns a positive number', async function () {
  2827. const dbs = new DatabaseSchema();
  2828. dbs.defineModel({
  2829. name: 'model',
  2830. properties: {
  2831. foo: {
  2832. type: DataType.ANY,
  2833. unique: PropertyUniqueness.SPARSE,
  2834. },
  2835. },
  2836. });
  2837. const puv = dbs.getService(PropertyUniquenessValidator);
  2838. const promise = puv.validate(() => 1, 'patch', 'model', {foo: 'bar'});
  2839. await expect(promise).to.be.rejectedWith(
  2840. 'An existing document of the model "model" already has ' +
  2841. 'the property "foo" with the value "bar" and should be unique.',
  2842. );
  2843. });
  2844. it('passes validation if the "countMethod" returns zero', async function () {
  2845. const dbs = new DatabaseSchema();
  2846. dbs.defineModel({
  2847. name: 'model',
  2848. properties: {
  2849. foo: {
  2850. type: DataType.ANY,
  2851. unique: PropertyUniqueness.SPARSE,
  2852. },
  2853. },
  2854. });
  2855. const puv = dbs.getService(PropertyUniquenessValidator);
  2856. await puv.validate(() => 0, 'patch', 'model', {foo: 'bar'});
  2857. });
  2858. it('invokes the "countMethod" for given properties which should be unique', async function () {
  2859. const dbs = new DatabaseSchema();
  2860. dbs.defineModel({
  2861. name: 'model',
  2862. properties: {
  2863. foo: {
  2864. type: DataType.ANY,
  2865. unique: PropertyUniqueness.SPARSE,
  2866. },
  2867. bar: {
  2868. type: DataType.ANY,
  2869. unique: false,
  2870. },
  2871. baz: {
  2872. type: DataType.ANY,
  2873. unique: PropertyUniqueness.SPARSE,
  2874. },
  2875. },
  2876. });
  2877. const puv = dbs.getService(PropertyUniquenessValidator);
  2878. let invoked = 0;
  2879. const modelData = {foo: 'val1', bar: 'val2'};
  2880. const countMethod = where => {
  2881. if (invoked === 0) expect(where).to.be.eql({foo: 'val1'});
  2882. invoked++;
  2883. return 0;
  2884. };
  2885. await puv.validate(countMethod, 'patch', 'model', modelData);
  2886. expect(invoked).to.be.eq(1);
  2887. });
  2888. it('skips uniqueness checking for non-provided fields', async function () {
  2889. const dbs = new DatabaseSchema();
  2890. dbs.defineModel({
  2891. name: 'model',
  2892. properties: {
  2893. foo: {
  2894. type: DataType.ANY,
  2895. unique: PropertyUniqueness.SPARSE,
  2896. },
  2897. },
  2898. });
  2899. const puv = dbs.getService(PropertyUniquenessValidator);
  2900. const promise1 = puv.validate(() => 1, 'patch', 'model', {
  2901. foo: 'bar',
  2902. });
  2903. const promise2 = puv.validate(() => 1, 'patch', 'model', {
  2904. baz: 'qux',
  2905. });
  2906. await expect(promise1).to.be.rejectedWith(
  2907. 'An existing document of the model "model" already has ' +
  2908. 'the property "foo" with the value "bar" and should be unique.',
  2909. );
  2910. await expect(promise2).not.to.be.rejected;
  2911. });
  2912. it('skips uniqueness checking for an undefined value', async function () {
  2913. const dbs = new DatabaseSchema();
  2914. dbs.defineModel({
  2915. name: 'model',
  2916. properties: {
  2917. foo: {
  2918. type: DataType.STRING,
  2919. unique: PropertyUniqueness.SPARSE,
  2920. },
  2921. bar: {
  2922. type: DataType.STRING,
  2923. unique: PropertyUniqueness.SPARSE,
  2924. },
  2925. },
  2926. });
  2927. const puv = dbs.getService(PropertyUniquenessValidator);
  2928. let invoked = 0;
  2929. const modelData = {foo: 'val1', bar: undefined};
  2930. const countMethod = where => {
  2931. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  2932. invoked++;
  2933. return 0;
  2934. };
  2935. await puv.validate(countMethod, 'patch', 'model', modelData);
  2936. expect(invoked).to.be.eql(1);
  2937. });
  2938. it('skips uniqueness checking for an empty string', async function () {
  2939. const dbs = new DatabaseSchema();
  2940. dbs.defineModel({
  2941. name: 'model',
  2942. properties: {
  2943. foo: {
  2944. type: DataType.STRING,
  2945. unique: PropertyUniqueness.SPARSE,
  2946. },
  2947. bar: {
  2948. type: DataType.STRING,
  2949. unique: PropertyUniqueness.SPARSE,
  2950. },
  2951. },
  2952. });
  2953. const puv = dbs.getService(PropertyUniquenessValidator);
  2954. let invoked = 0;
  2955. const modelData = {foo: 'val1', bar: ''};
  2956. const countMethod = where => {
  2957. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  2958. invoked++;
  2959. return 0;
  2960. };
  2961. await puv.validate(countMethod, 'patch', 'model', modelData);
  2962. expect(invoked).to.be.eql(1);
  2963. });
  2964. it('skips uniqueness checking for a zero number', async function () {
  2965. const dbs = new DatabaseSchema();
  2966. dbs.defineModel({
  2967. name: 'model',
  2968. properties: {
  2969. foo: {
  2970. type: DataType.NUMBER,
  2971. unique: PropertyUniqueness.SPARSE,
  2972. },
  2973. bar: {
  2974. type: DataType.NUMBER,
  2975. unique: PropertyUniqueness.SPARSE,
  2976. },
  2977. },
  2978. });
  2979. const puv = dbs.getService(PropertyUniquenessValidator);
  2980. let invoked = 0;
  2981. const modelData = {foo: 10, bar: 0};
  2982. const countMethod = where => {
  2983. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  2984. invoked++;
  2985. return 0;
  2986. };
  2987. await puv.validate(countMethod, 'patch', 'model', modelData);
  2988. expect(invoked).to.be.eql(1);
  2989. });
  2990. it('skips uniqueness checking for a false value', async function () {
  2991. const dbs = new DatabaseSchema();
  2992. dbs.defineModel({
  2993. name: 'model',
  2994. properties: {
  2995. foo: {
  2996. type: DataType.BOOLEAN,
  2997. unique: PropertyUniqueness.SPARSE,
  2998. },
  2999. bar: {
  3000. type: DataType.BOOLEAN,
  3001. unique: PropertyUniqueness.SPARSE,
  3002. },
  3003. },
  3004. });
  3005. const puv = dbs.getService(PropertyUniquenessValidator);
  3006. let invoked = 0;
  3007. const modelData = {foo: true, bar: false};
  3008. const countMethod = where => {
  3009. if (invoked === 0) expect(where).to.be.eql({foo: modelData.foo});
  3010. invoked++;
  3011. return 0;
  3012. };
  3013. await puv.validate(countMethod, 'patch', 'model', modelData);
  3014. expect(invoked).to.be.eql(1);
  3015. });
  3016. });
  3017. describe('patchById', function () {
  3018. it('throws an error if the "countMethod" returns a positive number', async function () {
  3019. const dbs = new DatabaseSchema();
  3020. dbs.defineModel({
  3021. name: 'model',
  3022. properties: {
  3023. foo: {
  3024. type: DataType.ANY,
  3025. unique: PropertyUniqueness.SPARSE,
  3026. },
  3027. },
  3028. });
  3029. const puv = dbs.getService(PropertyUniquenessValidator);
  3030. const promise = puv.validate(
  3031. () => 1,
  3032. 'patchById',
  3033. 'model',
  3034. {foo: 'bar'},
  3035. 1,
  3036. );
  3037. await expect(promise).to.be.rejectedWith(
  3038. 'An existing document of the model "model" already has ' +
  3039. 'the property "foo" with the value "bar" and should be unique.',
  3040. );
  3041. });
  3042. it('passes validation if the "countMethod" returns zero', async function () {
  3043. const dbs = new DatabaseSchema();
  3044. dbs.defineModel({
  3045. name: 'model',
  3046. properties: {
  3047. foo: {
  3048. type: DataType.ANY,
  3049. unique: PropertyUniqueness.SPARSE,
  3050. },
  3051. },
  3052. });
  3053. const puv = dbs.getService(PropertyUniquenessValidator);
  3054. await puv.validate(() => 0, 'patchById', 'model', {foo: 'bar'}, 1);
  3055. });
  3056. it('invokes the "countMethod" for given properties which should be unique', async function () {
  3057. const dbs = new DatabaseSchema();
  3058. dbs.defineModel({
  3059. name: 'model',
  3060. properties: {
  3061. foo: {
  3062. type: DataType.ANY,
  3063. unique: PropertyUniqueness.SPARSE,
  3064. },
  3065. bar: {
  3066. type: DataType.ANY,
  3067. unique: false,
  3068. },
  3069. baz: {
  3070. type: DataType.ANY,
  3071. unique: PropertyUniqueness.SPARSE,
  3072. },
  3073. },
  3074. });
  3075. const puv = dbs.getService(PropertyUniquenessValidator);
  3076. let invoked = 0;
  3077. const idValue = 1;
  3078. const modelData = {foo: 'val1', bar: 'val2'};
  3079. const countMethod = where => {
  3080. if (invoked === 0)
  3081. expect(where).to.be.eql({
  3082. [DEF_PK]: {neq: idValue},
  3083. foo: 'val1',
  3084. });
  3085. invoked++;
  3086. return 0;
  3087. };
  3088. await puv.validate(
  3089. countMethod,
  3090. 'patchById',
  3091. 'model',
  3092. modelData,
  3093. idValue,
  3094. );
  3095. expect(invoked).to.be.eq(1);
  3096. });
  3097. it('skips uniqueness checking for non-provided fields', async function () {
  3098. const dbs = new DatabaseSchema();
  3099. dbs.defineModel({
  3100. name: 'model',
  3101. properties: {
  3102. foo: {
  3103. type: DataType.ANY,
  3104. unique: PropertyUniqueness.SPARSE,
  3105. },
  3106. },
  3107. });
  3108. const puv = dbs.getService(PropertyUniquenessValidator);
  3109. const promise1 = puv.validate(() => 1, 'patchById', 'model', {
  3110. foo: 'bar',
  3111. });
  3112. const promise2 = puv.validate(() => 1, 'patchById', 'model', {
  3113. baz: 'qux',
  3114. });
  3115. await expect(promise1).to.be.rejectedWith(
  3116. 'An existing document of the model "model" already has ' +
  3117. 'the property "foo" with the value "bar" and should be unique.',
  3118. );
  3119. await expect(promise2).not.to.be.rejected;
  3120. });
  3121. it('skips uniqueness checking for an undefined value', async function () {
  3122. const dbs = new DatabaseSchema();
  3123. dbs.defineModel({
  3124. name: 'model',
  3125. properties: {
  3126. foo: {
  3127. type: DataType.STRING,
  3128. unique: PropertyUniqueness.SPARSE,
  3129. },
  3130. bar: {
  3131. type: DataType.STRING,
  3132. unique: PropertyUniqueness.SPARSE,
  3133. },
  3134. },
  3135. });
  3136. const puv = dbs.getService(PropertyUniquenessValidator);
  3137. let invoked = 0;
  3138. const modelData = {foo: 'val1', bar: undefined};
  3139. const countMethod = where => {
  3140. if (invoked === 0)
  3141. expect(where).to.be.eql({
  3142. [DEF_PK]: {neq: 1},
  3143. foo: modelData.foo,
  3144. });
  3145. invoked++;
  3146. return 0;
  3147. };
  3148. await puv.validate(countMethod, 'patchById', 'model', modelData, 1);
  3149. expect(invoked).to.be.eql(1);
  3150. });
  3151. it('skips uniqueness checking for an empty string', async function () {
  3152. const dbs = new DatabaseSchema();
  3153. dbs.defineModel({
  3154. name: 'model',
  3155. properties: {
  3156. foo: {
  3157. type: DataType.STRING,
  3158. unique: PropertyUniqueness.SPARSE,
  3159. },
  3160. bar: {
  3161. type: DataType.STRING,
  3162. unique: PropertyUniqueness.SPARSE,
  3163. },
  3164. },
  3165. });
  3166. const puv = dbs.getService(PropertyUniquenessValidator);
  3167. let invoked = 0;
  3168. const modelData = {foo: 'val1', bar: ''};
  3169. const countMethod = where => {
  3170. if (invoked === 0)
  3171. expect(where).to.be.eql({
  3172. [DEF_PK]: {neq: 1},
  3173. foo: modelData.foo,
  3174. });
  3175. invoked++;
  3176. return 0;
  3177. };
  3178. await puv.validate(countMethod, 'patchById', 'model', modelData, 1);
  3179. expect(invoked).to.be.eql(1);
  3180. });
  3181. it('skips uniqueness checking for a zero number', async function () {
  3182. const dbs = new DatabaseSchema();
  3183. dbs.defineModel({
  3184. name: 'model',
  3185. properties: {
  3186. foo: {
  3187. type: DataType.NUMBER,
  3188. unique: PropertyUniqueness.SPARSE,
  3189. },
  3190. bar: {
  3191. type: DataType.NUMBER,
  3192. unique: PropertyUniqueness.SPARSE,
  3193. },
  3194. },
  3195. });
  3196. const puv = dbs.getService(PropertyUniquenessValidator);
  3197. let invoked = 0;
  3198. const modelData = {foo: 10, bar: 0};
  3199. const countMethod = where => {
  3200. if (invoked === 0)
  3201. expect(where).to.be.eql({
  3202. [DEF_PK]: {neq: 1},
  3203. foo: modelData.foo,
  3204. });
  3205. invoked++;
  3206. return 0;
  3207. };
  3208. await puv.validate(countMethod, 'patchById', 'model', modelData, 1);
  3209. expect(invoked).to.be.eql(1);
  3210. });
  3211. it('skips uniqueness checking for a false value', async function () {
  3212. const dbs = new DatabaseSchema();
  3213. dbs.defineModel({
  3214. name: 'model',
  3215. properties: {
  3216. foo: {
  3217. type: DataType.BOOLEAN,
  3218. unique: PropertyUniqueness.SPARSE,
  3219. },
  3220. bar: {
  3221. type: DataType.BOOLEAN,
  3222. unique: PropertyUniqueness.SPARSE,
  3223. },
  3224. },
  3225. });
  3226. const puv = dbs.getService(PropertyUniquenessValidator);
  3227. let invoked = 0;
  3228. const modelData = {foo: true, bar: false};
  3229. const countMethod = where => {
  3230. if (invoked === 0)
  3231. expect(where).to.be.eql({
  3232. [DEF_PK]: {neq: 1},
  3233. foo: modelData.foo,
  3234. });
  3235. invoked++;
  3236. return 0;
  3237. };
  3238. await puv.validate(countMethod, 'patchById', 'model', modelData, 1);
  3239. expect(invoked).to.be.eql(1);
  3240. });
  3241. it('uses a custom primary key to check existence of the given identifier', async function () {
  3242. const dbs = new DatabaseSchema();
  3243. dbs.defineModel({
  3244. name: 'model',
  3245. properties: {
  3246. myId: {
  3247. type: DataType.NUMBER,
  3248. primaryKey: true,
  3249. },
  3250. foo: {
  3251. type: DataType.ANY,
  3252. unique: PropertyUniqueness.SPARSE,
  3253. },
  3254. },
  3255. });
  3256. const puv = dbs.getService(PropertyUniquenessValidator);
  3257. let invoked = 0;
  3258. const idValue = 1;
  3259. const modelData = {foo: 'bar'};
  3260. const countMethod = where => {
  3261. if (invoked === 0)
  3262. expect(where).to.be.eql({
  3263. myId: {neq: idValue},
  3264. foo: 'bar',
  3265. });
  3266. invoked++;
  3267. return 0;
  3268. };
  3269. await puv.validate(
  3270. countMethod,
  3271. 'patchById',
  3272. 'model',
  3273. modelData,
  3274. idValue,
  3275. );
  3276. expect(invoked).to.be.eq(1);
  3277. });
  3278. });
  3279. });
  3280. });
  3281. });