class ReportSerializerTestCase(TestCase): def setUp(self): self.factory = RequestFactory() self.report = ReportGenerator().generate() url = reverse('reports:download', kwargs={'report_id': self.report.pk}) self.request = self.factory.get(url) self.serializer = ReportSerializer(instance=self.report, context={'request': self.request}) self.serialized_data = self.serializer.data def test_get_url(self): """ render url for download report """ self.report.render() self.report.refresh_from_db() self.assertEqual(self.serializer.get_url(self.report), '/reports/{}/download'.format(self.report.pk)) def test_fields(self): """ do all fields respond properly """ self.report.downloaded_at = timezone.now() self.report.generated_at = timezone.now() self.serializer = ReportSerializer(instance=self.report, context={'request': self.request}) self.serialized_data = self.serializer.data for field in self.serializer.fields.keys(): self.assertIsNotNone(self.serialized_data.get(field))
class BaseReportViewTestCase(TestCase): def setUp(self): self.client = Client() self.report = ReportGenerator().generate() self.report.render() self.operator = OperatorGenerator().generate( user=self.report.user, company=self.report.company) self.other_user = UserGenerator().generate()
def test_send_notify(self): """ send message about finished report """ report = ReportGenerator().generate() report.render() _send_notify(report.user, 'somefilename.pdf', 'test subject', 'test body', 'file desc', report.get_absolute_url()) self.assertEqual(mail.outbox[0].subject, 'test subject') self.assertEqual(mail.outbox[0].to, [report.user.email])
def test_xls_download_number_segments(self): """ rest download of captable xls """ # data company = CompanyGenerator().generate() self.add_subscription(company) secs = TwoInitialSecuritiesGenerator().generate(company=company) security = secs[1] security.track_numbers = True security.save() shareholder_list = [] for x in range(1, 6): # don't statt with 0, Generators 'if' will fail shareholder_list.append(ShareholderGenerator().generate( company=company, number=str(x))) # initial share creation PositionGenerator().generate(buyer=shareholder_list[0], count=1000, value=10, security=security) # single transaction PositionGenerator().generate(buyer=shareholder_list[1], count=10, value=10, security=security, seller=shareholder_list[0]) # shareholder bought and sold PositionGenerator().generate(buyer=shareholder_list[2], count=20, value=20, security=security, seller=shareholder_list[0]) PositionGenerator().generate(buyer=shareholder_list[0], count=20, value=20, security=security, seller=shareholder_list[2]) # login and retest report = ReportGenerator().generate(company=company, file_type='XLS') report.render() user = report.user OperatorGenerator().generate(user=user, company=company) self.client.force_login(user) response = self.client.get( reverse('reports:download', kwargs={"report_id": report.id})) # assert response code self.assertEqual(response.status_code, 200)
def test_captable_xls_download(self): """ rest download of captable xls """ # data company = CompanyGenerator().generate() shareholder_list = [] for x in range(1, 6): # don't statt with 0, Generators 'if' will fail shareholder_list.append(ShareholderGenerator().generate( company=company, number=str(x))) shareholder_list = sorted(shareholder_list, key=lambda t: t.user.last_name.lower()) # initial share creation p = PositionGenerator().generate(seller=None, buyer=shareholder_list[0], count=1000, value=10) security = p.security # single transaction PositionGenerator().generate(buyer=shareholder_list[1], count=10, value=10, seller=shareholder_list[0], security=security) # shareholder bought and sold PositionGenerator().generate(buyer=shareholder_list[2], count=20, value=20, seller=shareholder_list[0], security=security) PositionGenerator().generate(buyer=shareholder_list[0], count=20, value=20, seller=shareholder_list[2], security=security) # login and retest report = ReportGenerator().generate(company=company, file_type='XLS') report.render() user = report.user OperatorGenerator().generate(user=user, company=company) self.add_subscription(company) self.client.force_login(user) response = self.client.get( reverse('reports:download', kwargs={"report_id": report.id})) # assert response code self.assertEqual(response.status_code, 200)
def test_pdf_download_with_number_segments(self): """ test download of captable pdf """ company = CompanyGenerator().generate() self.add_subscription(company) secs = TwoInitialSecuritiesGenerator().generate(company=company) report = ReportGenerator().generate(company=company) report.render() user = report.user OperatorGenerator().generate(user=user, company=company) security = secs[1] security.track_numbers = True security.save() self.client.force_login(user) response = self.client.get( reverse('reports:download', kwargs={"report_id": report.id})) # assert response code self.assertEqual(response.status_code, 200) # assert proper xls content = get_file_content_as_string(response) self.assertTrue(content.startswith('%PDF'))
class ReportModelTestCase(TestCase): def setUp(self): self.report = ReportGenerator().generate(file_type='PDF', report_type='captable', eta=timezone.now() + relativedelta(minutes=3)) def test_get_report_upload_path(self): """ render path for saving prive files for reports """ path = get_report_upload_path(self.report, 'some-report-filename.pdf') self.assertEqual( path, 'private/reports/{}/{}/some-report-filename.pdf' ''.format(self.report.company.pk, self.report.pk)) def test_get_absolute_url(self): """ get path to download report file """ url = self.report.get_absolute_url() self.assertEqual(url, '/reports/{}/download'.format(self.report.pk)) @patch('reports.tasks.render_vested_shares_pdf.apply_async') @patch('reports.tasks.render_captable_pdf.apply_async') @patch('reports.tasks.render_assembly_participation_xls.apply_async') @patch('reports.tasks.render_assembly_participation_pdf.apply_async') def test_render(self, mock_participation_pdf_task, mock_participation_task, mock_task, mock_vested_task): """ trigger rendering of report file """ # FIXME iterate over constant from models self.report.render() self.assertTrue(mock_task.called) self.report.report_type = 'assembly_participation' self.report.file_type = 'XLS' self.report.save() self.report.render() self.assertTrue(mock_participation_task.called) self.report.report_type = 'assembly_participation' self.report.file_type = 'PDF' self.report.save() self.report.render() self.assertTrue(mock_participation_pdf_task.called) self.report.report_type = 'vested_shares' self.report.file_type = 'PDF' self.report.save() self.report.render() self.assertTrue(mock_vested_task.called) def test_update_eta(self): """ calculate and update eta """ self.report.update_eta() self.report.refresh_from_db() self.assertTrue( self.report.eta > timezone.now() + datetime.timedelta(seconds=170)) def test_update_eta_with_unfinished(self): """ calculate and update eta if there are more unfinished reports """ mommy.make('reports.Report', generated_at=None, eta=(timezone.now() + relativedelta(minutes=3)), _quantity=3) self.report.update_eta() self.report.refresh_from_db() self.assertTrue(self.report.eta > timezone.now() + datetime.timedelta(seconds=4 * 180 - 10)) self.assertTrue(self.report.eta < timezone.now() + datetime.timedelta(seconds=5 * 180 + 10)) def test_update_eta_from_prev_report(self): """ calculate and update eta with prev reports existing""" report = ReportGenerator().generate(company=self.report.company, file_type='PDF', report_type='captable') report.generation_time = 60 report.save() self.report.update_eta() self.report.refresh_from_db() self.assertTrue(self.report.eta < timezone.now() + datetime.timedelta(seconds=6 * 70)) self.assertTrue(self.report.eta > timezone.now() + datetime.timedelta(seconds=6 * 50))