def test_post_from_location_form__form_valid(self): client = self.setup_user_and_client('Jerlene', 'Elder') mode = ManualPalletMoveView.MODE_ENTER_FROM_LOCATION location = Location.get_location('01', '01', 'B1') Box.objects.create( box_type=Box.box_type_default(), box_number=BoxNumber.format_box_number(42), location=location, ) response = client.post( self.url, { 'mode': mode, 'from-loc_row': location.loc_row.pk, 'from-loc_bin': location.loc_bin.pk, 'from-loc_tier': location.loc_tier.pk, } ) self.assertEqual( 200, response.status_code, response.content.decode() ) self.assertEqual( ManualPalletMoveView.MODE_ENTER_TO_LOCATION, response.context.get('mode'), ) form = response.context.get('to_location_form') self.assertIsInstance(form, MoveToLocationForm)
def test_get_form(self): box_number = BoxNumber.format_box_number(15) pallet_box = PalletBox(box_number=box_number, ) form = BoxItemFormView.get_form(pallet_box) self.assertIsNotNone(form) self.assertIsNone(form.prefix) self.assertEqual(box_number, form.initial['box_number'])
def test_get_success_url(self): # create user for this test (It will only exist briefly in the test # database). # create an associated profile - just for the test user = User.objects.create_user('awesterville', '*****@*****.**', 'abc123') profile = Profile.objects.create( title='Test User', user=user, ) # Client sends HTTP requests and receives HTTP responses like a user's # browser. It doesn't run any JavaScript, for that you need to use # Selenium to control a real browser. client = Client() # The first time I ran this test, I found that response.url was the # login page. Ooops, forgot to log in. Call the force_login method # to make Django act like we've gone through the login page client.force_login(user) box_number = BoxNumber.format_box_number(1) url = reverse('fpiweb:box_new', args=(box_number, )) box_type = BoxType.objects.get(box_type_code='Evans') post_data = { 'box_number': box_number, 'box_type': box_type.pk, } response = client.post(url, post_data) # Check whether the HTTP status code is 200, if it's not display the # text of the page returned. Sometimes this makes looking for what # happened like looking for a needle in a haystack. # Unit-testing frameworks like unittest or pytest may discard text # sent to the terminal (stdout) unless a test fails. # print(dir(response)) # Here's a handy page to bookmark: # https://en.wikipedia.org/wiki/List_of_HTTP_status_codes # # Status code 418 is my favorite! self.assertEqual(302, response.status_code, str(response.content)) box = Box.objects.order_by('-pk').first() self.assertEqual(reverse('fpiweb:box_details', args=(box.pk, )), response.url) box = Box.objects.get(box_number=box_number) self.assertEqual(box_type, box.box_type) self.assertEqual(box_type.box_type_qty, box.quantity)
def get(self, request, **kwargs): box_number = kwargs.get('number') if box_number is None: return error_page(request, "missing kwargs['number']") box_number = BoxNumber.format_box_number(box_number) try: box = Box.objects.get(box_number=box_number) except Box.DoesNotExist: return redirect('fpiweb:box_new', box_number=box_number) if not box.product: return redirect('fpiweb:box_edit', pk=box.pk) return redirect('fpiweb:box_empty_move', pk=box.pk)
def test_expire_months(self): """ensure that start month <= end month""" post_data = { 'box_number': BoxNumber.format_box_number(12), 'product': Product.objects.first().pk, 'exp_year': 2022, 'exp_month_start': 5, 'exp_month_end': 3, } form = BoxItemForm(post_data) self.assertFalse(form.is_valid()) self.assertIn( 'Exp month end must be later than or equal to Exp month start', form.non_field_errors() )
def clean(self, value): value = super().clean(value) if BoxNumber.validate(value): return value.upper() # Did the user just enter digit? Try and turn this # into a valid box number try: value = int(value) except (TypeError, ValueError): raise ValidationError( '%(value)s is not a valid box number', params={'value': value}, ) return BoxNumber.format_box_number(value)
def get(self, request, **kwargs): box_number = kwargs.get('number') if box_number is None: return error_page(request, "missing kwargs['number']") box_number = BoxNumber.format_box_number(box_number) action = request.session.get('action') if action != Action.ACTION_BUILD_PALLET: return error_page(request, "What to do when action is {}?".format(action)) try: box = Box.objects.get(box_number=box_number) except Box.DoesNotExist: return redirect('fpiweb:box_new', box_number=box_number) return redirect('fpiweb:build_pallet_add_box', args=(box.pk, ))
def clean(self, value: str) -> str: value = super().clean(value) formal_box_number = 'BOX0000' raise_error = False if BoxNumber.validate(value): formal_box_number = value.upper() elif value.isdigit(): # Did the user just enter digit? Try and turn this # into a valid box number formal_box_number = BoxNumber.format_box_number(int(value)) if not BoxNumber.validate(formal_box_number): raise_error = True else: raise_error = True if raise_error: raise ValidationError( '%(value)s is not a valid box number', params={'value': value}, ) return formal_box_number
def test_clean(self): # ---------------------------------------------------------------- # super class's clean detects error (i.e. location doesn't exist) # ---------------------------------------------------------------- loc_row = '03' loc_bin = '03' loc_tier = 'A1' location = Location.get_location(loc_row, loc_bin, loc_tier) location.delete() form = ExistingLocationWithBoxesForm({ 'loc_row': location.loc_row, 'loc_bin': location.loc_bin, 'loc_tier': location.loc_tier }) self.assertFalse(form.is_valid()) self.assertEqual( {'__all__': ['Location 03, 03, A1 does not exist.']}, form.errors, ) self.assertEqual( ['Location 03, 03, A1 does not exist.'], form.non_field_errors(), ) # --------------------------- # Try a location w/out boxes # --------------------------- location = Location.objects.annotate( box_count=Count('box') ).filter( box_count=0 ).first() form = ExistingLocationWithBoxesForm({ 'loc_row': location.loc_row, 'loc_bin': location.loc_bin, 'loc_tier': location.loc_tier, }) self.assertFalse(form.is_valid()) expected_error = "Location {}, {}, {} doesn't have any boxes".format( location.loc_row.loc_row, location.loc_bin.loc_bin, location.loc_tier.loc_tier, ) self.assertEqual( {'__all__': [expected_error]}, form.errors, ) self.assertEqual( [expected_error], form.non_field_errors(), ) # --------------------------------------------- # Add a box to the location form will validate # --------------------------------------------- Box.objects.create( box_type=Box.box_type_default(), box_number=BoxNumber.format_box_number(111), location=location, ) form = ExistingLocationWithBoxesForm({ 'loc_row': location.loc_row, 'loc_bin': location.loc_bin, 'loc_tier': location.loc_tier, }) self.assertTrue(form.is_valid()) self.assertEqual( location, form.cleaned_data['location'], )
def test_pallet_finish(self) -> None: """ Test loading and finishing off a pallet. Build a new pallet, add some boxes to it, stash the pallet id, the pallet box ids, and the box ids associated with them. Then finish the pallet and make sure the pallet and all its pallet boxes have been deleted, while the boxes themselves have been preserved. Since we are going through the new and fill logic above, we are going to assume the activity records have been properly created. :return: """ # set some arbitrary values pallet_name = 'Hopefully this never matches !@#$%^&*()_+' location_code = '0409C2' box_type_code = 'Evans' starting_box_number = 98765 number_of_boxes = 40 ending_box_number = starting_box_number + number_of_boxes product_choices = 'Corn', 'Green Beans' exp_year_choices = (now().year + 1), (now().year + 2) # get corresponding records box_type_rec = BoxType.objects.get(box_type_code=box_type_code) product1 = Product.objects.get(prod_name=product_choices[0]) product2 = Product.objects.get(prod_name=product_choices[1]) product_rec_choices = product1, product2 bm = BoxManagementClass() # build the pallet location_rec = Location.objects.get(loc_code=location_code) pallet_rec = Pallet.objects.create( name=pallet_name, location=location_rec, pallet_status=Pallet.FILL, ) pallet_rec_id = pallet_rec.id # build table of values for later comparison pallet_box_info = dict() for ndx, box_number in enumerate( range(starting_box_number, ending_box_number)): ind = ndx % 2 box_name = BoxNumber.format_box_number(box_number) product = product_rec_choices[ind] exp_year = exp_year_choices[ind] box_rec = bm.box_new(box_number=box_name, box_type=box_type_rec) pallet_box_rec = PalletBox.objects.create(pallet=pallet_rec, box_number=box_name, box=box_rec, product=product, exp_year=exp_year, box_status=PalletBox.NEW) pallet_box_info[box_number] = PalletBoxInfo( pallet_box_id=pallet_box_rec.id, box_id=box_rec.id, box_number=box_name, product=product, exp_year=exp_year) # finish (publish) the pallet bm.pallet_finish(pallet_rec) # validate that worked properly for entry in pallet_box_info: with raises(PalletBox.DoesNotExist): _ = PalletBox.objects.get( pk=pallet_box_info[entry].pallet_box_id) box_rec = Box.objects.get(pk=pallet_box_info[entry].box_id) assert box_rec.box_number == pallet_box_info[entry].box_number assert box_rec.box_type == box_type_rec assert box_rec.location == location_rec assert box_rec.product == pallet_box_info[entry].product assert box_rec.exp_year == pallet_box_info[entry].exp_year assert box_rec.exp_month_start == 0 assert box_rec.exp_month_end == 0 filled_seconds_ago = (now() - box_rec.date_filled).total_seconds() assert filled_seconds_ago < 10 assert box_rec.quantity == box_type_rec.box_type_qty with raises(Pallet.DoesNotExist): _ = Pallet.objects.get(pk=pallet_rec_id) return