You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_compat.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. import codecs
  3. import datetime
  4. import io
  5. import os
  6. from tempfile import NamedTemporaryFile
  7. from django.test import TestCase, override_settings
  8. from django.utils.encoding import smart_str
  9. from oscar.core.compat import UnicodeCSVWriter, existing_user_fields
  10. class TestExistingUserFields(TestCase):
  11. def test_order(self):
  12. fields = existing_user_fields(['email', 'first_name', 'last_name'])
  13. self.assertEqual(fields, ['email', 'first_name', 'last_name'])
  14. class TestUnicodeCSVWriter(TestCase):
  15. def test_can_write_different_values(self):
  16. writer = UnicodeCSVWriter(open_file=io.StringIO())
  17. s = 'ünįcodē'
  18. rows = [[s, s, 123, datetime.date.today()], ]
  19. writer.writerows(rows)
  20. self.assertRaises(TypeError, writer.writerows, [object()])
  21. def test_context_manager(self):
  22. tmp_file = NamedTemporaryFile()
  23. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  24. s = 'ünįcodē'
  25. rows = [[s, s, 123, datetime.date.today()], ]
  26. writer.writerows(rows)
  27. def test_csv_write_output(self):
  28. tmp_file = NamedTemporaryFile(delete=False)
  29. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  30. s = 'ünįcodē'
  31. row = [s, 123, 'foo-bar']
  32. writer.writerows([row])
  33. with open(tmp_file.name, 'r') as read_file:
  34. content = smart_str(read_file.read(), encoding='utf-8').strip()
  35. self.assertEqual(content, 'ünįcodē,123,foo-bar')
  36. # Clean up
  37. os.unlink(tmp_file.name)
  38. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  39. def test_bom_write_with_open_file(self):
  40. csv_file = NamedTemporaryFile(delete=False)
  41. with open(csv_file.name, 'w') as open_file:
  42. writer = UnicodeCSVWriter(open_file=open_file, encoding="utf-8")
  43. s = 'ünįcodē'
  44. row = [s, 123, datetime.date.today()]
  45. writer.writerows([row])
  46. with open(csv_file.name, 'rb') as read_file:
  47. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  48. # Clean up
  49. os.unlink(csv_file.name)
  50. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  51. def test_bom_write_with_filename(self):
  52. csv_file = NamedTemporaryFile(delete=False)
  53. with UnicodeCSVWriter(filename=csv_file.name, encoding="utf-8") as writer:
  54. s = 'ünįcodē'
  55. row = [s, 123, datetime.date.today()]
  56. writer.writerows([row])
  57. with open(csv_file.name, 'rb') as read_file:
  58. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  59. # Clean up
  60. os.unlink(csv_file.name)
  61. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  62. def test_bom_not_written_for_other_encodings(self):
  63. csv_file = NamedTemporaryFile(delete=False)
  64. with UnicodeCSVWriter(filename=csv_file.name, encoding="ascii") as writer:
  65. s = 'boring ascii'
  66. row = [s, 123, datetime.date.today()]
  67. writer.writerows([row])
  68. with open(csv_file.name, 'rb') as read_file:
  69. self.assertFalse(read_file.read().startswith(codecs.BOM_UTF8))
  70. # Clean up
  71. os.unlink(csv_file.name)