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
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)
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)
def test_delete_with_id_calls_resource_delete(self): res = MockResource() m = Model(res, **ID_ATTRIBUTE_SET) m.delete() self.assertTrue(res.delete.called)
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
def test_delete_without_id_doesnt_call_resource_delete(self): res = MockResource() m = Model(res, **ATTRIBUTE_SET) m.delete() self.assertFalse(res.delete.called)
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)
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)
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)
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)
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)
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)
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')
def test_save_without_id_doesnt_call_update(self): res = MockResource() m = Model(res, **ATTRIBUTE_SET) m.save() self.assertFalse(res.update.called)
def test_save_without_id_calls_create(self): res = MockResource() m = Model(res, **ATTRIBUTE_SET) m.save() self.assertTrue(res.create.called)
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)
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))
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)
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
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]])
def _make_model(self, **kwargs): return Model(self, **kwargs)
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)