def test_send_wrong_data_in_bulkedit_form(self):
        url = '/assets/dc/bulkedit/?select=%s&select=%s&select=%s' % (
            self.first_asset.id,
            self.second_asset.id,
            self.asset_with_duplicated_sn.id,
        )
        post_data = get_bulk_edit_post_data(
            {
                'invoice_date': 'wrong_field_data',
                'sn': self.asset_with_duplicated_sn.sn,
            },
            {
                'invoice_date': '',
                'model': '',
                'status': '',
                'source': '',
            },
            {
                'invoice_no': '',
            }
        )

        send_post_with_empty_fields = self.client.post(url, post_data)

        # Try to send post with empty field send_post should be false
        try:
            self.assertRedirects(
                send_post_with_empty_fields,
                url,
                status_code=302,
                target_status_code=200,
            )
            send_post = True
        except AssertionError:
            send_post = False
        # If not here is error msg
        self.assertFalse(send_post, 'Empty fields was send!')

        # Find what was wrong
        bulk_data = [
            dict(
                row=0, field='invoice_date', error='Enter a valid date.',
            ),
            dict(
                row=0, field='sn', error='Asset with this Sn already exists.',
            ),
            dict(
                row=1,
                field='invoice_date',
                error='Invoice date cannot be empty.',
            ),
            dict(
                row=1, field='model', error='This field is required.',
            ),
            dict(
                row=2,
                field='invoice_no',
                error='Invoice number cannot be empty.',
            )
        ]
        for bulk in bulk_data:
            formset = send_post_with_empty_fields.context_data['formset']
            self.assertEqual(
                formset[bulk['row']]._errors[bulk['field']][0],
                bulk['error']
            )

        # if sn was duplicated, the message should be shown on the screen
        msg = SCREEN_ERROR_MESSAGES['duplicated_sn_or_bc']
        self.assertTrue(msg in send_post_with_empty_fields.content)
Example #2
0
    def test_edit_via_bulkedit_form(self):
        url = '/assets/dc/bulkedit/?select=%s&select=%s' % (
            self.asset.id, self.asset1.id)
        content = self.client.get(url)
        self.assertEqual(content.status_code, 200)

        post_data = get_bulk_edit_post_data({
            'model': self.model.id,
            'invoice_no': 'Invoice No1',
            'order_no': 'Order No1',
            'invoice_date': '2012-02-02',
            'status': AssetStatus.in_progress.id,
            'sn': '3333-3333-3333-3333',
            'barcode': 'bc-3333-3333-3333-3333',
        }, {
            'model': self.model1.id,
            'invoice_no': 'Invoice No2',
            'order_no': 'Order No2',
            'invoice_date': '2011-02-03',
            'status': AssetStatus.waiting_for_release.id,
            'sn': '4444-4444-4444-4444',
            'barcode': 'bc-4444-4444-4444-4444',
        })

        response = self.client.post(url, post_data, follow=True)

        # Find success message
        self.assertTrue('Changes saved.' in response.content)

        # if everything is ok, server return response code = 302, and
        # redirect us to /assets/dc/search given response code 200
        self.assertRedirects(
            response,
            url,
            status_code=302,
            target_status_code=200,
        )

        # Simulate reopening bulkedit form to check if data were written
        new_view = self.client.get(url)
        fields = new_view.context['formset'].queryset

        correct_data = [
            dict(
                model=unicode(self.model),
                invoice_no='Invoice No1',
                order_no='Order No1',
                invoice_date='2012-02-02',
                status=AssetStatus.in_progress.id,
                sn='3333-3333-3333-3333',
                barcode='bc-3333-3333-3333-3333',
            ),
            dict(
                model=unicode(self.model1),
                invoice_no='Invoice No2',
                order_no='Order No2',
                invoice_date='2011-02-03',
                status=AssetStatus.waiting_for_release.id,
                sn='4444-4444-4444-4444',
                barcode='bc-4444-4444-4444-4444',
            )
        ]
        for counter, data in enumerate(correct_data):
            for key in data.keys():
                self.assertEqual(
                    unicode(getattr(fields[counter], key)), unicode(data[key]),
                    'returned {} expected {} for field {}'.format(
                        getattr(fields[counter], key), data[key], key
                    )
                )
    def test_send_wrong_data_in_bulkedit_form(self):
        url = '/assets/dc/bulkedit/?select=%s&select=%s&select=%s' % (
            self.first_asset.id,
            self.second_asset.id,
            self.asset_with_duplicated_sn.id,
        )
        post_data = get_bulk_edit_post_data(
            {
                'invoice_date': 'wrong_field_data',
                'sn': self.asset_with_duplicated_sn.sn,
            }, {
                'invoice_date': '',
                'model': '',
                'status': '',
                'source': '',
            }, {
                'invoice_no': '',
            })

        send_post_with_empty_fields = self.client.post(url, post_data)

        # Try to send post with empty field send_post should be false
        try:
            self.assertRedirects(
                send_post_with_empty_fields,
                url,
                status_code=302,
                target_status_code=200,
            )
            send_post = True
        except AssertionError:
            send_post = False
        # If not here is error msg
        self.assertFalse(send_post, 'Empty fields was send!')

        # Find what was wrong
        bulk_data = [
            dict(
                row=0,
                field='invoice_date',
                error='Enter a valid date.',
            ),
            dict(
                row=0,
                field='sn',
                error='Asset with this Sn already exists.',
            ),
            dict(
                row=1,
                field='invoice_date',
                error='Invoice date cannot be empty.',
            ),
            dict(
                row=1,
                field='model',
                error='This field is required.',
            ),
            dict(
                row=2,
                field='invoice_no',
                error='Invoice number cannot be empty.',
            )
        ]
        for bulk in bulk_data:
            formset = send_post_with_empty_fields.context_data['formset']
            self.assertEqual(formset[bulk['row']]._errors[bulk['field']][0],
                             bulk['error'])

        # if sn was duplicated, the message should be shown on the screen
        msg = SCREEN_ERROR_MESSAGES['duplicated_sn_or_bc']
        self.assertTrue(msg in send_post_with_empty_fields.content)