Exemplo n.º 1
0
class ShearTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['a'] = 'Original Value'
        self.flock['b'] = {'i': 'Original Value', 'ii': 42}
        self.flock['c'] = [1, 2, 3]
        self.flock['d'] = set('ABC')

    def test_consistent_types(self):
        for collection in 'bcd':
            pre_shear_type = type(self.flock[collection])
            self.flock.shear()
            post_shear_type = type(self.flock[collection])
            assert pre_shear_type is post_shear_type

    def test_edit_post_shear(self):
        self.flock.shear()
        self.flock['b']['i'] = 'New Value'
        self.flock['c'].append(4)
        self.flock['d'].add('D')
        assert self.flock['c'] == [1, 2, 3, 4]
        assert self.flock['b']['i'] == 'New Value'
        assert 'D' in self.flock['d']
        self.flock['unreleated'] = 'Something'
        assert self.flock['b']['i'] == 'New Value'
        assert self.flock['c'] == [1, 2, 3, 4]
        assert 'D' in self.flock['d']
Exemplo n.º 2
0
class FlockAggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear_list(self):
        self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: sum(x))
        assert not self.flock.check()

        assert len(self.flock['sum']) == 9
        self.assertContentsEqual(self.flock['sum'].keys(), set(range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].items(), ((x,x * 3) for x in range(1, 10)))

        assert self.flock['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum']() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)}

        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_shear_func(self):
        self.flock['sum'] = FlockAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum)
        assert not self.flock.check()

        assert len(self.flock['sum']) == 9
        self.assertContentsEqual(self.flock['sum'].keys(), range(1, 10))
        self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].items(), ((x,x * 3) for x in range(1, 10)))

        assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)}

        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_check(self):
        self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: int(x))
        check = self.flock.check()
        assert check
        assert len(check['sum']) == 9
        for value in check['sum'].values():
            assert len(value) == 2

    def assertContentsEqual(self, param, param1, *args, **kwargs):
        return self.assertSetEqual(set(param), set(param1), *args, **kwargs)
Exemplo n.º 3
0
class FlockAggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear_list(self):
        self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: sum(x))
        assert not self.flock.check()

        assert len(self.flock['sum']) == 9
        self.assertContentsEqual(self.flock['sum'].keys(), set(range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].items(), ((x, x * 3) for x in range(1, 10)))

        assert self.flock['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum']() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)}

        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_shear_func(self):
        self.flock['sum'] = FlockAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum)
        assert not self.flock.check()

        assert len(self.flock['sum']) == 9
        self.assertContentsEqual(self.flock['sum'].keys(), range(1, 10))
        self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10)))
        self.assertContentsEqual(self.flock['sum'].items(), ((x, x * 3) for x in range(1, 10)))

        assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)}
        assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)}
        assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)}

        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_check(self):
        self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: int(x))
        check = self.flock.check()
        assert check
        assert len(check['sum']) == 9
        for value in check['sum'].values():
            assert len(value) == 2

    def assertContentsEqual(self, param, param1, *args, **kwargs):
        return self.assertSetEqual(set(param), set(param1), *args, **kwargs)
Exemplo n.º 4
0
class MetaAggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear(self):
        self.flock['sum'] = MetaAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum)
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared
Exemplo n.º 5
0
class MetaAggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear(self):
        self.flock['sum'] = MetaAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum)
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared
Exemplo n.º 6
0
class AggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear(self):
        self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: sum(x))
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_check(self):
        self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: int(x))
        check = self.flock.check()
        assert check
        assert len(check['sum']) == 9
        for value in check['sum'].values():
            assert len(value) == 2
Exemplo n.º 7
0
class AggregatorTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.flock = FlockDict()
        self.flock['x'] = {x: x for x in range(1, 10)}
        self.flock['y'] = {x: 2 * x for x in range(1, 10)}

    def test_shear(self):
        self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: sum(x))
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 3
        assert isinstance(sheared, dict)
        assert sheared['sum'] == {x: x * 3 for x in range(1, 10)}
        assert dict(self.flock()) == sheared

    def test_check(self):
        self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: int(x))
        check = self.flock.check()
        assert check
        assert len(check['sum']) == 9
        for value in check['sum'].values():
            assert len(value) == 2
