def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    if six.PY2:
        raise SkipTest("Pickling MultiContext instances is broken on Python 2.")
    d1 = DataContext(name = 'test_context1',
                     subcontext = {'a':1, 'b':2})
    d2 = DataContext(name = 'test_context2',
                     subcontext = {'foo':100, 'bar':200, 'baz':300})
    m = MultiContext(d1, d2, name='test_mc')

    f = BytesIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m['a'] == m['a']
    assert new_m['b'] == m['b']
    assert new_m['foo'] == m['foo']
    assert new_m['bar'] == m['bar']

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    d1 = DataContext(name='test_context1', subcontext={'a': 1, 'b': 2})
    d2 = DataContext(
        name='test_context2', subcontext={'foo': 100,
                                          'bar': 200,
                                          'baz': 300})
    m = MultiContext(d1, d2, name='test_mc')

    f = StringIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m['a'] == m['a']
    assert new_m['b'] == m['b']
    assert new_m['foo'] == m['foo']
    assert new_m['bar'] == m['bar']

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    d1 = DataContext(name="test_context1", subcontext={"a": 1, "b": 2})
    d2 = DataContext(name="test_context2", subcontext={"foo": 100, "bar": 200, "baz": 300})
    m = MultiContext(d1, d2, name="test_mc")

    f = StringIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m["a"] == m["a"]
    assert new_m["b"] == m["b"]
    assert new_m["foo"] == m["foo"]
    assert new_m["bar"] == m["bar"]

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name='d1', subcontext={'a': 1, 'b': 2})
        d2 = DataContext(name='d2', subcontext={'a': 3, 'c': 4})
        m = MultiContext(d1, d2, name='m')

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ['a', 'b', 'c'])
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name='d1',subcontext={'a':1,'b':2})
        d2 = DataContext(name='d2',subcontext={'a':3,'c':4})
        m = MultiContext(d1, d2, name='m')

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ['a', 'b', 'c'])
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name="d1", subcontext={"a": 1, "b": 2})
        d2 = DataContext(name="d2", subcontext={"a": 3, "c": 4})
        m = MultiContext(d1, d2, name="m")

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ["a", "b", "c"])
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name='test_context1', subcontext={'a': 1, 'b': 2})
        d2 = DataContext(name='test_context2')

        m = MultiContext(* [d1, d2], **{'name': 'test_mc'})
        self.assertTrue(len(list(m.keys())) == 2)

        # Add another context
        d3 = DataContext(name='test_context3', subcontext={'c': 3, 'd': 4})
        m.subcontexts.append(d3)
        self.assertTrue(len(list(m.keys())) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {'cc': 5}
        self.assertTrue(len(list(m.keys())) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(list(m.keys())) == 3)
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name="test_context1", subcontext={"a": 1, "b": 2})
        d2 = DataContext(name="test_context2")

        m = MultiContext(*[d1, d2], **{"name": "test_mc"})
        self.assertTrue(len(m.keys()) == 2)

        # Add another context
        d3 = DataContext(name="test_context3", subcontext={"c": 3, "d": 4})
        m.subcontexts.append(d3)
        self.assertTrue(len(m.keys()) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {"cc": 5}
        self.assertTrue(len(m.keys()) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(m.keys()) == 3)
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name = 'test_context1',
                         subcontext = {'a':1, 'b':2})
        d2 = DataContext(name = 'test_context2')

        m = MultiContext(*[d1,d2], **{'name': 'test_mc'})
        self.assertTrue(len(list(m.keys())) == 2)

        # Add another context
        d3 = DataContext(name = 'test_context3',
                         subcontext = {'c':3, 'd':4})
        m.subcontexts.append(d3)
        self.assertTrue(len(list(m.keys())) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {'cc': 5}
        self.assertTrue(len(list(m.keys())) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(list(m.keys())) == 3)
def test_checkpoint():
    d1 = DataContext()
    d1['a'] = object()
    d2 = DataContext()
    d2['b'] = object()
    m = MultiContext(d1, d2)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    for csc, msc in zip(copy.subcontexts, m.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in list(msc.keys()):
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy['a'] is m['a']
    assert copy['b'] is m['b']
def test_checkpoint():
    d1 = DataContext()
    d1['a'] = object()
    d2 = DataContext()
    d2['b'] = object()
    m = MultiContext(d1, d2)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    for csc, msc in zip(copy.subcontexts, m.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in msc.keys():
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy['a'] is m['a']
    assert copy['b'] is m['b']
def test_checkpoint_nested():
    d1 = DataContext()
    d1["a"] = object()
    d2 = DataContext()
    d2["b"] = object()
    m1 = MultiContext(d1, d2)
    m = MultiContext(m1)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    csc1 = copy.subcontexts[0]
    msc1 = m.subcontexts[0]
    for csc, msc in zip(csc1.subcontexts, msc1.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in msc.keys():
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy["a"] is m["a"]
    assert copy["b"] is m["b"]