'use strict';

const
  assert = require('chai').assert,
  utils = require('./_utils');

describe('MongoTenant', function() {
  describe('#Integration', function() {
    it('should inject default accessor method.', function() {
      let Model = utils.createTestModel({});

      assert(typeof Model.byTenant === 'function', 'Expected default accessor method to be statically available.');
    });

    it('should inject custom accessor method.', function() {
      let Model = utils.createTestModel({}, {
        mongoTenant: {
          accessorMethod: 'by_tenant'
        }
      });

      assert(typeof Model.by_tenant === 'function', 'Expected custom accessor method `by_tenant` to be statically available.');
    });

    it('should expose its tenant binding.', function() {
      let
        Model = utils.createTestModel({}),
        ModelBoundToTenant = Model.byTenant(1);

      assert(ModelBoundToTenant.hasTenantContext === true, 'Expected static flag `hasTenantContext` on models bound to a certain tenant.');
      assert((new ModelBoundToTenant()).hasTenantContext === true, 'Expected property `hasTenantContext` on model instances bound to a certain tenant.');
      assert(typeof Model.hasTenantContext === 'undefined', 'Expected the mongoose model to be untouched.');
      assert(typeof (new Model()).hasTenantContext === 'undefined', 'Expected the mongoose model to be untouched.');
    });

    it('should bind the model to the proper tenant.', function() {
      let
        Model = utils.createTestModel({}),
        modelA, modelB;

      modelA = Model.byTenant(1);
      modelB = Model.byTenant(2);

      assert.equal(modelA.getTenantId(), 1, 'Expected the tenantId of tenant specific model to be `1`.');
      assert.equal((new modelA()).getTenantId(), 1, 'Expected the tenantId of tenant specific model to be `1`.');
      assert.equal(modelB.getTenantId(), 2, 'Expected the tenantId of tenant specific model to be `2`.');
      assert.equal((new modelB()).getTenantId(), 2, 'Expected the tenantId of tenant specific model to be `2`.');
    });

    it('should create tenant specific models only once and cache previous compilations.', function() {
      let
        Model = utils.createTestModel({}),
        modelA, modelB;

      assert.equal(
        Model.byTenant(1), Model.byTenant(1),
        'Expected multiple calls to tenant specific model accessor to deliver same model compilation.'
      );
    });
  });
});
@@ -154,24 +154,52 @@
 unction() %7B%0A
+    utils.clearDatabase();%0A%0A
     it('shou
@@ -2462,16 +2462,770 @@
 %0A    %7D);
+%0A%0A    it('should bind Model.remove() to correct tenant context.', function(done) %7B%0A      let TestModel = utils.createTestModel(%7B%7D);%0A%0A      TestModel.create(%7BtenantId: 'tenant1'%7D, %7BtenantId: 'tenant2'%7D, (err) =%3E %7B%0A        assert(!err, 'Expected creation of 2 test entities to work.');%0A%0A        TestModel.byTenant('tenant1').remove((err) =%3E %7B%0A          assert(!err, 'Expected Model.remove() to work.');%0A%0A          TestModel.find(%7B%7D, (err, entities) =%3E %7B%0A            assert(!err, 'Expected Model.find() to work.');%0A            assert.equal(entities.length, 1, 'Expected to find only one entity.');%0A            assert.equal(entities%5B0%5D.tenantId, 'tenant2', 'Expected tenant2 scope on entity.');%0A%0A            done();%0A          %7D)%0A        %7D);%0A      %7D);%0A    %7D);
 %0A  %7D);%0A%7D

add test for static Model.remove() method.