class GivenAggregatorExtendedWithDecoratorGetKey(GivenEmptyAggregatorAsClassAttribute): aggregator = Aggregator() NUMBER_SEQUENCE_RESULT = Ens({'1': [1], '2': [2, 2]}) toStr = str @aggregator.getKey def getKey(self, obj): return self.toStr(obj)
class GivenAggregatorExtendedWithDecoratorAggregateFunction(GivenEmptyAggregatorAsClassAttribute): aggregator = Aggregator() NUMBER_SEQUENCE_RESULT = Ens({1: 1, 2: 4}) process = sum @aggregator.aggregateFunction def aggregateFunction(self, obj): return self.process(obj)
def setUp(self): if self.ATTRS is None: self.skipTest('Tests declared in abstract class') self.ens = Ens(**self.ATTRS)
class GivenEmptyAggregatorAsClassAttribute(TestAggregatorBase): aggregator = Aggregator() EMPTY_SEQUENCE_RESULT = Ens() NUMBER_SEQUENCE_RESULT = Ens({1: [1], 2: [2, 2]})
class GivenAggregateWithAggregateFunction(GivenAggregateWithNoArguments): AGGREGATOR_PARAMETERS = {'aggregateFunction': sum} NUMBER_SEQUENCE_RESULT = Ens({1: 1, 2: 4})
def testEqualEnsOfSameMembers(self): other = Ens(self.ATTRS) self.assertTrue(self.ens == other) self.assertFalse(self.ens != other)
def testMapOverMultipleEnsReturnsAggregatedEns(self): self.checkEnsEqual({'x': 4}, Ens.map(max, Ens(x=1), Ens(x=4)))
class GivenAggregateWithCallableGetKey(GivenAggregateWithNoArguments): AGGREGATOR_PARAMETERS = {'getKey': str} NUMBER_SEQUENCE_RESULT = Ens({'1': [1], '2': [2, 2]})
def testHasAttributeWhenInitializedWithEns(self): self.assertEqual(123, Ens(Ens(attr=123)).attr)
def testRaisesAmbiguousInitializationErrorWhenInitializedAmbiguously(self): with self.assertRaises(Ens.AmbiguousInitializationError): Ens({'attr': 123}, attr=42)
def testHasAttributeWhenInitializedWithDict(self): self.assertEqual(123, Ens({'attr': 123}).attr)
def setUp(self): self.ens = Ens(self.ATTRS)
def testNotEqualEnsOfDifferentMembers(self): other = Ens(self.ens, otherMember='a') self.assertTrue(self.ens != other) self.assertFalse(self.ens == other)
class GivenAggregatorWithRequiredKeysAsClassAttribute(TestAggregatorBase): aggregator = Aggregator(requiredKeys=['a', 'b']) EMPTY_SEQUENCE_RESULT = Ens(a=[], b=[]) NUMBER_SEQUENCE_RESULT = Ens({1: [1], 2: [2, 2], 'a': [], 'b': []})
def testMapOverMultipleEnsWithMissingValuesReturnsAggregatedEns(self): self.checkEnsEqual({'x': (None, 4)}, Ens.map(lambda a, b: (a, b), Ens(), Ens(x=4)))
class GivenAggregateWithNoArguments(TestGivenAggregator): AGGREGATOR_PARAMETERS = {} EMPTY_SEQUENCE_RESULT = Ens() NUMBER_SEQUENCE_RESULT = Ens({1: [1], 2: [2, 2]})
def testMapAcceptsDictsAsInput(self): self.checkEnsEqual({'x': '42'}, Ens.map(str, {'x': 42}))
class GivenAggregateWithStringGetKey(GivenAggregateWithNoArguments): AGGREGATOR_PARAMETERS = {'getKey': '__class__.__name__'} NUMBER_SEQUENCE_RESULT = Ens({'int': [2, 1, 2]})
def setUp(self): self.mapping = Ens.asMapping(Ens(self.ATTRS))
class GivenAggregateWithRequiredKeys(TestGivenAggregator): AGGREGATOR_PARAMETERS = {'requiredKeys': ['a', 'b']} EMPTY_SEQUENCE_RESULT = Ens(a=[], b=[]) NUMBER_SEQUENCE_RESULT = Ens({1: [1], 2: [2, 2], 'a': [], 'b': []})
def testMappedChangesAttributes(self): self.checkEnsEqual(dict((k, str(v)) for (k, v) in self.ATTRS.items()), Ens.map(str, self.ens))