def test_saving_item_with_default_values(self): """ Test saving item records with default values """ item = Item() item.save() self.assertIn(item, Item.objects.all())
def test_saving_purchase_with_default_values(self): """ Test saving item records with default values """ item = Item() item.save() purchase = Purchase(item=item) purchase.save() self.assertIn(purchase, Purchase.objects.all())
def test_blank_item_names_are_not_allowed(self): """ Test application logic that prevents blank items names from being saved to the database. """ item = Item(name='') with self.assertRaises(ValidationError): item.save() item.full_clean()
def test_duplicate_item_names_are_not_allowed(self): """ Test application logic that prevents duplicate items names from being saved to the database. """ Item.objects.create(name='ITEM A') Item.objects.create(name='ITEM B') item = Item(name='ITEM A') with self.assertRaises(IntegrityError): item.save() item.full_clean()
def form_view(request): """ Render item purchase form, and handle the submitted data. """ form = None item = Item() if 'POST' == request.method: form = PurchaseForm(data=request.POST) if form.is_valid(): # pragma: no branch form.save() result = { 'code': 0, 'message': 'success', } return HttpResponse(json.dumps(result), content_type='application/json') else: try: itemid = int(request.GET['item']) if 0 < itemid: item = Item.objects.get(pk=itemid) form = PurchaseForm(data={'item': item}) except Item.DoesNotExist: pass except MultiValueDictKeyError: pass return render(request, 'items/purchase.html', {'form': form, 'item': item})
def test_default_values(self): """ Test if vanilla item is created with correct default values """ item = Item() purchase = Purchase(item=item) self.assertEqual(purchase.item, item) self.assertEqual(purchase.quantity, 1) self.assertEqual(purchase.date, datetime.date.today())
def test_template_receive_the_form_via_context(self): """ Purchase form template should have the PurchaseForm instance in it's context. """ # No item specified response = self.client.get(self.uri) self.assertIn('form', response.context) self.assertEqual(response.context['form'], None) self.assertIn('item', response.context) self.assertEqual(type(response.context['item']), type(Item())) # Non-existing item specified response = self.client.get(self.uri + '?item=1') self.assertIn('form', response.context) self.assertEqual(response.context['form'], None) self.assertIn('item', response.context) self.assertEqual(type(response.context['item']), type(Item())) # Valid item specified item = Item.objects.create() response = self.client.get(self.uri + '?item=1') self.assertIn('form', response.context) self.assertEqual(type(response.context['form']), type(PurchaseForm())) self.assertIn('item', response.context) self.assertEqual(response.context['item'], item) # Invalid item specified response = self.client.get(self.uri + '?item=-1') self.assertIn('form', response.context) self.assertEqual(response.context['form'], None) self.assertIn('item', response.context) self.assertEqual(type(response.context['item']), type(Item()))
def test_default_values(self): """ Test if vanilla item is created with correct default values """ item = Item() self.assertEqual(item.name, '') self.assertEqual(item.unit_symbol, '') self.assertEqual(item.unit_weight, 1) self.assertEqual(item.purchase_threshold, 21) self.assertEqual(item.extended_threshold, 0) self.assertEqual(item.heavy, False) self.assertEqual(item.stock_age, None) self.assertEqual(item.stock_age_percent, 0)