mirror of
https://github.com/ansible/awx.git
synced 2026-03-28 22:35:08 -02:30
Add tests for fact template rendering
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import compareNestedFacts from './compare-facts/nested';
|
import compareNestedFacts from './compare-facts/nested';
|
||||||
import compareFlatFacts from './compare-facts/flat';
|
import compareFlatFacts from './compare-facts/flat';
|
||||||
|
import FactTemplate from './compare-facts/fact-template';
|
||||||
|
|
||||||
export function compareFacts(module, facts) {
|
export function compareFacts(module, facts) {
|
||||||
if (module.displayType === 'nested') {
|
if (module.displayType === 'nested') {
|
||||||
@@ -15,7 +16,11 @@ export function compareFacts(module, facts) {
|
|||||||
} else {
|
} else {
|
||||||
// For flat structures we compare left-to-right, then right-to-left to
|
// For flat structures we compare left-to-right, then right-to-left to
|
||||||
// make sure we get a good comparison between both hosts
|
// make sure we get a good comparison between both hosts
|
||||||
var compare = _.partialRight(compareFlatFacts, module.nameKey, module.compareKey, module.factTemplate);
|
var compare = _.partialRight(compareFlatFacts,
|
||||||
|
module.nameKey,
|
||||||
|
module.compareKey,
|
||||||
|
new FactTemplate(module.factTemplate));
|
||||||
|
|
||||||
var leftToRight = compare(facts[0], facts[1]);
|
var leftToRight = compare(facts[0], facts[1]);
|
||||||
var rightToLeft = compare(facts[1], facts[0]);
|
var rightToLeft = compare(facts[1], facts[0]);
|
||||||
|
|
||||||
|
|||||||
@@ -4,23 +4,6 @@
|
|||||||
* All Rights Reserved
|
* All Rights Reserved
|
||||||
*************************************************/
|
*************************************************/
|
||||||
|
|
||||||
import stringFilters from 'tower/shared/string-filters/main';
|
|
||||||
|
|
||||||
var $injector = angular.injector(['ng', stringFilters.name]);
|
|
||||||
var $interpolate = $injector.get('$interpolate');
|
|
||||||
|
|
||||||
function getFactTemplate(factTemplate, fact) {
|
|
||||||
if (_.isFunction(factTemplate)) {
|
|
||||||
return factTemplate(fact);
|
|
||||||
} else {
|
|
||||||
return factTemplate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderFactTemplate(template, fact) {
|
|
||||||
return $interpolate(template)(fact);
|
|
||||||
}
|
|
||||||
|
|
||||||
function slotFactValues(basisPosition, basisValue, comparatorValue) {
|
function slotFactValues(basisPosition, basisValue, comparatorValue) {
|
||||||
var leftValue, rightValue;
|
var leftValue, rightValue;
|
||||||
|
|
||||||
@@ -45,7 +28,7 @@ export default
|
|||||||
var searcher = {};
|
var searcher = {};
|
||||||
searcher[nameKey] = basisFact[nameKey];
|
searcher[nameKey] = basisFact[nameKey];
|
||||||
|
|
||||||
var basisTemplate, comparatorTemplate, slottedValues, basisValue, comparatorValue;
|
var slottedValues, basisValue, comparatorValue;
|
||||||
|
|
||||||
var matchingFact = _.where(comparatorFacts.facts, searcher);
|
var matchingFact = _.where(comparatorFacts.facts, searcher);
|
||||||
var diffs;
|
var diffs;
|
||||||
@@ -54,9 +37,7 @@ export default
|
|||||||
|
|
||||||
if (!_.isUndefined(factTemplate)) {
|
if (!_.isUndefined(factTemplate)) {
|
||||||
|
|
||||||
basisTemplate = getFactTemplate(factTemplate, basisFact);
|
basisValue = factTemplate.render(basisFact);
|
||||||
|
|
||||||
basisValue = renderFactTemplate(basisTemplate, basisFact);
|
|
||||||
slottedValues = slotFactValues(basisFacts.position, basisValue, 'absent');
|
slottedValues = slotFactValues(basisFacts.position, basisValue, 'absent');
|
||||||
|
|
||||||
diffs =
|
diffs =
|
||||||
@@ -76,7 +57,7 @@ export default
|
|||||||
value1IsAbsent: slottedValues.left === 'absent',
|
value1IsAbsent: slottedValues.left === 'absent',
|
||||||
value2: slottedValues.right,
|
value2: slottedValues.right,
|
||||||
value2IsAbsent: slottedValues.right === 'absent'
|
value2IsAbsent: slottedValues.right === 'absent'
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -85,11 +66,8 @@ export default
|
|||||||
|
|
||||||
if (!_.isUndefined(factTemplate)) {
|
if (!_.isUndefined(factTemplate)) {
|
||||||
|
|
||||||
basisTemplate = getFactTemplate(factTemplate, basisFact);
|
basisValue = factTemplate.render(basisFact);
|
||||||
comparatorTemplate = getFactTemplate(factTemplate, matchingFact);
|
comparatorValue = factTemplate.render(matchingFact);
|
||||||
|
|
||||||
basisValue = renderFactTemplate(basisTemplate, basisFact);
|
|
||||||
comparatorValue = renderFactTemplate(comparatorTemplate, matchingFact);
|
|
||||||
|
|
||||||
slottedValues = slotFactValues(basisFacts.position, basisValue, comparatorValue);
|
slottedValues = slotFactValues(basisFacts.position, basisValue, comparatorValue);
|
||||||
|
|
||||||
|
|||||||
@@ -73,10 +73,6 @@ describe('CompareFacts.Flat', function() {
|
|||||||
return facts;
|
return facts;
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(function () {
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
it('uses "absent" for the missing value', function() {
|
it('uses "absent" for the missing value', function() {
|
||||||
|
|
||||||
var facts = factData([{ 'name': 'foo'
|
var facts = factData([{ 'name': 'foo'
|
||||||
@@ -123,5 +119,62 @@ describe('CompareFacts.Flat', function() {
|
|||||||
}]);
|
}]);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('with factTemplate', function() {
|
||||||
|
it('does not attempt to render the absent fact', function() {
|
||||||
|
var facts = factData([{ 'name': 'foo'
|
||||||
|
}]);
|
||||||
|
|
||||||
|
var renderCallCount = 0;
|
||||||
|
var factTemplate =
|
||||||
|
{ render: function() {
|
||||||
|
renderCallCount++;
|
||||||
|
},
|
||||||
|
template: ""
|
||||||
|
};
|
||||||
|
|
||||||
|
compareFacts(facts[0], facts[1], 'name', ['value'], factTemplate);
|
||||||
|
|
||||||
|
expect(renderCallCount).to.equal(1);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('with factTemplate', function() {
|
||||||
|
var factData;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
factData = [{ position: 'left',
|
||||||
|
facts:
|
||||||
|
[{ 'name': 'foo',
|
||||||
|
'value': 'bar'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{ position: 'right',
|
||||||
|
facts:
|
||||||
|
[{ 'name': 'foo',
|
||||||
|
'value': 'baz'
|
||||||
|
}]
|
||||||
|
}];
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders the template with each provided fact', function() {
|
||||||
|
|
||||||
|
var renderCalledWith = [];
|
||||||
|
var factTemplate =
|
||||||
|
{ render: function(fact) {
|
||||||
|
renderCalledWith.push(fact);
|
||||||
|
},
|
||||||
|
template: ""
|
||||||
|
};
|
||||||
|
|
||||||
|
compareFacts(factData[0], factData[1], 'name', ['value'], factTemplate);
|
||||||
|
|
||||||
|
expect(renderCalledWith).to.include(factData[0].facts[0]);
|
||||||
|
expect(renderCalledWith).to.include(factData[1].facts[0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user