示例#1
0
    def test_register(self):
        context = Context()
        dbm_backend.register(context)
        self._store.save(self._person)

        # creating a different connection to test...
        connection = context.get_connection("dbm://%s" % self._tempfile.name)
        store = connection.get_store()
        person = store.fetch(Person, "foobar")
        self.assertEqual(person, self._person)
示例#2
0
文件: test_context.py 项目: nod/norm
 def setUp(self):
     self._context = Context()
     self._mock_connection = Mock()
     self._mock_connection2 = Mock()
     self._context.register_connection("mock", self._mock_connection)
     self._context.register_connection("mock2", self._mock_connection2)
示例#3
0
文件: test_context.py 项目: nod/norm
class TestNormContext(TestCase):
    def setUp(self):
        self._context = Context()
        self._mock_connection = Mock()
        self._mock_connection2 = Mock()
        self._context.register_connection("mock", self._mock_connection)
        self._context.register_connection("mock2", self._mock_connection2)

    def test_context_register_connection(self):
        self._context.get_connection("mock://host")
        self._mock_connection.from_uri.assert_called_with("mock://host")
        self._context.get_connection("mock2://host")
        self._mock_connection2.from_uri.assert_called_with("mock2://host")

    def test_store_context_descriptor(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        result = Model.store.fetch("foobar")
        mock_store.fetch.assert_called_with(Model, "foobar")
        self.assertEqual(result, mock_store.fetch.return_value)

        model = Model(id="foobar", foo="bar")
        result = model.store.save()
        mock_store.save.assert_called_with(model)
        self.assertEqual(result, mock_store.save.return_value)

    def test_store_context_model_mismatch(self):
        mock_store = Mock()
        mock_store.serialize._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        with self.assertRaises(AttributeError):
            Model.store.serialize()

        with self.assertRaises(AttributeError):
            Model().store.unregistered()

    def test_store_context_instance(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        # an instance should be able to perform both serialization
        # and deserialization methods on a store
        instance = Model()
        instance.store.save()
        mock_store.save.assert_called_with(instance)

        instance.store.fetch("foo")
        mock_store.fetch.assert_called_with(Model, "foo")

    def test_store_context_wrapper(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            use = StoreContextWrapper()

        M = Model.use(mock_store)
        result = M.store.fetch("foobar")
        mock_store.fetch.assert_called_with(M, "foobar")
        self.assertEqual(result, mock_store.fetch.return_value)

        m = M(foo="bar")
        m.store.save()
        mock_store.save.assert_called_with(m)
示例#4
0
 def setUp(self):
     self._context = Context()
     self._mock_connection = Mock()
     self._mock_connection2 = Mock()
     self._context.register_connection("mock", self._mock_connection)
     self._context.register_connection("mock2", self._mock_connection2)
示例#5
0
class TestNormContext(TestCase):

    def setUp(self):
        self._context = Context()
        self._mock_connection = Mock()
        self._mock_connection2 = Mock()
        self._context.register_connection("mock", self._mock_connection)
        self._context.register_connection("mock2", self._mock_connection2)

    def test_context_register_connection(self):
        self._context.get_connection("mock://host")
        self._mock_connection.from_uri.assert_called_with("mock://host")
        self._context.get_connection("mock2://host")
        self._mock_connection2.from_uri.assert_called_with("mock2://host")

    def test_store_context_descriptor(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        result = Model.store.fetch("foobar")
        mock_store.fetch.assert_called_with(Model, "foobar")
        self.assertEqual(result, mock_store.fetch.return_value)

        model = Model(id="foobar", foo="bar")
        result = model.store.save()
        mock_store.save.assert_called_with(model)
        self.assertEqual(result, mock_store.save.return_value)

    def test_store_context_model_mismatch(self):
        mock_store = Mock()
        mock_store.serialize._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        with self.assertRaises(AttributeError):
            Model.store.serialize()

        with self.assertRaises(AttributeError):
            Model().store.unregistered()

    def test_store_context_instance(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            store = StoreContext(mock_store)

        # an instance should be able to perform both serialization
        # and deserialization methods on a store
        instance = Model()
        instance.store.save()
        mock_store.save.assert_called_with(instance)

        instance.store.fetch("foo")
        mock_store.fetch.assert_called_with(Model, "foo")

    def test_store_context_wrapper(self):
        mock_store = Mock()
        mock_store.fetch._NORM_DESERIALIZE = True
        mock_store.save._NORM_SERIALIZE = True

        class Model(Base):
            use = StoreContextWrapper()

        M = Model.use(mock_store)
        result = M.store.fetch("foobar")
        mock_store.fetch.assert_called_with(M, "foobar")
        self.assertEqual(result, mock_store.fetch.return_value)

        m = M(foo="bar")
        m.store.save()
        mock_store.save.assert_called_with(m)