def test_instance_data_array(self): s = Pizza(name='P', for_sale=True) s.save() for p in range(15): PizzaPrice(pizza=s, amount=str(p), date='2011-04-%s' % (p + 1)).save() response, json = self.do_get( '/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10, json) self.assertTrue(json.has_key('next_page'), json) self.assertEquals( json['next_page'], '/slumber/slumber_test/Pizza/data/1/prices/?start_after=6', json['next_page']) response, json = self.do_get( '/slumber/slumber_test/Pizza/data/1/prices/', {'start_after': '6'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 5) self.assertEquals( json['next_page'], '/slumber/slumber_test/Pizza/data/1/prices/?start_after=1') response, json = self.do_get( '/slumber/slumber_test/Pizza/data/1/prices/', {'start_after': '1'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 0) self.assertFalse(json.has_key('next_page'))
def test_update_instance(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_post('/slumber/slumber_test/Pizza/update/1/', { 'name': 'New pizza'}) self.assertEquals(response.status_code, 302) n = Pizza.objects.get(pk=1) self.assertEquals(n.name, "New pizza")
def test_delete_instance(self): s = Pizza(name='P') s.save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/' % s.pk) self.assertEquals(response.status_code, 200) self.assertTrue(json['operations'].has_key('delete'), json['operations']) response, json = self.do_post(json['operations']['delete'], {}) self.assertEquals(response.status_code, 200) with self.assertRaises(Pizza.DoesNotExist): Pizza.objects.get(pk=s.pk)
def test_instance_data(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/' % s.pk) self.maxDiff = None self.assertEquals(json, dict( fields=dict( id=dict(data=s.pk, type='django.db.models.fields.AutoField'), for_sale=dict(data=s.for_sale, type='django.db.models.fields.BooleanField'), max_extra_toppings=dict(data=s.max_extra_toppings, type='django.db.models.fields.IntegerField'), name=dict(data=s.name, type='django.db.models.fields.CharField')), display='S1', data_arrays=dict( prices='/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk)))
def test_get_instance(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber/slumber_test/Pizza/') get_url = json['operations']['get'] self.assertEquals(get_url, '/slumber/slumber_test/Pizza/get/') def check_query(query): response, json = self.do_get(get_url, query) self.assertEquals(response.status_code, 302, response) self.assertEquals(response['location'], 'http://localhost/slumber/slumber_test/Pizza/data/%s/' % s.pk) check_query({'pk': s.pk}) check_query({'id': s.pk}) check_query({'name': s.name})
def test_instance_data_array(self): s = Pizza(name='P', for_sale=True) s.save() for p in range(15): PizzaPrice(pizza=s, date='2011-04-%s' % (p+1)).save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10, json) self.assertTrue(json.has_key('next_page'), json) self.assertEquals(json['next_page'], '/slumber/slumber_test/Pizza/data/1/prices/?start_after=6', json['next_page']) response, json = self.do_get('/slumber/slumber_test/Pizza/data/1/prices/', {'start_after': '6'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 5) self.assertEquals(json['page'][0], { 'type': '/slumber/slumber_test/PizzaPrice/', 'pk': 5, 'data': '/slumber/slumber_test/PizzaPrice/data/5/', 'display': 'PizzaPrice object'}) self.assertFalse(json.has_key('next_page'), json.keys())
def test_instance_data_pizza(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/' % s.pk) self.maxDiff = None self.assertEquals(json, dict( _meta={'message': 'OK', 'status': 200}, identity='/slumber/slumber_test/Pizza/data/1/', display='S1', operations=dict( data='/slumber/slumber_test/Pizza/data/1/', delete='/slumber/slumber_test/Pizza/delete/1/', update='/slumber/slumber_test/Pizza/update/1/'), fields=dict( id=dict(data=s.pk, kind='value', type='django.db.models.fields.AutoField'), for_sale=dict(data=s.for_sale, kind='value', type='django.db.models.fields.BooleanField'), max_extra_toppings=dict(data=s.max_extra_toppings, kind='value', type='django.db.models.fields.IntegerField'), name=dict(data=s.name, kind='value', type='django.db.models.fields.CharField'), exclusive_to={'data': None, 'kind': 'object', 'type': '/slumber/slumber_test/Shop/'}), data_arrays=dict( prices='/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk)))
def test_instance_data(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/' % s.pk) self.maxDiff = None self.assertEquals( json, dict(fields=dict( id=dict(data=s.pk, type='django.db.models.fields.AutoField'), for_sale=dict(data=s.for_sale, type='django.db.models.fields.BooleanField'), max_extra_toppings=dict( data=s.max_extra_toppings, type='django.db.models.fields.IntegerField'), name=dict(data=s.name, type='django.db.models.fields.CharField')), display='S1', data_arrays=dict( prices='/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk)))
def test_instance_data_array(self): s = Pizza(name='P', for_sale=True) s.save() for p in range(15): PizzaPrice(pizza=s, amount=str(p), date='2011-04-%s' % (p+1)).save() response, json = self.do_get('/slumber/slumber_test/Pizza/data/%s/prices/' % s.pk) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10, json) self.assertTrue(json.has_key('next_page'), json) self.assertEquals(json['next_page'], '/slumber/slumber_test/Pizza/data/1/prices/?start_after=6', json['next_page']) response, json = self.do_get('/slumber/slumber_test/Pizza/data/1/prices/', {'start_after': '6'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 5) self.assertEquals(json['next_page'], '/slumber/slumber_test/Pizza/data/1/prices/?start_after=1') response, json = self.do_get('/slumber/slumber_test/Pizza/data/1/prices/', {'start_after': '1'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 0) self.assertFalse(json.has_key('next_page'))
def test_instance_data_pizzaprice(self): s = Pizza(name='p1', for_sale=True) s.save() p = PizzaPrice(pizza=s, date='2010-01-01') p.save() response, json = self.do_get('/slumber/slumber_test/PizzaPrice/data/%s/' % p.pk) self.assertEquals(json, dict( _meta={'message': 'OK', 'status': 200}, identity='/slumber/slumber_test/PizzaPrice/data/1/', display="PizzaPrice object", operations=dict( data='/slumber/slumber_test/PizzaPrice/data/1/', delete='/slumber/slumber_test/PizzaPrice/delete/1/', update='/slumber/slumber_test/PizzaPrice/update/1/'), fields=dict( id={'data': 1, 'kind': 'value', 'type': 'django.db.models.fields.AutoField'}, pizza={'data': { 'type': '/slumber/slumber_test/Pizza/', 'display':'p1', 'data': '/slumber/slumber_test/Pizza/data/1/'}, 'kind': 'object', 'type': '/slumber/slumber_test/Pizza/'}, date={'data': '2010-01-01', 'kind': 'value', 'type': 'django.db.models.fields.DateField'}, ), data_arrays={'amounts': '/slumber/slumber_test/PizzaPrice/data/1/amounts/'}))
def test_model_operation_instances_twelve_instances(self): for i in range(12): Pizza(name='S%s' % i, for_sale=True).save() response, json = self.do_get('/slumber/slumber_test/Pizza/instances/') self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10) self.assertEquals( json['next_page'], '/slumber/slumber_test/Pizza/instances/?start_after=3') response, json = self.do_get('/slumber/slumber_test/Pizza/instances/', {'start_after': '3'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 2) self.assertEquals( json['next_page'], '/slumber/slumber_test/Pizza/instances/?start_after=1') response, json = self.do_get('/slumber/slumber_test/Pizza/instances/', {'start_after': '1'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 0) self.assertFalse(json.has_key('next_page'), json)
def test_instance_data(self): s = Pizza(name='S1', for_sale=True) s.save() client = Client('localhost:8003', '/slumber') pizza = client.slumber_test.Pizza.get(pk=s.pk) self.assertEqual('S1', pizza.name)
def setUp(self): client._flush_client_instance_cache() self.s = Pizza(name='S1', for_sale=True) self.s.save() self.pizza = client.slumber_test.Pizza.get(pk=self.s.pk)
class TestsWithPizza(TestCase): def setUp(self): client._flush_client_instance_cache() self.s = Pizza(name='S1', for_sale=True) self.s.save() self.pizza = client.slumber_test.Pizza.get(pk=self.s.pk) def test_instance_type(self): self.assertEqual(self.s.pk, self.pizza.id) self.assertEqual(type(self.pizza).__name__, 'slumber_test.Pizza') pizza_type = str(type(self.pizza)) self.assertTrue(pizza_type.endswith("slumber_test.Pizza'>"), pizza_type) def test_instance_data(self): self.assertEqual('S1', self.pizza.name) prices = self.pizza.prices self.assertEqual(len(prices), 0) self.assertTrue(self.pizza.exclusive_to is None, self.pizza.exclusive_to) try: self.pizza.not_a_field self.fail("This should have thrown an AttributeError") except AttributeError: pass def test_instance_data_with_data_array(self): for p in range(15): PizzaPrice(pizza=self.s, date='2011-04-%s' % (p+1)).save() self.assertEqual('S1', self.pizza.name) prices = self.pizza.prices self.assertEquals(len(prices), 15) first_price = prices[0] self.assertEquals(unicode(first_price), "PizzaPrice object") self.assertEquals(first_price.pizza.for_sale, True) first_price_type = str(type(first_price)) self.assertTrue(first_price_type.endswith("slumber_test.PizzaPrice'>"), first_price_type) def test_instance_data_with_nested_data_array(self): p = PizzaPrice(pizza=self.s, date='2010-06-20') p.save() PizzaSizePrice(price=p, size='s', amount='13.95').save() PizzaSizePrice(price=p, size='m', amount='15.95').save() PizzaSizePrice(price=p, size='l', amount='19.95').save() self.assertEqual('S1', self.pizza.name) self.assertEqual(len(self.pizza.prices), 1) self.assertEqual(len(self.pizza.prices[0].amounts), 3) for a in self.pizza.prices[0].amounts: self.assertTrue(a.size in ['s', 'm', 'l'], a.size) def test_instance_no_pk(self): with self.assertRaises(AssertionError): pizza = client.slumber_test.Pizza.get() def test_2nd_pizza_comes_from_cache(self): # Force a cache read self.assertEqual(unicode(self.pizza), u"S1") # Make a 2nd alias to the same object fail = lambda *a, **f: self.fail("_InstanceConnector.__init__ called again %s, %s" % (a, f)) with patch('slumber.connector.instance._InstanceConnector.__init__', fail): pizza2 = client.slumber_test.Pizza.get(pk=self.s.pk) self.assertEqual(unicode(pizza2), u"S1") def test_pizza_not_found(self): with self.assertRaises(AssertionError): p2 = client.slumber_test.Pizza.get(pk=2) def test_aliase_writes_are_visible(self): m1 = client.slumber_test.Pizza.get(pk=1) m2 = client.slumber_test.Pizza.get(pk=1) self.assertEqual(m1.id, m2.id) with self.assertRaises(AttributeError): m1.attr with self.assertRaises(AttributeError): m2.attr m1.attr = 'attribute data' self.assertEqual(m1.attr, 'attribute data') self.assertEqual(m1.attr, m2.attr)
def test_model_operation_instances_one_instance(self): Pizza(name='S1', for_sale=True).save() response, json = self.do_get('/slumber/slumber_test/Pizza/instances/') self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 1)