def test_insert_process(self):
        query = flexmock(Builder)

        model = OrmModelStub()
        query_builder = flexmock(QueryBuilder)
        query_builder.should_receive('insert_get_id').once().with_args({'name': 'john'}, 'id').and_return(1)
        model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None)))
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive('fire').once()\
            .with_args('saving: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('creating: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('created: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('saved: %s' % model.__class__.__name__, model)\
            .and_return(True)

        model.name = 'john'
        model.set_exists(False)
        self.assertTrue(model.save())
        self.assertEqual(1, model.id)
        self.assertTrue(model.exists)
        self.assertTrue(model._update_timestamps.called)

        model = OrmModelStub()
        query_builder.should_receive('insert').once().with_args({'name': 'john'})
        model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None)))
        model._update_timestamps = mock.MagicMock()
        model.set_incrementing(False)

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive('fire').once()\
            .with_args('saving: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('creating: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('created: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('saved: %s' % model.__class__.__name__, model)\
            .and_return(True)

        model.name = 'john'
        model.set_exists(False)
        self.assertTrue(model.save())
        self.assertFalse(hasattr(model, 'id'))
        self.assertTrue(model.exists)
        self.assertTrue(model._update_timestamps.called)
示例#2
0
    def test_update_process(self):
        query = flexmock(Builder)
        query.should_receive('where').once().with_args('id', 1)
        query.should_receive('update').once().with_args({'name': 'john'})

        model = OrmModelStub()
        model.new_query = mock.MagicMock(
            return_value=Builder(QueryBuilder(None, None, None)))
        model._update_timestamps = mock.MagicMock()
        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive('fire').once()\
            .with_args('saving: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('updating: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('updated: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('saved: %s' % model.__class__.__name__, model)\
            .and_return(True)

        model.id = 1
        model.foo = 'bar'
        model.sync_original()
        model.name = 'john'
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        model._update_timestamps.assert_called_once_with()
示例#3
0
    def test_update_process_uses_old_primary_key(self):
        query = flexmock(Builder)
        query.should_receive("where").once().with_args("id", 1)
        query.should_receive("update").once().with_args({"id": 2, "name": "john"})

        model = models.OrmModelStub()
        model.new_query = mock.MagicMock(
            return_value=Builder(QueryBuilder(None, None, None))
        )
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive("fire").once().with_args(
            "saving: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "updating: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "updated: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "saved: %s" % model.__class__.__name__, model
        ).and_return(True)

        model.id = 1
        model.sync_original()
        model.id = 2
        model.name = "john"
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        self.assertTrue(model._update_timestamps.called)
    def test_insert_is_cancelled_if_creating_event_returns_false(self):
        model = flexmock(OrmModelStub())
        query = flexmock(Builder(flexmock(QueryBuilder(None, None, None))))
        model.should_receive('new_query_without_scopes').once().and_return(query)
        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive('fire').once()\
            .with_args('saving: %s' % model.__class__.__name__, model)\
            .and_return(True)
        events.should_receive('fire').once()\
            .with_args('creating: %s' % model.__class__.__name__, model)\
            .and_return(False)

        self.assertFalse(model.save())
        self.assertFalse(model.exists)
示例#5
0
    def test_update_is_cancelled_if_updating_event_returns_false(self):
        model = flexmock(models.OrmModelStub())
        query = flexmock(Builder(flexmock(QueryBuilder(None, None, None))))
        model.should_receive("new_query_without_scopes").once().and_return(query)
        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive("fire").once().with_args(
            "saving: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "updating: %s" % model.__class__.__name__, model
        ).and_return(False)
        model.set_exists(True)
        model.foo = "bar"

        self.assertFalse(model.save())
    def test_update_process_without_timestamps(self):
        query = flexmock(Builder)
        query.should_receive('where').once().with_args('id', 1)
        query.should_receive('update').once().with_args({'name': 'john'})

        model = flexmock(OrmModelStub())
        model.__timestamps__ = False
        model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None)))
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        model.should_receive('_fire_model_event').and_return(True)

        model.id = 1
        model.sync_original()
        model.name = 'john'
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        self.assertFalse(model._update_timestamps.called)
示例#7
0
    def test_update_process_does_not_override_timestamps(self):
        query = flexmock(Builder)
        query.should_receive("where").once().with_args("id", 1)
        query.should_receive("update").once().with_args(
            {"created_at": "foo", "updated_at": "bar"}
        )

        model = models.OrmModelStub()
        model.new_query = mock.MagicMock(
            return_value=Builder(QueryBuilder(None, None, None))
        )
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive("fire").once().with_args(
            "saving: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "updating: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "updated: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "saved: %s" % model.__class__.__name__, model
        ).and_return(True)

        model.id = 1
        model.sync_original()
        model.created_at = "foo"
        model.updated_at = "bar"
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        self.assertTrue(model._update_timestamps.called)
示例#8
0
    def test_insert_process(self):
        query = flexmock(Builder)

        model = models.OrmModelStub()
        query_builder = flexmock(QueryBuilder)
        query_builder.should_receive("insert_get_id").once().with_args(
            {"name": "john"}, "id"
        ).and_return(1)
        model.new_query = mock.MagicMock(
            return_value=Builder(QueryBuilder(None, None, None))
        )
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive("fire").once().with_args(
            "saving: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "creating: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "created: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "saved: %s" % model.__class__.__name__, model
        ).and_return(True)

        model.name = "john"
        model.set_exists(False)
        self.assertTrue(model.save())
        self.assertEqual(1, model.id)
        self.assertTrue(model.exists)
        self.assertTrue(model._update_timestamps.called)

        model = models.OrmModelStub()
        query_builder.should_receive("insert").once().with_args({"name": "john"})
        model.new_query = mock.MagicMock(
            return_value=Builder(QueryBuilder(None, None, None))
        )
        model._update_timestamps = mock.MagicMock()
        model.set_incrementing(False)

        events = flexmock(Event())
        model.__dispatcher__ = events
        events.should_receive("fire").once().with_args(
            "saving: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "creating: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "created: %s" % model.__class__.__name__, model
        ).and_return(True)
        events.should_receive("fire").once().with_args(
            "saved: %s" % model.__class__.__name__, model
        ).and_return(True)

        model.name = "john"
        model.set_exists(False)
        self.assertTrue(model.save())
        self.assertFalse(hasattr(model, "id"))
        self.assertTrue(model.exists)
        self.assertTrue(model._update_timestamps.called)