Browse Source

chore: improve tests

e22m4u 2 years ago
parent
commit
d2caf33a99
2 changed files with 122 additions and 8 deletions
  1. 9 8
      src/mongodb-adapter.js
  2. 113 0
      src/mongodb-adapter.spec.js

+ 9 - 8
src/mongodb-adapter.js

@@ -322,24 +322,25 @@ export class MongodbAdapter extends Adapter {
    * @private
    */
   _buildSort(modelName, clause) {
-    if (!clause) return;
-    clause = Array.isArray(clause) ? clause : [clause];
+    if (clause == null) return;
+    if (Array.isArray(clause) === false) clause = [clause];
     if (!clause.length) return;
     const utils = this.getService(ModelDefinitionUtils);
     const idPropName = this._getIdPropName(modelName);
     return clause.reduce((acc, order) => {
       if (!order || typeof order !== 'string')
         throw new InvalidArgumentError(
-          'A field order must be a non-empty String, but %v given.',
+          'The provided option "order" should be a non-empty String ' +
+            'or an Array of non-empty String, but %v given.',
           order,
         );
       const direction = order.match(/\s+(A|DE)SC$/);
-      let key = order.replace(/\s+(A|DE)SC$/, '').trim();
-      if (key === idPropName) {
-        key = '_id';
+      let field = order.replace(/\s+(A|DE)SC$/, '').trim();
+      if (field === idPropName) {
+        field = '_id';
       } else {
         try {
-          key = utils.getColumnNameByPropertyName(modelName, key);
+          field = utils.getColumnNameByPropertyName(modelName, field);
         } catch (error) {
           if (
             !(error instanceof InvalidArgumentError) ||
@@ -349,7 +350,7 @@ export class MongodbAdapter extends Adapter {
           }
         }
       }
-      acc[key] = direction && direction[1] === 'DE' ? -1 : 1;
+      acc[field] = direction && direction[1] === 'DE' ? -1 : 1;
       return acc;
     }, {});
   }

+ 113 - 0
src/mongodb-adapter.spec.js

@@ -228,6 +228,119 @@ describe('MongodbAdapter', function () {
     });
   });
 
+  describe('_buildSort', function () {
+    describe('single field', function () {
+      it('returns undefined if the second argument is undefined', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const res = A._buildSort('model', undefined);
+        expect(res).to.be.undefined;
+      });
+
+      it('returns undefined if the second argument is null', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const res = A._buildSort('model', null);
+        expect(res).to.be.undefined;
+      });
+
+      it('requires the second argument to be a non-empty string', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const throwable = v => () => A._buildSort('model', v);
+        const error = v =>
+          format(
+            'The provided option "order" should be a non-empty String ' +
+              'or an Array of non-empty String, but %s given.',
+            v,
+          );
+        expect(throwable('')).to.throw(error('""'));
+        expect(throwable(10)).to.throw(error('10'));
+        expect(throwable(0)).to.throw(error('0'));
+        expect(throwable(true)).to.throw(error('true'));
+        expect(throwable(false)).to.throw(error('false'));
+        expect(throwable({})).to.throw(error('Object'));
+        expect(throwable('bar')()).to.be.eql({bar: 1});
+        expect(throwable(undefined)()).to.be.undefined;
+        expect(throwable(null)()).to.be.undefined;
+      });
+
+      it('recognizes direction by the given direction flag', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const res1 = A._buildSort('model', 'foo');
+        const res2 = A._buildSort('model', 'foo DESC');
+        const res3 = A._buildSort('model', 'foo ASC');
+        expect(res1).to.be.eql({foo: 1});
+        expect(res2).to.be.eql({foo: -1});
+        expect(res3).to.be.eql({foo: 1});
+      });
+    });
+
+    describe('multiple fields', function () {
+      it('returns undefined if the second argument is an empty array', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const res = A._buildSort('model', []);
+        expect(res).to.be.undefined;
+      });
+
+      it('requires the second argument to be an array of non-empty strings', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const throwable = v => () => A._buildSort('model', v);
+        const error = v =>
+          format(
+            'The provided option "order" should be a non-empty String ' +
+              'or an Array of non-empty String, but %s given.',
+            v,
+          );
+        expect(throwable([''])).to.throw(error('""'));
+        expect(throwable([10])).to.throw(error('10'));
+        expect(throwable([0])).to.throw(error('0'));
+        expect(throwable([true])).to.throw(error('true'));
+        expect(throwable([false])).to.throw(error('false'));
+        expect(throwable([{}])).to.throw(error('Object'));
+        expect(throwable([undefined])).to.throw(error('undefined'));
+        expect(throwable([null])).to.throw(error('null'));
+        expect(throwable([])()).to.be.undefined;
+        expect(throwable(['bar', 'baz'])()).to.be.eql({bar: 1, baz: 1});
+      });
+
+      it('recognizes direction by the given direction flag', async function () {
+        const schema = createSchema();
+        schema.defineModel({name: 'model', datasource: 'mongodb'});
+        const A = await schema
+          .getService(AdapterRegistry)
+          .getAdapter('mongodb');
+        const res1 = A._buildSort('model', ['foo', 'bar']);
+        const res2 = A._buildSort('model', ['foo DESC', 'bar ASC']);
+        const res3 = A._buildSort('model', ['foo ASC', 'bar DESC']);
+        expect(res1).to.be.eql({foo: 1, bar: 1});
+        expect(res2).to.be.eql({foo: -1, bar: 1});
+        expect(res3).to.be.eql({foo: 1, bar: -1});
+      });
+    });
+  });
+
   describe('create', function () {
     it('generates a new identifier when a value of a primary key is not provided', async function () {
       const schema = createSchema();