From 4ae88b1beebe9cc47d1cd58b7b260a16c411d012 Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Tue, 27 Aug 2019 17:31:08 +0500 Subject: [PATCH] BOM-135 fixed all failing tests for the file. --- lms/djangoapps/shoppingcart/reports.py | 7 +++++- .../shoppingcart/tests/test_reports.py | 22 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lms/djangoapps/shoppingcart/reports.py b/lms/djangoapps/shoppingcart/reports.py index 08a2802466..15b30a664e 100644 --- a/lms/djangoapps/shoppingcart/reports.py +++ b/lms/djangoapps/shoppingcart/reports.py @@ -4,8 +4,10 @@ from __future__ import absolute_import from decimal import Decimal +import csv import unicodecsv from django.utils.translation import ugettext as _ +import six from six import text_type from course_modes.models import CourseMode @@ -52,7 +54,10 @@ class Report(object): generates a CSV report of the appropriate type. """ items = self.rows() - writer = unicodecsv.writer(filelike, encoding="utf-8") + if six.PY2: + writer = unicodecsv.writer(filelike, encoding="utf-8") + else: + writer = csv.writer(filelike) writer.writerow(self.header()) for item in items: writer.writerow(item) diff --git a/lms/djangoapps/shoppingcart/tests/test_reports.py b/lms/djangoapps/shoppingcart/tests/test_reports.py index c9ea37f615..0a2be06666 100644 --- a/lms/djangoapps/shoppingcart/tests/test_reports.py +++ b/lms/djangoapps/shoppingcart/tests/test_reports.py @@ -5,12 +5,13 @@ Tests for the Shopping Cart Models from __future__ import absolute_import import datetime -from six import StringIO from textwrap import dedent import pytz from django.conf import settings from mock import patch +import six +from six import StringIO from six import text_type from course_modes.models import CourseMode @@ -112,7 +113,7 @@ class ReportTypeTests(ModuleStoreTestCase): Order Number,Customer Name,Date of Original Transaction,Date of Refund,Amount of Refund,Service Fees (if any) 3,King Bowsér,{time_str},{time_str},40.00,0.00 4,Súsan Smith,{time_str},{time_str},40.00,0.00 - """.format(time_str=str(self.test_time))).encode('utf-8') + """.format(time_str=str(self.test_time))) self.CORRECT_CERT_STATUS_CSV = dedent(""" University,Course,Course Announce Date,Course Start Date,Course Registration Close Date,Course Registration Period,Total Enrolled,Audit Enrollment,Honor Code Enrollment,Verified Enrollment,Gross Revenue,Gross Revenue over the Minimum,Number of Verified Students Contributing More than the Minimum,Number of Refunds,Dollars Refunded @@ -144,7 +145,10 @@ class ReportTypeTests(ModuleStoreTestCase): 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_REFUND_REPORT_CSV.strip()) + self.assertEqual( + csv.replace('\r\n', '\n').strip() if six.PY3 else csv.replace('\r\n', '\n').strip().decode('utf-8'), + self.CORRECT_REFUND_REPORT_CSV.strip() + ) def test_basic_cert_status_csv(self): report = initialize_report("certificate_status", self.now - self.FIVE_MINS, self.now + self.FIVE_MINS, 'A', 'Z') @@ -205,11 +209,11 @@ class ItemizedPurchaseReportTest(ModuleStoreTestCase): cert.refund_requested_time = self.now cert.save() - self.CORRECT_CSV = dedent(b""" + self.CORRECT_CSV = dedent((b""" 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))) + %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) @@ -227,9 +231,11 @@ class ItemizedPurchaseReportTest(ModuleStoreTestCase): 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() + 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())