Exemplo n.º 1
0
 def test_cleared_context(self):
     changes = dict(id="unique id", args=["some", 1], wibble="wobble")
     ctx = Context()
     ctx.update(changes)
     ctx.clear()
     self.assertDictEqual(get_context_as_dict(ctx), default_context)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 2
0
 def test_updated_context(self):
     expected = dict(default_context)
     changes = dict(id="unique id", args=["some", 1], wibble="wobble")
     ctx = Context()
     expected.update(changes)
     ctx.update(changes)
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 3
0
 def test_updated_context(self):
     expected = dict(default_context)
     changes = dict(id='unique id', args=['some', 1], wibble='wobble')
     ctx = Context()
     expected.update(changes)
     ctx.update(changes)
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 4
0
 def test_cleared_context(self):
     changes = dict(id='unique id', args=['some', 1], wibble='wobble')
     ctx = Context()
     ctx.update(changes)
     ctx.clear()
     defaults = dict(default_context, children=[])
     self.assertDictEqual(get_context_as_dict(ctx), defaults)
     self.assertDictEqual(get_context_as_dict(Context()), defaults)
Exemplo n.º 5
0
 def test_modified_context(self):
     expected = dict(default_context)
     ctx = Context()
     expected["id"] = "unique id"
     expected["args"] = ["some", 1]
     ctx.id = "unique id"
     ctx.args = ["some", 1]
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 6
0
 def test_context_get(self):
     expected = dict(default_context)
     changes = dict(id="unique id", args=["some", 1], wibble="wobble")
     ctx = Context()
     expected.update(changes)
     ctx.update(changes)
     ctx_dict = get_context_as_dict(ctx, getter=Context.get)
     self.assertDictEqual(ctx_dict, expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 7
0
 def test_modified_context(self):
     expected = dict(default_context)
     ctx = Context()
     expected['id'] = 'unique id'
     expected['args'] = ['some', 1]
     ctx.id = 'unique id'
     ctx.args = ['some', 1]
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 8
0
 def test_context_get(self):
     expected = dict(default_context)
     changes = dict(id='unique id', args=['some', 1], wibble='wobble')
     ctx = Context()
     expected.update(changes)
     ctx.update(changes)
     ctx_dict = get_context_as_dict(ctx, getter=Context.get)
     self.assertDictEqual(ctx_dict, expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 9
0
 def test_modified_context(self):
     expected = dict(default_context)
     ctx = Context()
     expected["id"] = "unique id"
     expected["args"] = ["some", 1]
     ctx.id = "unique id"
     ctx.args = ["some", 1]
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 10
0
 def test_modified_context(self):
     expected = dict(default_context)
     ctx = Context()
     expected['id'] = 'unique id'
     expected['args'] = ['some', 1]
     ctx.id = 'unique id'
     ctx.args = ['some', 1]
     self.assertDictEqual(get_context_as_dict(ctx), expected)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 11
0
 def test_default_context_threaded(self):
     ctx = Context()
     worker = ContextManipulator(ctx)
     worker.start()
     worker.join()
     self.assertDictEqual(worker.result, default_context)
     self.assertDictEqual(get_context_as_dict(ctx), default_context)
Exemplo n.º 12
0
    def test_modified_contex_threadedt(self):
        expected_a = dict(default_context)
        expected_a["id"] = "a"
        expected_a["args"] = ["some", 1]
        expected_a["wibble"] = "wobble"
        expected_b = dict(default_context)
        expected_b["id"] = "b"
        expected_b["args"] = ["other", 2]
        expected_b["weasel"] = "woozle"
        ctx = Context()

        worker_a = ContextManipulator(ctx,
                                      (setattr, ["id", "a"]),
                                      (setattr, ["args", ["some", 1]]),
                                      (setattr, ["wibble", "wobble"]))
        worker_b = ContextManipulator(ctx,
                                      (setattr, ["id", "b"]),
                                      (setattr, ["args", ["other", 2]]),
                                      (setattr, ["weasel", "woozle"]))
        worker_a.start()
        worker_b.start()
        worker_a.join()
        worker_b.join()

        self.assertDictEqual(worker_a.result, expected_a)
        self.assertDictEqual(worker_b.result, expected_b)
        self.assertDictEqual(get_context_as_dict(ctx), default_context)
Exemplo n.º 13
0
 def test_cleared_context(self):
     changes = dict(id="unique id", args=["some", 1], wibble="wobble")
     ctx = Context()
     ctx.update(changes)
     ctx.clear()
     self.assertDictEqual(get_context_as_dict(ctx), default_context)
     self.assertDictEqual(get_context_as_dict(Context()), default_context)
Exemplo n.º 14
0
 def test_cleared_context(self):
     changes = dict(id='unique id', args=['some', 1], wibble='wobble')
     ctx = Context()
     ctx.update(changes)
     ctx.clear()
     defaults = dict(default_context, children=[])
     self.assertDictEqual(get_context_as_dict(ctx), defaults)
     self.assertDictEqual(get_context_as_dict(Context()), defaults)
Exemplo n.º 15
0
    def test_cleared_context_threaded(self):
        changes_a = dict(id="a", args=["some", 1], wibble="wobble")
        expected_b = dict(default_context)
        changes_b = dict(id="b", args=["other", 2], weasel="woozle")
        expected_b.update(changes_b)
        ctx = Context()

        worker_a = ContextManipulator(ctx, (Context.update, [changes_a]),
                                      (Context.clear, []))
        worker_b = ContextManipulator(ctx, (Context.update, [changes_b]))
        worker_a.start()
        worker_b.start()
        worker_a.join()
        worker_b.join()

        self.assertDictEqual(worker_a.result, default_context)
        self.assertDictEqual(worker_b.result, expected_b)
        self.assertDictEqual(get_context_as_dict(ctx), default_context)
Exemplo n.º 16
0
 def test_default_context(self):
     # A bit of a tautological test, since it uses the same
     # initializer as the default_context constructor.
     defaults = dict(default_context, children=[])
     self.assertDictEqual(get_context_as_dict(Context()), defaults)
Exemplo n.º 17
0
# Retreive the values of all context attributes as a
# dictionary in an implementation-agnostic manner.
def get_context_as_dict(ctx, getter=getattr):
    defaults = {}
    for attr_name in dir(ctx):
        if attr_name.startswith('_'):
            continue  # Ignore pseudo-private attributes
        attr = getter(ctx, attr_name)
        if callable(attr):
            continue  # Ignore methods and other non-trivial types
        defaults[attr_name] = attr
    return defaults


default_context = get_context_as_dict(Context())


class test_Context(Case):
    def test_default_context(self):
        # A bit of a tautological test, since it uses the same
        # initializer as the default_context constructor.
        defaults = dict(default_context, children=[])
        self.assertDictEqual(get_context_as_dict(Context()), defaults)

    def test_updated_context(self):
        expected = dict(default_context)
        changes = dict(id='unique id', args=['some', 1], wibble='wobble')
        ctx = Context()
        expected.update(changes)
        ctx.update(changes)