Ejemplo n.º 1
0
    def form_valid(self, form):
        user = self.request.user
        form.instance.admin = user
        entity = form.save()
        default_coa = form.cleaned_data.get('default_coa')
        activate_accounts = form.cleaned_data.get('activate_all_accounts')
        if default_coa:
            populate_default_coa(entity_model=entity,
                                 activate_accounts=activate_accounts)

        sample_data = form.cleaned_data.get('generate_sample_data')
        if sample_data:
            generate_sample_data(entity_model=entity.slug,
                                 user_model=self.request.user,
                                 start_dt=localtime() - timedelta(days=30 * 6),
                                 days_fw=30 * 9,
                                 tx_quantity=50)
        self.object = entity
        return super().form_valid(form)
Ejemplo n.º 2
0
    def test_entity_views(self):
        """
        Testing the creation of a number of entities.
        """
        # ENTITY-CREATE VIEW...
        entity_create_url = reverse('django_ledger:entity-create')
        response = self.CLIENT.get(entity_create_url, follow=False)

        # making sure user is logged in...
        if response.status_code == 302:
            self.login_client()
            response = self.CLIENT.get(entity_create_url)
            self.assertContains(response, status_code=200, text='New Entity Information')

        # checks that form throws Validation Error if any value is missing...
        entity_must_have_all = ['city', 'state', 'zip_code', 'country']
        for ent_data in self.TEST_DATA:
            while entity_must_have_all:
                ent_copy = ent_data.copy()
                del ent_copy[entity_must_have_all.pop()]
                response = self.CLIENT.post(entity_create_url, data=ent_copy, follow=False)
                self.assertContains(response, status_code=200, text='New Entity Information')
                self.assertFormError(response, form='form', field=None,
                                     errors='Must provide all City/State/Zip/Country')

        # checks that valid url is provided...
        ent_copy = self.get_random_entity_data()
        ent_copy['website'] = ent_copy['website'][1:]
        response = self.CLIENT.post(entity_create_url, data=ent_copy)
        self.assertFormError(response, form='form', field='website', errors='Enter a valid URL.')

        # checks that a valid entity name is provided...
        ent_copy = self.get_random_entity_data()
        ent_copy['name'] = ''
        response = self.CLIENT.post(entity_create_url, data=ent_copy, follow=False)
        self.assertFormError(response, form='form', field='name', errors='Please provide a valid name for new Entity.')

        # checks for valid entity name length....
        ent_copy = self.get_random_entity_data()
        ent_copy['name'] = 'In'
        response = self.CLIENT.post(entity_create_url, data=ent_copy, follow=False)
        self.assertFormError(response, form='form', field='name', errors='Looks like this entity name is too short...')

        # creating a number of entities...
        for ent_data in self.TEST_DATA:
            response = self.CLIENT.post(entity_create_url, data=ent_data, follow=True)
            # user must be redirected if success...
            self.assertContains(response, status_code=200, text='My Entities')
            self.assertContains(response, status_code=200, text=ent_data['name'])

        # ENTITY-LIST VIEW...
        with self.assertNumQueries(3):
            entity_list_url = reverse('django_ledger:entity-list')
            response = self.CLIENT.get(entity_list_url)

            # checks if it was able to render template...
            self.assertContains(response, status_code=200, text='My Entities')

            # checks if all entities where rendered...
            for ent_data in self.TEST_DATA:
                self.assertContains(response, status_code=200, text=ent_data['name'])

            # checks if all entities have proper anchor tags to dashboard and update views...
            entity_qs = response.context['entities']
            for entity_model in entity_qs:
                # checks if entity shows up in the list...
                self.assertContains(response,
                                    status_code=200,
                                    text=entity_model.name,
                                    msg_prefix=f'Entity {entity_model.name} not in the view!')

                # checks if there is a button with a link to the dashboard...
                self.assertContains(response,
                                    status_code=200,
                                    text=reverse('django_ledger:entity-dashboard',
                                                 kwargs={
                                                     'entity_slug': entity_model.slug
                                                 }))
                # checks if there is a button with a link to the update view...
                self.assertContains(response,
                                    status_code=200,
                                    text=reverse('django_ledger:entity-update',
                                                 kwargs={
                                                     'entity_slug': entity_model.slug
                                                 }))

        # ENTITY-UPDATE VIEW...
        with self.assertNumQueries(3):
            entity_model = entity_qs.first()
            entity_update_url = reverse('django_ledger:entity-update',
                                        kwargs={
                                            'entity_slug': entity_model.slug
                                        })
            response = self.CLIENT.get(entity_update_url)

        with self.assertNumQueries(5):
            # idea: DJL-123 - Chart of Accounts OneToOne Field on CoA Model?
            ent_data = response.context['form'].initial
            ent_data['name'] = 'New Cool Name LLC'
            ent_data = {k: v for k, v in ent_data.items() if v}
            response = self.CLIENT.post(entity_update_url, data=ent_data)

        with self.assertNumQueries(3):
            # redirects to entity list
            self.assertRedirects(response, expected_url=entity_list_url)

        with self.assertNumQueries(3):
            response = self.CLIENT.get(entity_list_url)
            # checks if updated entity is in list...
            self.assertContains(response, status_code=200, text=ent_data['name'])

        # ENTITY-DETAIL VIEW...
        entity_model = entity_model.get_previous_by_created()
        logger.warning(f'Populating CoA for {entity_model.name}...')
        # populates accounts with DJL default CoA.
        populate_default_coa(
            entity_model=entity_model,
            activate_accounts=True
        )

        logger.warning(f'Generating sample data for {entity_model.name}...')
        # generates sample data to perform tests.
        generate_sample_data(
            entity_model=entity_model,
            user_model=self.user_model,
            start_dt=self.START_DATE,
            days_fw=self.DAYS_FWD,
            tx_quantity=int(self.DAYS_FWD * 0.5)
        )

        with self.assertNumQueries(2):
            # this will redirect to entity-detail-month...
            entity_detail_url = reverse('django_ledger:entity-dashboard',
                                        kwargs={
                                            'entity_slug': entity_model.slug
                                        })
            response = self.CLIENT.get(entity_detail_url)

        with self.assertNumQueries(9):
            local_dt = localdate()
            entity_month_detail_url = reverse('django_ledger:entity-dashboard-month',
                                              kwargs={
                                                  'entity_slug': entity_model.slug,
                                                  'year': local_dt.year,
                                                  'month': local_dt.month
                                              })
            self.assertRedirects(response, entity_month_detail_url)

        with self.assertNumQueries(6):
            # same as before, but this time the session must not be update because user has not suited entities...
            response = self.CLIENT.get(entity_month_detail_url)
            self.assertContains(response, text=entity_model.name)
            self.assertTrue(response.context['bills'].count() >= 0)
            self.assertTrue(response.context['invoices'].count() >= 0)

        # ENTITY-DELETE VIEW...
        with self.assertNumQueries(3):
            entity_delete_url = reverse('django_ledger:entity-delete',
                                        kwargs={
                                            'entity_slug': entity_model.slug
                                        })
            response = self.CLIENT.get(entity_delete_url)
            self.assertContains(response,
                                status_code=200,
                                text=entity_model.name)
            self.assertContains(response,
                                status_code=200,
                                text=f'Are you sure you want to delete')
            self.assertContains(response,
                                status_code=200,
                                text=entity_delete_url)

        # this is a complex operation that requires several queries...
        response = self.CLIENT.post(entity_delete_url,
                                    data={
                                        'slug': entity_model.slug
                                    })
        with self.assertNumQueries(3):
            # checks that user is redirected to home after entity is deleted...
            home_url = reverse('django_ledger:home')
            self.assertRedirects(response, home_url)

        with self.assertNumQueries(3):
            # checks that entity no longer shows in entity list...
            response = self.CLIENT.get(entity_list_url)
            self.assertNotContains(response, text=entity_model.name)