repository.spec.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {expect} from 'chai';
  2. import {Schema} from '../schema.js';
  3. import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '../definition/index.js';
  4. describe('Repository', function () {
  5. describe('create', function () {
  6. it('creates a new item from the given data', async function () {
  7. const schema = new Schema();
  8. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  9. schema.defineModel({name: 'model', datasource: 'datasource'});
  10. const data = {foo: 'bar'};
  11. const rep = schema.getRepository('model');
  12. const result = await rep.create(data);
  13. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], ...data});
  14. });
  15. });
  16. describe('replaceById', function () {
  17. it('replaces an item by the given id', async function () {
  18. const schema = new Schema();
  19. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  20. schema.defineModel({name: 'model', datasource: 'datasource'});
  21. const rep = schema.getRepository('model');
  22. const created = await rep.create({foo: 'bar'});
  23. const result = await rep.replaceById(created[DEF_PK], {baz: 'qux'});
  24. expect(result).to.be.eql({[DEF_PK]: created[DEF_PK], baz: 'qux'});
  25. });
  26. });
  27. describe('replaceOrCreate', function () {
  28. it('creates a new item from the given data', async function () {
  29. const schema = new Schema();
  30. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  31. schema.defineModel({name: 'model', datasource: 'datasource'});
  32. const data = {foo: 'bar'};
  33. const rep = schema.getRepository('model');
  34. const result = await rep.replaceOrCreate(data);
  35. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], ...data});
  36. });
  37. it('replaces an existing item', async function () {
  38. const schema = new Schema();
  39. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  40. schema.defineModel({name: 'model', datasource: 'datasource'});
  41. const rep = schema.getRepository('model');
  42. const created = await rep.create({foo: 'bar'});
  43. const replacer = {[DEF_PK]: created[DEF_PK], bar: 'qux'};
  44. const result = await rep.replaceOrCreate(replacer);
  45. expect(result).to.be.eql(replacer);
  46. });
  47. });
  48. describe('patch', function () {
  49. it('patches all items', async function () {
  50. const schema = new Schema();
  51. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  52. schema.defineModel({name: 'model', datasource: 'datasource'});
  53. const rep = schema.getRepository('model');
  54. await rep.create({foo: 'a1', bar: 'b1'});
  55. await rep.create({foo: 'a2', bar: 'b2'});
  56. await rep.create({foo: 'a3', bar: 'b3'});
  57. const result = await rep.patch({foo: 'test'});
  58. expect(result).to.be.eq(3);
  59. });
  60. it('patches found items by the "where" clause', async function () {
  61. const schema = new Schema();
  62. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  63. schema.defineModel({name: 'model', datasource: 'datasource'});
  64. const rep = schema.getRepository('model');
  65. await rep.create({foo: 'a', bar: '1'});
  66. await rep.create({foo: 'b', bar: '2'});
  67. await rep.create({foo: 'c', bar: '2'});
  68. const result = await rep.patch({foo: 'test'}, {bar: '2'});
  69. expect(result).to.be.eq(2);
  70. });
  71. });
  72. describe('patchById', function () {
  73. it('patches an item by the given id', async function () {
  74. const schema = new Schema();
  75. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  76. schema.defineModel({name: 'model', datasource: 'datasource'});
  77. const rep = schema.getRepository('model');
  78. const created = await rep.create({foo: 'bar'});
  79. const result = await rep.patchById(created[DEF_PK], {baz: 'qux'});
  80. expect(result).to.be.eql({
  81. [DEF_PK]: created[DEF_PK],
  82. foo: 'bar',
  83. baz: 'qux',
  84. });
  85. });
  86. });
  87. describe('find', function () {
  88. it('returns all items', async function () {
  89. const schema = new Schema();
  90. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  91. schema.defineModel({name: 'model', datasource: 'datasource'});
  92. const rep = schema.getRepository('model');
  93. const created1 = await rep.create({foo: 'bar'});
  94. const created2 = await rep.create({baz: 'qux'});
  95. const result = await rep.find();
  96. expect(result).to.be.eql([created1, created2]);
  97. });
  98. it('returns found items by the "where" clause', async function () {
  99. const schema = new Schema();
  100. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  101. schema.defineModel({name: 'model', datasource: 'datasource'});
  102. const rep = schema.getRepository('model');
  103. await rep.create({foo: 'bar'});
  104. const created = await rep.create({baz: 'qux'});
  105. const result = await rep.find({where: {baz: 'qux'}});
  106. expect(result).to.be.eql([created]);
  107. });
  108. });
  109. describe('findOne', function () {
  110. it('returns a first item', async function () {
  111. const schema = new Schema();
  112. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  113. schema.defineModel({name: 'model', datasource: 'datasource'});
  114. const rep = schema.getRepository('model');
  115. const created = await rep.create({foo: 'bar'});
  116. await rep.create({baz: 'qux'});
  117. const result = await rep.findOne();
  118. expect(result).to.be.eql(created);
  119. });
  120. it('returns a found item by the "where" clause', async function () {
  121. const schema = new Schema();
  122. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  123. schema.defineModel({name: 'model', datasource: 'datasource'});
  124. const rep = schema.getRepository('model');
  125. await rep.create({foo: 'bar'});
  126. const created = await rep.create({baz: 'qux'});
  127. const result = await rep.findOne({where: {baz: 'qux'}});
  128. expect(result).to.be.eql(created);
  129. });
  130. });
  131. describe('findById', function () {
  132. it('returns an item by the given id', async function () {
  133. const schema = new Schema();
  134. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  135. schema.defineModel({name: 'model', datasource: 'datasource'});
  136. const rep = schema.getRepository('model');
  137. const created = await rep.create({foo: 'bar'});
  138. const result = await rep.findById(created[DEF_PK]);
  139. expect(result).to.be.eql(created);
  140. });
  141. });
  142. describe('delete', function () {
  143. it('removes all items', async function () {
  144. const schema = new Schema();
  145. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  146. schema.defineModel({name: 'model', datasource: 'datasource'});
  147. const rep = schema.getRepository('model');
  148. await rep.create({foo: 'bar'});
  149. await rep.create({baz: 'qux'});
  150. const result = await rep.delete();
  151. expect(result).to.be.eq(2);
  152. });
  153. it('removes found items by the "where" clause', async function () {
  154. const schema = new Schema();
  155. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  156. schema.defineModel({name: 'model', datasource: 'datasource'});
  157. const rep = schema.getRepository('model');
  158. await rep.create({foo: 'bar'});
  159. await rep.create({foo: 'bar'});
  160. await rep.create({baz: 'qux'});
  161. const result = await rep.delete({foo: 'bar'});
  162. expect(result).to.be.eql(2);
  163. });
  164. });
  165. describe('deleteById', function () {
  166. it('removes an item by the given id', async function () {
  167. const schema = new Schema();
  168. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  169. schema.defineModel({name: 'model', datasource: 'datasource'});
  170. const rep = schema.getRepository('model');
  171. const created = await rep.create({foo: 'bar'});
  172. const result = await rep.deleteById(created[DEF_PK]);
  173. expect(result).to.be.true;
  174. });
  175. });
  176. describe('exists', function () {
  177. it('returns true if the given id exists', async function () {
  178. const schema = new Schema();
  179. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  180. schema.defineModel({name: 'model', datasource: 'datasource'});
  181. const rep = schema.getRepository('model');
  182. const created = await rep.create({foo: 'bar'});
  183. const result = await rep.exists(created[DEF_PK]);
  184. expect(result).to.be.true;
  185. });
  186. });
  187. describe('count', function () {
  188. it('counts all items', async function () {
  189. const schema = new Schema();
  190. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  191. schema.defineModel({name: 'model', datasource: 'datasource'});
  192. const rep = schema.getRepository('model');
  193. await rep.create({foo: 'bar'});
  194. await rep.create({baz: 'qux'});
  195. const result = await rep.count();
  196. expect(result).to.be.eq(2);
  197. });
  198. it('counts found items by the "where" clause', async function () {
  199. const schema = new Schema();
  200. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  201. schema.defineModel({name: 'model', datasource: 'datasource'});
  202. const rep = schema.getRepository('model');
  203. await rep.create({foo: 'bar'});
  204. await rep.create({foo: 'bar'});
  205. await rep.create({baz: 'qux'});
  206. const result = await rep.count({foo: 'bar'});
  207. expect(result).to.be.eq(2);
  208. });
  209. });
  210. });