Exemplo n.º 8
0
            HeroicSkill('Rapidity', HEROIC, 6, bonuses={'Initiative': -1}),
            HeroicSkill('Vicous Blow', HEROIC, 1, bonuses={'Damage': 1}),
            HeroicSkill('Stealth Strike', HEROIC, 3),
            HeroicSkill('Aura Extension', HEROIC, 1),
            HeroicSkill('3rd Tier', HEROIC, 5),
            HeroicSkill('Nimble', HEROIC, 4, bonuses={
                'Dodge': 1,
                'Parry': 1
            }),
            HeroicSkill('Flurry', HEROIC, 6),
            HeroicSkill('2nd Circle', HEROIC, 10),
            HeroicSkill('4th Tier', HEROIC, 10),
        ]
        char['Race'] = "Human"
        char['level'] = 8
    else:
        for key, value in yaml.load(opt.infile).items():
            char[key] = value

    apply_rules(char)
    char.check()
    # pprint(char.shear())
    sheared = char.shear()
    for attribute in chain([], sheared['base_stats'].keys()):
        print("%s: %s" % (attribute, sheared.pop(attribute)))
    pprint(sheared)
    if opt.outfile:
        # sheared['skills'] = []
        # for skill in sheared.skill:
        yaml.dump(sheared, opt.outfile, width=80)
Exemplo n.º 9
0
            HeroicSkill('Uncanny Strike', HEROIC, 1, bonuses={'Hit Bonus': 1}),
            HeroicSkill('Grevious Blow', HEROIC, 8, bonuses={'Base Damage': 1}),
            HeroicSkill('Rapidity', HEROIC, 6, bonuses={'Initiative': -1}),
            HeroicSkill('Vicous Blow', HEROIC, 1, bonuses={'Damage': 1}),
            HeroicSkill('Stealth Strike', HEROIC, 3),
            HeroicSkill('Aura Extension', HEROIC, 1),
            HeroicSkill('3rd Tier', HEROIC, 5),
            HeroicSkill('Nimble', HEROIC, 4, bonuses={'Dodge': 1, 'Parry': 1}),
            HeroicSkill('Flurry', HEROIC, 6),
            HeroicSkill('2nd Circle', HEROIC, 10),
            HeroicSkill('4th Tier', HEROIC, 10),

        ]
        char['Race'] = "Human"
        char['level'] = 8
    else:
        for key, value in yaml.load(opt.infile).items():
            char[key] = value

    apply_rules(char)
    char.check()
    # pprint(char.shear())
    sheared = char.shear()
    for attribute in chain([], sheared['base_stats'].keys()):
        print("%s: %s" % (attribute, sheared.pop(attribute)))
    pprint(sheared)
    if opt.outfile:
        # sheared['skills'] = []
        # for skill in sheared.skill:
        yaml.dump(sheared, opt.outfile, width=80)
Exemplo n.º 10
0
class BasicFlockTestCase(unittest.TestCase):
    """
    Tests of the basic operations of a flock
    """

    def setUp(self):
        super().setUp()
        self.flock = FlockDict()

    def test_simple_values(self):
        """
        Test that simple(non-dict, non-callable) values are stored and retrieved transparently.

        """
        assert len(self.flock) == 0
        self.flock[3] = 3
        assert self.flock[3] == 3
        assert len(self.flock) == 1
        self.flock["Shepherd"] = "Mary"
        assert len(self.flock) == 2
        self.assertEqual(self.flock["Shepherd"], "Mary")
        self.flock["Management"] = ["Mary", "Joshua", "Isaac"]
        assert len(self.flock) == 3
        self.assertEqual(self.flock["Management"], ["Mary", "Joshua", "Isaac"])
        self.flock["Shepherd"] = "John"
        self.assertEqual(self.flock["Shepherd"], "John")

    def test_simple_dict(self):
        """
        Test that nested dicts still look like dicts.

        """
        self.flock["Management"] = {"Mary": {"lambs": 1, "size": 'little'}}
        assert self.flock["Management"]["Mary"]['lambs'] == 1
        assert self.flock["Management"]["Mary"]['size'] == 'little'

    def test_simple_closure(self):
        """
        Test that simple closures are stored without mangling and evaluated on retrieval

        """
        self.flock["lamb"] = lambda: "little"
        self.assertEqual(self.flock["lamb"], "little")

    def test_shear(self):
        """
        Test trivial shear opperation
        """
        self.flock[3] = 15
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 1
        assert isinstance(sheared, dict)
        assert sheared[3] == 15

        self.flock['cat'] = lambda: 'Abbey'
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 2
        assert isinstance(sheared, dict)
        assert sheared['cat'] == 'Abbey'

    def test_consistent_shear(self):
        t = toggle()
        self.flock['toggle'] = t
        self.flock['toggle2'] = t
        self.flock.update({x: lambda: self.flock['toggle'] for x in range(5)})
        sheared = self.flock.shear()
        self.assertEqual(sheared['toggle'], not sheared['toggle2'])
        self.assertEqual([sheared['toggle']] * 5, [sheared[x] for x in range(5)])
