class TestQueries(ConfigureUser, ServiceTestsWithDirectory, DBTestCase): REAL_URL = 'http://localhost:8000/slumber/pizzas/slumber_examples/Shop/data/1/' SLUMBER_URL = 'slumber://pizzas/slumber_examples/Shop/data/1/' def setUp(self): super(TestQueries, self).setUp() self.shop = Shop(name='Test', slug='test', active=True) self.shop.save() def test_have_shop(self): with patch('slumber._client', Client()): shop = client.pizzas.slumber_examples.Shop.get(pk=1) self.assertEqual(shop._url, self.REAL_URL) def test_find_order_by_shop_remote_instance(self): with patch('slumber._client', Client()): shop = client.pizzas.slumber_examples.Shop.get(pk=1) order = Order(shop=shop) order.save() fetched = Order.objects.get(shop=shop) self.assertEqual(order, fetched) def test_find_order_by_shop_url(self): with patch('slumber._client', Client()): shop = client.pizzas.slumber_examples.Shop.get(pk=1) order = Order(shop=shop) order.save() fetched = Order.objects.get(shop=self.SLUMBER_URL) self.assertEqual(order, fetched)
def test_shop_instance_has_web_address(self): shop = Shop(name='Test', slug='test') shop.save() self.assertEquals(shop.web_address, 'http://www.example.com/test/') rshop = client.slumber_examples.Shop.get(pk=shop.pk) self.assertEquals(rshop.slug, 'test') self.assertEquals(rshop.web_address, 'http://www.example.com/test/')
def test_instance_data_order(self): shop = Shop(name="Home", slug='home', active=True) shop.save() order = Order(shop=self.url("/slumber_examples/Shop/data/%s/" % shop.pk)) order.save() response, json = self.do_get('/slumber_examples/Order/data/%s/' % order.pk) self.assertEquals(response.status_code, 200) expected = dict( _meta={'message': 'OK', 'status': 200, 'username': '******'}, type=self.url('/slumber_examples/Order/'), identity=self.url('/slumber_examples/Order/data/1/'), display="Order object", operations=dict( data=self.url('/slumber_examples/Order/data/1/'), delete=self.url('/slumber_examples/Order/delete/1/'), update=self.url('/slumber_examples/Order/update/1/')), fields=dict( id={'data': 1, 'kind': 'value', 'type': 'django.db.models.fields.AutoField'}, nested={'data': None, 'kind': 'value', 'type': 'slumber.fields.RemoteForeignKey'}, shop={'data': self.url('/slumber_examples/Shop/data/%s/' % shop.pk), 'kind': 'value', 'type': 'slumber.fields.RemoteForeignKey'} ), data_arrays={}) self.assertEquals(json, expected)
def test_to_json_configuration(self): shop = Shop(name='Test', slug='test') shop.save() configure(Shop, to_json={ 'slumber_examples.Shop.web_address': lambda m, i, fm, v: v.replace('http:', 'https:') }) rshop = client.slumber_examples.Shop.get(pk=shop.pk) self.assertEquals(rshop.slug, 'test') self.assertEquals(rshop.web_address, 'https://www.example.com/test/') from slumber.server.json import DATA_MAPPING del DATA_MAPPING['slumber_examples.Shop.web_address']
def test_to_json_configuration(self): shop = Shop(name='Test', slug='test') shop.save() configure(Shop, to_json = { 'slumber_examples.Shop.web_address': lambda m, i, fm, v: v.replace('http:', 'https:') }) rshop = client.slumber_examples.Shop.get(pk=shop.pk) self.assertEquals(rshop.slug, 'test') self.assertEquals(rshop.web_address, 'https://www.example.com/test/') from slumber.server.json import DATA_MAPPING del DATA_MAPPING['slumber_examples.Shop.web_address']
def test_instance_data_shop_with_null_active(self): s = Shop(name='Test shop', slug='test-shop') s.save() response, json = self.do_get('/slumber_examples/Shop/data/%s/' % s.pk) self.maxDiff = None self.assertEquals( json, dict(_meta={ 'message': 'OK', 'status': 200, 'username': '******' }, type=self.url('/slumber_examples/Shop/'), identity=self.url('/slumber_examples/Shop/data/1/'), display='Test shop', operations=dict( data=self.url('/slumber_examples/Shop/data/1/'), delete=self.url('/slumber_examples/Shop/delete/1/'), update=self.url('/slumber_examples/Shop/update/1/')), fields=dict( id=dict(data=s.pk, kind='value', type='django.db.models.fields.AutoField'), active=dict( data=None, kind='value', type='django.db.models.fields.NullBooleanField'), name={ 'data': 'Test shop', 'kind': 'value', 'type': 'django.db.models.fields.CharField' }, slug={ 'data': 'test-shop', 'kind': 'value', 'type': 'django.db.models.fields.CharField' }, web_address={ 'data': 'http://www.example.com/test-shop/', 'kind': 'property', 'type': 'slumber_examples.Shop.web_address' }), data_arrays={ 'pizza': self.url('/slumber_examples/Shop/data/1/pizza/') }))
def test_instance_data_shop_with_null_active(self): s = Shop(name='Test shop', slug='test-shop') s.save() response, json = self.do_get('/slumber_examples/Shop/data/%s/' % s.pk) self.maxDiff = None self.assertEquals(json, dict( _meta={'message': 'OK', 'status': 200, 'username': '******'}, type=self.url('/slumber_examples/Shop/'), identity=self.url('/slumber_examples/Shop/data/1/'), display='Test shop', operations=dict( data='/slumber/pizzas/shop/%s/' % s.pk, update=self.url('/slumber_examples/Shop/update/1/')), fields=dict( id=dict(data=s.pk, kind='value', type='django.db.models.fields.AutoField'), active=dict(data=None, kind='value', type='django.db.models.fields.NullBooleanField'), name={'data': 'Test shop', 'kind': 'value', 'type': 'django.db.models.fields.CharField'}, slug={'data': 'test-shop', 'kind': 'value', 'type': 'django.db.models.fields.CharField'}, web_address={'data': 'http://www.example.com/test-shop/', 'kind': 'property', 'type': 'slumber_examples.Shop.web_address'}), data_arrays={'pizza': self.url('/slumber_examples/Shop/data/1/pizza/')}))
def setUp(self): super(TestQueries, self).setUp() self.shop = Shop(name='Test', slug='test', active=True) self.shop.save()