class ItemizedPurchaseReportTest(ModuleStoreTestCase): """ Tests for the models used to generate itemized purchase reports """ FIVE_MINS = datetime.timedelta(minutes=5) TEST_ANNOTATION = u'Ba\xfc\u5305' def setUp(self): super(ItemizedPurchaseReportTest, self).setUp() self.user = UserFactory.create() self.cost = 40 self.course = CourseFactory.create(org='MITx', number='999', display_name=u'Robot Super Course') self.course_key = self.course.id course_mode = CourseMode(course_id=self.course_key, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode.save() course_mode2 = CourseMode(course_id=self.course_key, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation(course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.annotation.save() self.course_reg_code_annotation = CourseRegCodeItemAnnotation(course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.course_reg_code_annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order(self.cart, self.course_key, mode_slug=course_mode.mode_slug) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_key, self.cost, 'verified') self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC) paid_reg = PaidCourseRegistration.objects.get(course_id=self.course_key, user=self.user) paid_reg.fulfilled_time = self.now paid_reg.refund_requested_time = self.now paid_reg.save() cert = CertificateItem.objects.get(course_id=self.course_key, user=self.user) cert.fulfilled_time = self.now cert.refund_requested_time = self.now cert.save() self.CORRECT_CSV = dedent(""" Purchase Time,Order ID,Status,Quantity,Unit Cost,Total Cost,Currency,Description,Comments {time_str},1,purchased,1,40.00,40.00,usd,Registration for Course: Robot Super Course,Ba\xc3\xbc\xe5\x8c\x85 {time_str},1,purchased,1,40.00,40.00,usd,verified cert for course Robot Super Course, """.format(time_str=str(self.now))) def test_purchased_items_btw_dates(self): report = initialize_report("itemized_purchase_report", self.now - self.FIVE_MINS, self.now + self.FIVE_MINS) purchases = report.rows() # since there's not many purchases, just run through the generator to make sure we've got the right number self.assertEqual(len(list(purchases)), 2) report = initialize_report("itemized_purchase_report", self.now + self.FIVE_MINS, self.now + self.FIVE_MINS + self.FIVE_MINS) no_purchases = report.rows() self.assertEqual(len(list(no_purchases)), 0) def test_purchased_csv(self): """ Tests that a generated purchase report CSV is as we expect """ report = initialize_report("itemized_purchase_report", self.now - self.FIVE_MINS, self.now + self.FIVE_MINS) csv_file = StringIO.StringIO() report.write_csv(csv_file) csv = csv_file.getvalue() csv_file.close() # Using excel mode csv, which automatically ends lines with \r\n, so need to convert to \n self.assertEqual(csv.replace('\r\n', '\n').strip(), self.CORRECT_CSV.strip()) def test_csv_report_no_annotation(self): """ Fill in gap in test coverage. csv_report_comments for PaidCourseRegistration instance with no matching annotation """ # delete the matching annotation self.annotation.delete() self.assertEqual(u"", self.reg.csv_report_comments) def test_paidcourseregistrationannotation_unicode(self): """ Fill in gap in test coverage. __unicode__ method of PaidCourseRegistrationAnnotation """ self.assertEqual(unicode(self.annotation), u'{} : {}'.format(self.course_key.to_deprecated_string(), self.TEST_ANNOTATION)) def test_courseregcodeitemannotationannotation_unicode(self): """ Fill in gap in test coverage. __unicode__ method of CourseRegCodeItemAnnotation """ self.assertEqual(unicode(self.course_reg_code_annotation), u'{} : {}'.format(self.course_key.to_deprecated_string(), self.TEST_ANNOTATION))
class ItemizedPurchaseReportTest(ModuleStoreTestCase): """ Tests for the models used to generate itemized purchase reports """ FIVE_MINS = datetime.timedelta(minutes=5) TEST_ANNOTATION = u'Ba\xfc\u5305' def setUp(self): super(ItemizedPurchaseReportTest, self).setUp() self.user = UserFactory.create() self.cost = 40 self.course = CourseFactory.create(org='MITx', number='999', display_name=u'Robot Super Course') self.course_key = self.course.id course_mode = CourseMode(course_id=self.course_key, mode_slug="honor", mode_display_name="honor cert", min_price=self.cost) course_mode.save() course_mode2 = CourseMode(course_id=self.course_key, mode_slug="verified", mode_display_name="verified cert", min_price=self.cost) course_mode2.save() self.annotation = PaidCourseRegistrationAnnotation( course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.annotation.save() self.course_reg_code_annotation = CourseRegCodeItemAnnotation( course_id=self.course_key, annotation=self.TEST_ANNOTATION) self.course_reg_code_annotation.save() self.cart = Order.get_cart_for_user(self.user) self.reg = PaidCourseRegistration.add_to_order( self.cart, self.course_key, mode_slug=course_mode.mode_slug) self.cert_item = CertificateItem.add_to_order(self.cart, self.course_key, self.cost, 'verified') self.cart.purchase() self.now = datetime.datetime.now(pytz.UTC) paid_reg = PaidCourseRegistration.objects.get( course_id=self.course_key, user=self.user) paid_reg.fulfilled_time = self.now paid_reg.refund_requested_time = self.now paid_reg.save() cert = CertificateItem.objects.get(course_id=self.course_key, user=self.user) cert.fulfilled_time = self.now cert.refund_requested_time = self.now cert.save() self.CORRECT_CSV = dedent( (b""" Purchase Time,Order ID,Status,Quantity,Unit Cost,Total Cost,Currency,Description,Comments %s,1,purchased,1,40.00,40.00,usd,Registration for Course: Robot Super Course,Ba\xc3\xbc\xe5\x8c\x85 %s,1,purchased,1,40.00,40.00,usd,verified cert for course Robot Super Course, """ % (six.b(str(self.now)), six.b(str(self.now)))).decode('utf-8')) def test_purchased_items_btw_dates(self): report = initialize_report("itemized_purchase_report", self.now - self.FIVE_MINS, self.now + self.FIVE_MINS) purchases = report.rows() # since there's not many purchases, just run through the generator to make sure we've got the right number self.assertEqual(len(list(purchases)), 2) report = initialize_report("itemized_purchase_report", self.now + self.FIVE_MINS, self.now + self.FIVE_MINS + self.FIVE_MINS) no_purchases = report.rows() self.assertEqual(len(list(no_purchases)), 0) def test_purchased_csv(self): """ Tests that a generated purchase report CSV is as we expect """ report = initialize_report("itemized_purchase_report", self.now - self.FIVE_MINS, self.now + self.FIVE_MINS) # Note :In this we are using six.StringIO as memory buffer to read/write csv for testing. # In case of py2 that will be BytesIO so we will need to decode the value before comparison. csv_file = StringIO() report.write_csv(csv_file) csv = csv_file.getvalue() if six.PY3 else csv_file.getvalue().decode( 'utf-8') csv_file.close() # Using excel mode csv, which automatically ends lines with \r\n, so need to convert to \n self.assertEqual( csv.replace('\r\n', '\n').strip(), self.CORRECT_CSV.strip()) def test_csv_report_no_annotation(self): """ Fill in gap in test coverage. csv_report_comments for PaidCourseRegistration instance with no matching annotation """ # delete the matching annotation self.annotation.delete() self.assertEqual("", self.reg.csv_report_comments) def test_paidcourseregistrationannotation_unicode(self): """ Fill in gap in test coverage. __str__ method of PaidCourseRegistrationAnnotation """ self.assertEqual( text_type(self.annotation), u'{} : {}'.format(text_type(self.course_key), self.TEST_ANNOTATION)) def test_courseregcodeitemannotationannotation_unicode(self): """ Fill in gap in test coverage. __str__ method of CourseRegCodeItemAnnotation """ self.assertEqual( text_type(self.course_reg_code_annotation), u'{} : {}'.format(text_type(self.course_key), self.TEST_ANNOTATION))