Exemplo n.º 11
0
class BasicFlockTestCase(unittest.TestCase):
    """
    Tests of the basic operations of a flock
    """

    def setUp(self):
        super().setUp()
        self.flock = FlockDict()

    def test_simple_values(self):
        """
        Test that simple(non-dict, non-callable) values are stored and retrieved transparently.
        """
        assert len(self.flock) == 0
        self.flock[3] = 3
        assert self.flock[3] == 3
        assert len(self.flock) == 1
        self.flock["Shepherd"] = "Mary"
        assert len(self.flock) == 2
        self.assertEqual(self.flock["Shepherd"], "Mary")
        self.flock["Management"] = ["Mary", "Joshua", "Isaac"]
        assert len(self.flock) == 3
        self.assertEqual(self.flock["Management"], ["Mary", "Joshua", "Isaac"])
        self.flock["Shepherd"] = "John"
        self.assertEqual(self.flock["Shepherd"], "John")
        assert "Shepherd" in self.flock
        assert 'missing' not in self.flock
        del self.flock["Shepherd"]
        assert "Shepherd" not in self.flock

    def test_simple_dict(self):
        """
        Test that nested dicts still look like dicts.
        """
        self.flock["Management"] = {"Mary": {"lambs": 1, "size": 'little'}}
        assert self.flock["Management"]["Mary"]['lambs'] == 1
        assert self.flock["Management"]["Mary"]['size'] == 'little'

    def test_simple_closure(self):
        """
        Test that simple closures are stored without mangling and evaluated on retrieval
        """
        self.flock["lamb"] = lambda: "little"
        self.assertEqual(self.flock["lamb"], "little")

    def test_callable_with_arguments(self):
        """
        Test that callables with arguments are passed transparently and never called
        """
        self.flock["func"] = lambda x, y: x + y
        self.assertEqual(self.flock["func"](1, 2), 3)

        def test2(x, y):
            assert False

        self.flock["test2"] = test2
        assert test2 == self.flock["test2"]  # Testing that this is not called

    def test_error(self):
        self.flock["bad"] = lambda: 1 / 0
        assert 'bad' in self.flock
        with raises(FlockException) as exc_info:
            self.flock.pop('bad')
        assert isinstance(exc_info.value.__cause__, ZeroDivisionError)

        with raises(FlockException) as exc_info:
            assert self.flock['bad'] != (
            lambda: 1 / 0), "This should not be called at all as the exception should be raised"
        assert isinstance(exc_info.value.__cause__, ZeroDivisionError)

        with raises(FlockException) as exc_info:
            self.flock.shear()
        assert isinstance(exc_info.value.__cause__, ZeroDivisionError)

        error = self.flock.shear(record_errors=True)['bad']
        assert isinstance(error, FlockException)
        assert isinstance(error.__cause__, ZeroDivisionError)

    def test_shear(self):
        """
        Test trivial shear operation
        """
        self.flock[3] = 15
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 1
        assert isinstance(sheared, dict)
        assert sheared[3] == 15

        self.flock['cat'] = lambda: 'Abbey'
        assert not self.flock.check()
        sheared = self.flock.shear()
        assert len(sheared) == 2
        assert isinstance(sheared, dict)
        assert sheared['cat'] == 'Abbey'
        assert sheared[3] == 15

        assert self.flock.dataset() == {'cat':'Abbey',3:15}
        assert len(self.flock.ruleset()) == 0
        assert not self.flock.ruleset()

    def test_consistent_shear(self):
        t = toggle()
        self.flock['toggle'] = t
        self.flock['toggle2'] = t
        self.flock.update({x: lambda: self.flock['toggle'] for x in range(5)})
        sheared = self.flock.shear()
        self.assertEqual(sheared['toggle'], not sheared['toggle2'])
        self.assertEqual([sheared['toggle']] * 5, [sheared[x] for x in range(5)])

        assert len(self.flock.dataset()) == 0
        assert not self.flock.dataset()
        assert len(self.flock.ruleset()) == 7
        assert all(isinstance(x, FunctionType) for x in self.flock.ruleset().values())