Exemple #1
0
    def test_delete_with_id_calls_resource_delete(self):
        res = MockResource()

        m = Model(res, **ID_ATTRIBUTE_SET)
        m.delete()

        self.assertTrue(res.delete.called)
Exemple #2
0
    def test_save_without_id_doesnt_call_update(self):
        res = MockResource()

        m = Model(res, **ATTRIBUTE_SET)
        m.save()

        self.assertFalse(res.update.called)
Exemple #3
0
    def test_save_with_id_when_exists_doesnt_call_create(self):
        res = MockResource(exists=True)

        m = Model(res, **ID_ATTRIBUTE_SET)
        m.save()

        self.assertFalse(res.create.called)
Exemple #4
0
    def test_delete_without_id_doesnt_call_resource_delete(self):
        res = MockResource()

        m = Model(res, **ATTRIBUTE_SET)
        m.delete()

        self.assertFalse(res.delete.called)
Exemple #5
0
    def test_save_with_id_when_exists_calls_update(self):
        res = MockResource(exists=True)

        m = Model(res, **ID_ATTRIBUTE_SET)
        m.save()

        self.assertTrue(res.update.called)
Exemple #6
0
    def test_save_with_id_when_not_exist_doesnt_call_update(self):
        res = MockResource()

        m = Model(res, **ID_ATTRIBUTE_SET)
        m.save()

        self.assertFalse(res.update.called)
Exemple #7
0
    def test_save_with_id_when_not_exist_calls_create(self):
        res = MockResource()

        m = Model(res, **ID_ATTRIBUTE_SET)
        m.save()

        self.assertTrue(res.create.called)
Exemple #8
0
    def test_save_without_id_calls_create(self):
        res = MockResource()

        m = Model(res, **ATTRIBUTE_SET)
        m.save()

        self.assertTrue(res.create.called)
Exemple #9
0
    def _make_model(self, **args):
        model = super()._make_model(**args)
        if getattr(model, 'products', None) is not None:
            model.products = [
                Model(OpfrontResource('/products', self._client), **prod)
                for prod in model.products
            ]

        if getattr(model, 'store', None) is not None:
            model.store = Model(OpfrontResource('/stores', self._client),
                                **model.store)

        return model
Exemple #10
0
    def test_model_serialized_replaces_submodels_by_model_ids(self):

        full_key = 'nested_model'
        id_key = full_key + '_id'

        nested_attrs = {
            **ATTRIBUTE_SET, full_key: Model(MagicMock(), **{'id': 'world'})
        }

        m = Model(MagicMock(), **nested_attrs)
        serialized = m.serialized
        self.assertNotIn(full_key, serialized)
        self.assertIn(id_key, serialized)
        self.assertEqual(serialized[id_key], nested_attrs[full_key].id)
Exemple #11
0
    def test_resource_update_returns_model_instance(self):
        client = MockClient(response=ATTRIBUTE_SET)
        res = OpfrontResource(ENDPOINT, client)

        m = res.update(Model(res, **ATTRIBUTE_SET))

        self.assertIsInstance(m, Model)
Exemple #12
0
    def _make_model(self, **args):
        model = super()._make_model(**args)
        if getattr(model, 'banner', None) is not None:
            model.banner = Model(OpfrontResource('/banners', self._client),
                                 **model.banner)

        return model
Exemple #13
0
    def test_resource_update_does_put_request(self):
        client = MockClient(response=ATTRIBUTE_SET)
        res = OpfrontResource(ENDPOINT, client)

        res.update(Model(res, **ATTRIBUTE_SET))

        self.assertEqual(client.do_request.call_args[0][1], 'PATCH')
Exemple #14
0
    def test_resource_create_does_not_append_to_url(self):
        client = MockClient(response=ATTRIBUTE_SET)
        res = OpfrontResource(ENDPOINT, client)

        res.create(Model(res, **ATTRIBUTE_SET))

        self.assertEqual(client.do_request.call_args[0][0], ENDPOINT)
Exemple #15
0
    def test_model_serialized_returns_all_required_attributes(self):
        m = Model(MagicMock(), **ATTRIBUTE_SET)

        serialized = m.serialized

        for k, v in ATTRIBUTE_SET.items():
            self.assertEqual(serialized.get(k), v)
Exemple #16
0
    def test_model_serialized_returns_no_extra_attributes(self):
        m = Model(MagicMock(), **ATTRIBUTE_SET)

        serialized = m.serialized

        for k, v in serialized.items():
            self.assertEqual(ATTRIBUTE_SET.get(k), v)
Exemple #17
0
    def test_resource_update_appends_id_to_url(self):
        client = MockClient(response=ATTRIBUTE_SET)
        res = OpfrontResource(ENDPOINT, client)

        res.update(Model(res, **ATTRIBUTE_SET))

        self.assertEqual(client.do_request.call_args[0][0],
                         '{0}/{1}'.format(ENDPOINT, RES_ID))
Exemple #18
0
    def test_resource_update_calls_do_request(self):
        client = MockClient(response=ATTRIBUTE_SET)
        res = OpfrontResource(ENDPOINT, client)

        res.update(Model(res, **ATTRIBUTE_SET))

        self.assertTrue(client.do_request.called)
        self.assertEqual(client.do_request.call_count, 1)
Exemple #19
0
    def _make_model(self, **args):
        model = super()._make_model(**args)
        if getattr(model, 'spectacle', None) is not None:
            model.spectacle = Model(
                OpfrontResource('/spectacles', self._client),
                **model.spectacle)

        return model
Exemple #20
0
    def test_model_serialized_replaces_list_of_submodels_by_list_of_model_ids(
            self):
        full_key = 'nested_models'
        id_key = 'nested_model_ids'

        nested_attrs = {
            **ATTRIBUTE_SET, full_key: [
                Model(MagicMock(), **{'id': 'hello'}),
                Model(MagicMock(), **{'id': 'world'})
            ]
        }

        model = Model(MagicMock(), **nested_attrs)
        serialized = model.serialized

        self.assertNotIn(full_key, serialized)
        self.assertIn(id_key, serialized)
        self.assertEqual(serialized[id_key],
                         [m.id for m in nested_attrs[full_key]])
Exemple #21
0
 def _make_model(self, **kwargs):
     return Model(self, **kwargs)
Exemple #22
0
    def test_model_adds_attributes_correctly(self):
        m = Model(MagicMock(), **ATTRIBUTE_SET)

        for k, v in ATTRIBUTE_SET.items():
            self.assertEqual(getattr(m, k), v)