|
16 | 16 | var projectConfig = require('./');
|
17 | 17 | var enums = require('../../utils/enums');
|
18 | 18 | var testDatafile = require('../../tests/test_data');
|
| 19 | +var configValidator = require('../../utils/config_validator'); |
19 | 20 |
|
20 | 21 | var _ = require('lodash/core');
|
21 | 22 | var fns = require('../../utils/fns');
|
@@ -727,4 +728,120 @@ describe('lib/core/project_config', function() {
|
727 | 728 | assert.strictEqual(didSetVariation, false);
|
728 | 729 | });
|
729 | 730 | });
|
| 731 | + |
| 732 | + describe('#tryCreatingProjectConfig', function() { |
| 733 | + var stubErrorHandler; |
| 734 | + var stubJsonSchemaValidator; |
| 735 | + var stubLogger; |
| 736 | + beforeEach(function() { |
| 737 | + stubErrorHandler = { |
| 738 | + handleError: sinon.stub(), |
| 739 | + }; |
| 740 | + stubJsonSchemaValidator = { |
| 741 | + validate: sinon.stub().returns(true), |
| 742 | + }; |
| 743 | + stubLogger = { |
| 744 | + log: sinon.stub(), |
| 745 | + }; |
| 746 | + sinon.stub(projectConfig, 'createProjectConfig').returns({}); |
| 747 | + sinon.stub(configValidator, 'validateDatafile').returns(true); |
| 748 | + }); |
| 749 | + |
| 750 | + afterEach(function() { |
| 751 | + projectConfig.createProjectConfig.restore(); |
| 752 | + configValidator.validateDatafile.restore(); |
| 753 | + }); |
| 754 | + |
| 755 | + it('returns a project config object created by createProjectConfig when all validation is applied and there are no errors', function() { |
| 756 | + configValidator.validateDatafile.returns(true); |
| 757 | + stubJsonSchemaValidator.validate.returns(true); |
| 758 | + var configObj = { |
| 759 | + foo: 'bar', |
| 760 | + experimentKeyMap: { |
| 761 | + a: { key: 'a' }, |
| 762 | + b: { key: 'b' }, |
| 763 | + } |
| 764 | + }; |
| 765 | + projectConfig.createProjectConfig.returns(configObj); |
| 766 | + var result = projectConfig.tryCreatingProjectConfig({ |
| 767 | + datafile: { foo: 'bar' }, |
| 768 | + errorHandler: stubErrorHandler, |
| 769 | + jsonSchemaValidator: stubJsonSchemaValidator, |
| 770 | + logger: stubLogger, |
| 771 | + skipJSONValidation: false, |
| 772 | + }); |
| 773 | + assert.deepEqual(result, configObj); |
| 774 | + }); |
| 775 | + |
| 776 | + it('returns null and calls handleError when validateDatafile throws', function() { |
| 777 | + configValidator.validateDatafile.throws(); |
| 778 | + stubJsonSchemaValidator.validate.returns(true); |
| 779 | + var result = projectConfig.tryCreatingProjectConfig({ |
| 780 | + datafile: { foo: 'bar' }, |
| 781 | + errorHandler: stubErrorHandler, |
| 782 | + jsonSchemaValidator: stubJsonSchemaValidator, |
| 783 | + logger: stubLogger, |
| 784 | + skipJSONValidation: false, |
| 785 | + }); |
| 786 | + assert.strictEqual(result, null); |
| 787 | + sinon.assert.calledOnce(stubErrorHandler.handleError); |
| 788 | + }); |
| 789 | + |
| 790 | + it('returns null and calls handleError when jsonSchemaValidator.validate throws', function() { |
| 791 | + configValidator.validateDatafile.returns(true); |
| 792 | + stubJsonSchemaValidator.validate.throws(); |
| 793 | + var result = projectConfig.tryCreatingProjectConfig({ |
| 794 | + datafile: { foo: 'bar' }, |
| 795 | + errorHandler: stubErrorHandler, |
| 796 | + jsonSchemaValidator: stubJsonSchemaValidator, |
| 797 | + logger: stubLogger, |
| 798 | + skipJSONValidation: false, |
| 799 | + }); |
| 800 | + assert.strictEqual(result, null); |
| 801 | + sinon.assert.calledOnce(stubErrorHandler.handleError); |
| 802 | + }); |
| 803 | + |
| 804 | + it('returns null when jsonSchemaValidator.validate returns false', function() { |
| 805 | + configValidator.validateDatafile.returns(true); |
| 806 | + stubJsonSchemaValidator.validate.returns(false); |
| 807 | + var result = projectConfig.tryCreatingProjectConfig({ |
| 808 | + datafile: { foo: 'bar' }, |
| 809 | + errorHandler: stubErrorHandler, |
| 810 | + jsonSchemaValidator: stubJsonSchemaValidator, |
| 811 | + logger: stubLogger, |
| 812 | + skipJSONValidation: false, |
| 813 | + }); |
| 814 | + assert.strictEqual(result, null); |
| 815 | + }); |
| 816 | + |
| 817 | + it('does not call jsonSchemaValidator.validate when skipJSONValidation is true', function() { |
| 818 | + projectConfig.tryCreatingProjectConfig({ |
| 819 | + datafile: { foo: 'bar' }, |
| 820 | + errorHandler: stubErrorHandler, |
| 821 | + jsonSchemaValidator: stubJsonSchemaValidator, |
| 822 | + logger: stubLogger, |
| 823 | + skipJSONValidation: true, |
| 824 | + }); |
| 825 | + sinon.assert.notCalled(stubJsonSchemaValidator.validate); |
| 826 | + }); |
| 827 | + |
| 828 | + it('skips json validation when jsonSchemaValidator is not provided', function() { |
| 829 | + configValidator.validateDatafile.returns(true); |
| 830 | + var configObj = { |
| 831 | + foo: 'bar', |
| 832 | + experimentKeyMap: { |
| 833 | + a: { key: 'a' }, |
| 834 | + b: { key: 'b' }, |
| 835 | + } |
| 836 | + }; |
| 837 | + projectConfig.createProjectConfig.returns(configObj); |
| 838 | + var result = projectConfig.tryCreatingProjectConfig({ |
| 839 | + datafile: { foo: 'bar' }, |
| 840 | + errorHandler: stubErrorHandler, |
| 841 | + logger: stubLogger, |
| 842 | + }); |
| 843 | + assert.deepEqual(result, configObj); |
| 844 | + sinon.assert.notCalled(stubErrorHandler.handleError); |
| 845 | + }); |
| 846 | + }); |
730 | 847 | });
|
0 commit comments