def testMappedAttributeKeyIsRemoved(self): """Tests that when a key attribute is mapped, that key is removed from the array: * * <things> * <option id="option1" value="foo"> * <option id="option2" value="bar"> * </things> * * The above should finally be mapped to an array that looks like this * (because "id" is the key attribute). * * array( * 'things' => array( * 'option1' => 'foo', * 'option2' => 'bar', * ) * ) """ node = PrototypedArrayNode('root'); node.setKeyAttribute('id', True); # each item under the root is an array, with one scalar item prototype = ArrayNode("", node); prototype.addChild(ScalarNode('foo')); node.setPrototype(prototype); children = []; children.append({'id': 'item_name', 'foo': 'bar'}); normalized = node.normalize(children); expected = dict(); expected['item_name'] = {'foo': 'bar'}; self.assertEqual(expected, normalized);
def testNormalizeThrowsExceptionWhenFalseIsNotAllowed(self): """@expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException """ try: node = ArrayNode('root'); node.normalize(False); self.fail() except Exception as e: self.assertTrue(isinstance(e, InvalidTypeException));
def testExceptionThrownOnUnrecognizedChild(self): """normalize() should protect against child values with no corresponding node """ node = ArrayNode('root'); try: node.normalize({'foo': 'bar'}); self.fail('An exception should have been raise for a bad child node'); except Exception as e: self.assertTrue(isinstance(e, InvalidConfigurationException)); self.assertEqual('Unrecognized options "foo" under "root"', e.getMessage());
def testGetDefaultValueReturnsDefaultValueForPrototypes(self): node = PrototypedArrayNode('root'); prototype = ArrayNode("", node); node.setPrototype(prototype); node.setDefaultValue(['test']); self.assertEqual({0: 'test'}, node.getDefaultValue());
def _getPrototypeNodeWithDefaultChildren(self): node = PrototypedArrayNode('root'); prototype = ArrayNode("", node); child = ScalarNode('foo'); child.setDefaultValue('bar'); prototype.addChild(child); prototype.setAddIfNotSet(True); node.setPrototype(prototype); return node;
def testRemappedKeysAreUnset(self): node = ArrayNode('root'); mappingsNode = PrototypedArrayNode('mappings'); node.addChild(mappingsNode); # each item under mappings is just a scalar prototype = ScalarNode("", mappingsNode); mappingsNode.setPrototype(prototype); remappings = []; remappings.append(['mapping', 'mappings']); node.setXmlRemappings(remappings); normalized = node.normalize({'mapping': ['foo', 'bar']}); self.assertEqual({'mappings': {0: 'foo', 1: 'bar'}}, normalized);
def testIgnoreExtraKeysNoException(self): """Tests that no exception is thrown for an unrecognized child if the: * ignoreExtraKeys option is set to True. * * Related to testExceptionThrownOnUnrecognizedChild """ node = ArrayNode('roo'); node.setIgnoreExtraKeys(True); node.normalize({'foo': 'bar'}); self.assertTrue(True, 'No exception was thrown when setIgnoreExtraKeys is True');
def testMappedAttributeKeyNotRemoved(self): """Tests the opposite of the testMappedAttributeKeyIsRemoved because * the removal can be toggled with an option. """ node = PrototypedArrayNode('root'); node.setKeyAttribute('id', False); # each item under the root is an array, with two scalar items prototype = ArrayNode("", node); prototype.addChild(ScalarNode('foo')); prototype.addChild(ScalarNode('id')); # the key attribute will remain node.setPrototype(prototype); children = list(); children.append({'id': 'item_name', 'foo': 'bar'}); normalized = node.normalize(children); expected = dict(); expected['item_name'] = {'id': 'item_name', 'foo': 'bar'}; self.assertEqual(expected, normalized);
def _createNode(self): if self._prototype is None: node = ArrayNode(self._name, self._parent); self._validateConcreteNode(node); node.setAddIfNotSet(self._addDefaults); for child in self._children.values(): child._parent = node; node.addChild(child.getNode()); else: node = PrototypedArrayNode(self._name, self._parent); self._validatePrototypeNode(node); if not self._key is None: node.setKeyAttribute(self._key, self._removeKeyItem); if self._atLeastOne: node.setMinNumberOfElements(1); if self._default: node.setDefaultValue(self._defaultValue); if not self._addDefaultChildren is False: node.setAddChildrenIfNoneSet(self._addDefaultChildren); if isinstance(self._prototype, type(self)): if self._prototype._prototype is None: self._prototype.addDefaultsIfNotSet(); self._prototype._parent = node; node.setPrototype(self._prototype.getNode()); node.setAllowNewKeys(self._allowNewKeys); node.addEquivalentValue(None, self._nullEquivalent); node.addEquivalentValue(True, self._trueEquivalent); node.addEquivalentValue(False, self._falseEquivalent); node.setPerformDeepMerging(self._performDeepMerging); node.setRequired(self._required); node.setIgnoreExtraKeys(self._ignoreExtraKeys); node.setNormalizeKeys(self._normalizeKeys); if not self._normalizationBuilder is None: node.setNormalizationClosures(self._normalizationBuilder.befores); node.setXmlRemappings(self._normalizationBuilder.remappings); if not self._mergeBuilder is None: node.setAllowOverwrite(self._mergeBuilder.allowOverwrite); node.setAllowFalse(self._mergeBuilder.allowFalse); if not self._validationBuilder is None: node.setFinalValidationClosures(self._validationBuilder.rules); return node;
def testGetDefaultValueReturnsAnEmptyArrayForPrototypes(self): node = PrototypedArrayNode('root'); prototype = ArrayNode("", node); node.setPrototype(prototype); self.assertFalse(node.getDefaultValue());
def test(denormalized, normalized): node = ArrayNode('foo'); self.assertEqual(normalized, node._preNormalize(denormalized));