test_script_order_confirmation_mail_parser.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import pytest
  2. import dingguo.parser
  3. import email
  4. import os
  5. import shutil
  6. import subprocess
  7. import yaml
  8. project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
  9. script_path = os.path.join(project_root_path, 'scripts', 'order-confirmation-mail-parser')
  10. test_data_path = os.path.join(project_root_path, 'tests', 'data')
  11. def get_mail_path_single_order_platform_a():
  12. return os.path.join(test_data_path, 'amazon', 'mail_1.eml')
  13. def get_mail_path_single_order_platform_b():
  14. return os.path.join(test_data_path, 'banggood', '1.eml')
  15. def get_mail_path_two_orders():
  16. return os.path.join(test_data_path, 'amazon', 'mail_2.eml')
  17. @pytest.mark.parametrize('mail_path', [
  18. get_mail_path_single_order_platform_a(),
  19. get_mail_path_two_orders(),
  20. ])
  21. def test_stdin(mail_path):
  22. process = subprocess.Popen([script_path], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
  23. with open(mail_path, 'r') as mail_file:
  24. script_stdout, script_stderr = process.communicate(input = mail_file.read())
  25. parsed_orders = yaml.load(script_stdout)
  26. with open(mail_path.replace('.eml', '.yml'), 'r') as expected_result_file:
  27. expected_orders = yaml.load(expected_result_file.read())
  28. assert expected_orders == parsed_orders
  29. @pytest.mark.parametrize('mail_paths', [
  30. [
  31. get_mail_path_single_order_platform_a(),
  32. ],
  33. [
  34. get_mail_path_two_orders(),
  35. ],
  36. [
  37. get_mail_path_single_order_platform_a(),
  38. get_mail_path_two_orders(),
  39. ],
  40. ])
  41. def test_path(mail_paths):
  42. parsed_orders = yaml.load(subprocess.check_output([script_path] + mail_paths))
  43. expected_orders = []
  44. for mail_path in mail_paths:
  45. with open(mail_path.replace('.eml', '.yml'), 'r') as expected_result_file:
  46. expected_orders += yaml.load(expected_result_file.read())
  47. assert expected_orders == parsed_orders
  48. @pytest.mark.parametrize('mail_paths', [
  49. [
  50. get_mail_path_single_order_platform_a(),
  51. get_mail_path_single_order_platform_b(),
  52. get_mail_path_two_orders(),
  53. ],
  54. ])
  55. def test_catalogue(tmpdir, mail_paths):
  56. os.chdir(tmpdir.strpath)
  57. for mail_index, mail_path in enumerate(mail_paths):
  58. shutil.copyfile(mail_path, '%d.eml' % mail_index)
  59. assert len(os.listdir('.')) == len(mail_paths)
  60. orders = yaml.load(subprocess.check_output(
  61. [script_path, '--catalogue'] + os.listdir('.'),
  62. ))
  63. assert set(os.listdir('.')) == set([o.platform for o in orders])
  64. for dir_name in os.listdir('.'):
  65. assert set(os.listdir(dir_name)) \
  66. == set([o.order_id for o in orders if o.platform == dir_name])
  67. @pytest.mark.parametrize('mail_paths', [
  68. [
  69. get_mail_path_single_order_platform_a(),
  70. ],
  71. [
  72. get_mail_path_two_orders(),
  73. ],
  74. [
  75. get_mail_path_single_order_platform_a(),
  76. get_mail_path_two_orders(),
  77. ],
  78. ])
  79. def test_register_create(tmpdir, mail_paths):
  80. os.chdir(tmpdir.strpath)
  81. subprocess.check_output(
  82. [script_path, '--register', 'registry.yml'] + mail_paths,
  83. )
  84. with open('registry.yml') as registry_file:
  85. registry = yaml.load(registry_file.read())
  86. orders = []
  87. for mail_path in mail_paths:
  88. with open(mail_path) as mail:
  89. orders += dingguo.parser.parse_order_confirmation_mail(
  90. email.message_from_file(mail)
  91. )
  92. for order in orders:
  93. assert order == registry.registry[order.platform][order.order_id]
  94. for platform in registry.registry:
  95. for order_id in registry.registry[platform]:
  96. assert registry.registry[platform][order_id] in orders
  97. @pytest.mark.parametrize('pre_mail_paths,post_mail_paths', [
  98. (
  99. [get_mail_path_single_order_platform_a()],
  100. [get_mail_path_single_order_platform_b()],
  101. ),
  102. (
  103. [get_mail_path_single_order_platform_a(), get_mail_path_single_order_platform_b()],
  104. [get_mail_path_two_orders()],
  105. ),
  106. ])
  107. def test_register_update(tmpdir, pre_mail_paths, post_mail_paths):
  108. os.chdir(tmpdir.strpath)
  109. subprocess.check_output(
  110. [script_path, '--register', 'registry.yml'] + pre_mail_paths,
  111. )
  112. subprocess.check_output(
  113. [script_path, '--register', 'registry.yml'] + post_mail_paths,
  114. )
  115. with open('registry.yml') as registry_file:
  116. registry = yaml.load(registry_file.read())
  117. orders = []
  118. for mail_path in (pre_mail_paths + post_mail_paths):
  119. with open(mail_path) as mail:
  120. orders += dingguo.parser.parse_order_confirmation_mail(
  121. email.message_from_file(mail)
  122. )
  123. for order in orders:
  124. assert order == registry.registry[order.platform][order.order_id]
  125. for platform in registry.registry:
  126. for order_id in registry.registry[platform]:
  127. assert registry.registry[platform][order_id] in orders