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_resource_delete_does_delete_request(self): client = MockClient() res = OpfrontResource(ENDPOINT, client) res.delete(RES_ID) self.assertEqual(client.do_request.call_args[0][1], 'DELETE')
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_resource_get_calls_do_request_with_http_get(self): client = MockClient() res = OpfrontResource(ENDPOINT, client) res.get(RES_ID) self.assertEqual(client.do_request.call_args[0][1], 'GET')
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_list_returns_correct_item_count(self): client = MockClient(response=LIST_BODY) res = OpfrontResource(ENDPOINT, client) hits = [hit for hit in res.list(page_size=PAGE_SIZE)] self.assertEqual(len(hits), TOTAL_LIST_HITS)
def test_resource_get_returns_model_instance(self): client = MockClient() res = OpfrontResource(ENDPOINT, client) m = res.get(RES_ID) self.assertIsInstance(m, Model)
def test_resource_list_returns_a_generator(self): client = MockClient(response=LIST_BODY) res = OpfrontResource(ENDPOINT, client) hits = res.list() self.assertIsInstance(hits, types.GeneratorType)
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 test_resource_list_perform_correct_number_of_request(self): client = MockClient(response=LIST_BODY) res = OpfrontResource(ENDPOINT, client) hits = [hit for hit in res.list(page_size=PAGE_SIZE)] self.assertEqual(client.do_request.call_count, TOTAL_LIST_HITS / PAGE_SIZE)
def test_resource_get_appends_id_to_endpoint(self): client = MockClient() res = OpfrontResource(ENDPOINT, client) res.get(RES_ID) self.assertEqual(client.do_request.call_args[0][0], '{0}/{1}'.format(ENDPOINT, RES_ID))
def test_resource_get_returns_model_with_response_attributes(self): client = MockClient(response=ATTRIBUTE_SET) res = OpfrontResource(ENDPOINT, client) m = res.get(RES_ID) for k, v in ATTRIBUTE_SET.items(): self.assertEqual(getattr(m, k), v)
def test_resource_get_calls_do_request(self): client = MockClient() res = OpfrontResource(ENDPOINT, client) res.get(RES_ID) self.assertTrue(client.do_request.called) self.assertEqual(client.do_request.call_count, 1)
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 __init__(self, email, password, **kwargs): client = OpfrontClient(email, password, **kwargs) # Resource definition self.banner = OpfrontResource('/banners', client) self.spectacle = OpfrontResource('/spectacles', client) self.store = StoreResource('/stores', client) self.product = ProductResource('/products', client) self.order = OrderResource('/orders', client) self.user = UserResource('/users', client)
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 _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 _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_resource_exists_returns_true_when_no_exception_is_raised(self): client = MockClient(response=ATTRIBUTE_SET) res = OpfrontResource(ENDPOINT, client) self.assertTrue(res.exists(RES_ID))
def test_resource_exists_returns_false_when_resource_not_found_is_raised( self): client = MockClient(should_raise=ResourceNotFoundError) res = OpfrontResource(ENDPOINT, client) self.assertFalse(res.exists(RES_ID))
def test_resource_exists_reraises_other_exceptions(self): client = MockClient(response=ATTRIBUTE_SET) res = OpfrontResource(ENDPOINT, client) with self.assertRaises(ValueError): res.exists(RES_ID)
def test_resource_call_returns_model(self): res = OpfrontResource(ENDPOINT, MockClient()) m = res(**ATTRIBUTE_SET) self.assertIsInstance(m, Model)
def test_resource_passes_attributes_to_model(self): res = OpfrontResource(ENDPOINT, MockClient()) m = res(**ATTRIBUTE_SET) for k, v in ATTRIBUTE_SET.items(): self.assertEqual(getattr(m, k), v)
def test_resource_call_passes_self_to_model(self): res = OpfrontResource(ENDPOINT, MockClient()) m = res(**ATTRIBUTE_SET) self.assertIs(m._res, res)