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))
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_render_address_data_pdf_performance(self): """ ensure we don't run into https://goo.gl/qKRyQx """ start = timezone.now() report = ReportGenerator().generate(company=self.company, report_type='address_data', file_type='PDF') # seed more capital mommy.make('shareholder.Position', buyer=self.company.get_company_shareholder(), seller=None, count=1000000, security=self.sec) # mass shareholder creation for x in range(0, 600): shareholder = mommy.make('shareholder.Shareholder', company=self.company) mommy.make('shareholder.Position', seller=self.company.get_company_shareholder(), buyer=shareholder, count=1, security=self.sec) # logger.warning('finished test data creation: {}'.format( # timezone.now()-start)) render_address_data_pdf( report.company.pk, report.pk, user_id=report.user.pk, ordering=None, notify=True, track_downloads=False) report.refresh_from_db() # logger.warning('finished pdf rendering: {}'.format( # timezone.now()-start)) self.assertIsNotNone(report.file) delta = timezone.now()-start self.assertLess(delta.seconds, 120)
def test_summarize_report(self): """ record some data when report finished rendering """ started_at = timezone.now() report = ReportGenerator().generate() _summarize_report(report, started_at) report.refresh_from_db() self.assertIsNotNone(report.generation_time) self.assertIsNotNone(report.generated_at)
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_add_file_to_report(self): """ attach file to report model """ report = ReportGenerator().generate() _add_file_to_report( u'somefilename.xls', report, 'some file content') report.refresh_from_db() with open(report.file.path) as f: self.assertEqual(f.read(), 'some file content')
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_render_assembly_participation_pdf(self): report = ReportGenerator().generate(company=self.company) PositionGenerator().generate(company=report.company, seller=None) render_assembly_participation_pdf( report.company.pk, report.pk, user_id=report.user.pk, ordering=None, notify=True, track_downloads=False) report.refresh_from_db() self.assertIsNotNone(report.file)
def test_render_captable_xls(self): report = ReportGenerator().generate(file_type='XLS', company=self.company) PositionGenerator().generate(company=report.company, seller=None) render_captable_xls(report.company.pk, report.pk, user_id=report.user.pk, ordering=None, notify=True, track_downloads=False) report.refresh_from_db() self.assertIsNotNone(report.file)
def test_render_captable_pdf(self): report = ReportGenerator().generate(company=self.company) # FIXME way tooooo many queries for 13 shs, just calculate how this # would be for 10.000 shs with self.assertLessNumQueries(533): render_captable_pdf(report.company.pk, report.pk, user_id=report.user.pk, ordering=None, notify=True, track_downloads=False) report.refresh_from_db() self.assertIsNotNone(report.file)
def test_render_vested_shares_pdf(self): report = ReportGenerator().generate(company=self.company, report_type='vested_shares', file_type='PDF') PositionGenerator().generate(company=report.company, seller=None) render_vested_shares_pdf( report.company.pk, report.pk, user_id=report.user.pk, ordering=None, notify=True, track_downloads=False) report.refresh_from_db() self.assertIsNotNone(report.file)
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))
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_prerender_reports(self, mock_xls, mock_pdf, mock_assembly_participation_xls): """ render every night the reports for all corps """ # corps not needing a report = noise CompanyGenerator().generate() ReportGenerator().generate() prerender_reports() company = self.shs[0].company pdf_report = company.report_set.filter(file_type='PDF', report_type='captable').last() xls_ass_report = company.report_set.filter( file_type='XLS', report_type='assembly_participation').last() xls_report = company.report_set.filter(file_type='XLS', report_type='captable').last() self.assertEqual(mock_pdf.call_count, 14) mock_pdf.assert_called_with( args=[company.pk, pdf_report.pk], kwargs={'ordering': pdf_report.order_by}) self.assertEqual(mock_xls.call_count, 14) mock_xls.assert_called_with( args=[company.pk, xls_report.pk], kwargs={'ordering': xls_report.order_by}) self.assertEqual(mock_assembly_participation_xls.call_count, 1) mock_assembly_participation_xls.assert_called_with( args=[company.pk, xls_ass_report.pk], kwargs={'ordering': xls_ass_report.order_by})
def test_get_filename(self): """ render filename for report """ report = ReportGenerator().generate() filename = _get_filename(report, report.company) self.assertEqual(filename.count('_'), 3) self.assertIn(slugify(report.company.name), filename) self.assertTrue(filename.endswith('pdf'))
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))
def setUp(self): self.report = ReportGenerator().generate(file_type='PDF', report_type='captable', eta=timezone.now() + relativedelta(minutes=3))