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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = [
  19. [s, s, 123, datetime.date.today()],
  20. ]
  21. writer.writerows(rows)
  22. self.assertRaises(TypeError, writer.writerows, [object()])
  23. def test_context_manager(self):
  24. tmp_file = NamedTemporaryFile()
  25. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  26. s = "ünįcodē"
  27. rows = [
  28. [s, s, 123, datetime.date.today()],
  29. ]
  30. writer.writerows(rows)
  31. def test_csv_write_output(self):
  32. tmp_file = NamedTemporaryFile(delete=False)
  33. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  34. s = "ünįcodē"
  35. row = [s, 123, "foo-bar"]
  36. writer.writerows([row])
  37. with open(tmp_file.name, "r", encoding="utf-8") as read_file:
  38. content = smart_str(read_file.read(), encoding="utf-8").strip()
  39. self.assertEqual(content, "ünįcodē,123,foo-bar")
  40. # Clean up
  41. os.unlink(tmp_file.name)
  42. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  43. def test_bom_write_with_open_file(self):
  44. csv_file = NamedTemporaryFile(delete=False)
  45. with open(csv_file.name, "w", encoding="utf-8") as open_file:
  46. writer = UnicodeCSVWriter(open_file=open_file, encoding="utf-8")
  47. s = "ünįcodē"
  48. row = [s, 123, datetime.date.today()]
  49. writer.writerows([row])
  50. with open(csv_file.name, "rb") as read_file:
  51. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  52. # Clean up
  53. os.unlink(csv_file.name)
  54. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  55. def test_bom_write_with_filename(self):
  56. csv_file = NamedTemporaryFile(delete=False)
  57. with UnicodeCSVWriter(filename=csv_file.name, encoding="utf-8") as writer:
  58. s = "ünįcodē"
  59. row = [s, 123, datetime.date.today()]
  60. writer.writerows([row])
  61. with open(csv_file.name, "rb") as read_file:
  62. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  63. # Clean up
  64. os.unlink(csv_file.name)
  65. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  66. def test_bom_not_written_for_other_encodings(self):
  67. csv_file = NamedTemporaryFile(delete=False)
  68. with UnicodeCSVWriter(filename=csv_file.name, encoding="ascii") as writer:
  69. s = "boring ascii"
  70. row = [s, 123, datetime.date.today()]
  71. writer.writerows([row])
  72. with open(csv_file.name, "rb") as read_file:
  73. self.assertFalse(read_file.read().startswith(codecs.BOM_UTF8))
  74. # Clean up
  75. os.unlink(csv_file.name)