test_read_write.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import pytest
  2. import tempfile
  3. import os
  4. from io import TextIOWrapper
  5. from dataclasses import asdict
  6. from job_search.read_write import StdReadWriter, ReadWriter
  7. from job_search.messages import (
  8. InitialConfigurationMessage,
  9. VisitedLinkedInJobPageMessage,
  10. JobOfferListMessage,
  11. JobAddedMessage,
  12. JobAlreadyExistsMessage,
  13. LogMessage,
  14. )
  15. def fake_std():
  16. (fd, file_path) = tempfile.mkstemp(prefix="job_search")
  17. yield file_path
  18. os.remove(file_path)
  19. @pytest.fixture
  20. def stdin():
  21. yield from fake_std()
  22. @pytest.fixture
  23. def stdout():
  24. yield from fake_std()
  25. @pytest.fixture
  26. def stdin_read(stdin):
  27. with open(stdin, "rb") as fake:
  28. yield TextIOWrapper(fake)
  29. @pytest.fixture
  30. def stdin_write(stdin):
  31. with open(stdin, "wb") as fake:
  32. yield TextIOWrapper(fake)
  33. @pytest.fixture
  34. def stdout_write(stdout):
  35. with open(stdout, "wb") as fake:
  36. yield TextIOWrapper(fake)
  37. @pytest.fixture
  38. def stdout_read(stdout):
  39. with open(stdout, "rb") as fake:
  40. yield TextIOWrapper(fake)
  41. @pytest.fixture
  42. def read_writer(stdin_read, stdout_write):
  43. return ReadWriter(stdin_read, stdout_write)
  44. class TestStdReadWriter:
  45. def test_get_message(self, stdin_read, stdin_write):
  46. simple_message = {"test": "pouet"}
  47. msg = StdReadWriter.encode_message(simple_message)
  48. StdReadWriter.send_message_on(stdin_write, msg)
  49. assert StdReadWriter.read_message(stdin_read) == simple_message
  50. def test_send(self, stdout_write, stdout_read):
  51. simple_message = {"test": "pouet"}
  52. msg = StdReadWriter.encode_message(simple_message)
  53. StdReadWriter.send_message_on(stdout_write, msg)
  54. assert StdReadWriter.read_message(stdout_read) == simple_message
  55. class TestReadWriter:
  56. @pytest.fixture(
  57. params=[
  58. (
  59. {"tag": "initial_configuration", "jobsPath": "jobs_path"},
  60. InitialConfigurationMessage(jobs_path="jobs_path"),
  61. ),
  62. (
  63. {
  64. "tag": "visited_linkedin_job_page",
  65. "url": "url",
  66. "jobTitle": "job_title",
  67. "pageTitle": "page_title",
  68. "company": "company",
  69. "companyUrl": "company_url",
  70. "companyDomain": "company_domain",
  71. "location": "location",
  72. "hasSimplifiedProcess": True,
  73. "flexibility": "hybrid",
  74. },
  75. VisitedLinkedInJobPageMessage(
  76. url="url",
  77. job_title="job_title",
  78. page_title="page_title",
  79. company="company",
  80. company_url="company_url",
  81. company_domain="company_domain",
  82. location="location",
  83. has_simplified_process=True,
  84. flexibility="hybrid",
  85. ),
  86. ),
  87. ]
  88. )
  89. def input_message(self, request):
  90. (ext_message_as_dict, message) = request.param
  91. encoded = StdReadWriter.encode_message(ext_message_as_dict)
  92. return message, encoded
  93. def test_get_message(self, read_writer, stdin_write, input_message):
  94. expected_message, encoded = input_message
  95. StdReadWriter.send_message_on(stdin_write, encoded)
  96. assert read_writer.get_message() == expected_message
  97. @pytest.fixture(
  98. params=[
  99. (
  100. JobOfferListMessage(job_offers=["job_offer_1", "job_offer_2"]),
  101. {"tag": "job_offer_list", "job_offers": ["job_offer_1", "job_offer_2"]},
  102. ),
  103. (
  104. JobAddedMessage(job="job"),
  105. {"tag": "job_added", "job": "job"},
  106. ),
  107. (
  108. JobAlreadyExistsMessage(job_id="job_id"),
  109. {"tag": "job_already_exists", "job_id": "job_id"},
  110. ),
  111. (
  112. LogMessage.debug(content="debug_content"),
  113. {"tag": "log_message", "level": "debug", "content": "debug_content"},
  114. ),
  115. (
  116. LogMessage.info(content={"message": "info"}),
  117. {"tag": "log_message", "level": "info", "content": {"message": "info"}},
  118. ),
  119. (
  120. LogMessage.error(content="error_content"),
  121. {"tag": "log_message", "level": "error", "content": "error_content"},
  122. ),
  123. ]
  124. )
  125. def output_message(self, request):
  126. return request.param
  127. def test_send_message(self, read_writer, stdout_read, output_message):
  128. original_message, expected_json = output_message
  129. read_writer.send_message(original_message)
  130. assert StdReadWriter.read_message(stdout_read) == expected_json