test_read_write.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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": "NativeMessageInitialConfiguration", "values": [{"jobsPath": "jobs_path"}]},
  60. InitialConfigurationMessage(jobs_path="jobs_path"),
  61. ),
  62. (
  63. {
  64. "tag": "NativeMessageVisitedJobPage",
  65. "values": [{
  66. "url": "url",
  67. "jobTitle": "job_title",
  68. "pageTitle": "page_title",
  69. "company": "company",
  70. "companyUrl": "company_url",
  71. "companyDomain": "company_domain",
  72. "location": "location",
  73. "hasSimplifiedProcess": True,
  74. "flexibility": "hybrid"
  75. }]
  76. },
  77. VisitedLinkedInJobPageMessage(
  78. url="url",
  79. job_title="job_title",
  80. page_title="page_title",
  81. company="company",
  82. company_url="company_url",
  83. company_domain="company_domain",
  84. location="location",
  85. has_simplified_process=True,
  86. flexibility="hybrid",
  87. ),
  88. ),
  89. ]
  90. )
  91. def input_message(self, request):
  92. (ext_message_as_dict, message) = request.param
  93. encoded = StdReadWriter.encode_message(ext_message_as_dict)
  94. return message, encoded
  95. def test_get_message(self, read_writer, stdin_write, input_message):
  96. expected_message, encoded = input_message
  97. StdReadWriter.send_message_on(stdin_write, encoded)
  98. assert read_writer.get_message() == expected_message
  99. @pytest.fixture(
  100. params=[
  101. (
  102. JobOfferListMessage(job_offers=["job_offer_1", "job_offer_2"]),
  103. {"tag": "job_offer_list", "job_offers": ["job_offer_1", "job_offer_2"]},
  104. ),
  105. (
  106. JobAddedMessage(job="job"),
  107. {"tag": "job_added", "job": "job"},
  108. ),
  109. (
  110. JobAlreadyExistsMessage(job_id="job_id"),
  111. {"tag": "job_already_exists", "job_id": "job_id"},
  112. ),
  113. (
  114. LogMessage.debug(content="debug_content"),
  115. {"tag": "log_message", "level": "debug", "content": "debug_content"},
  116. ),
  117. (
  118. LogMessage.info(content={"message": "info"}),
  119. {"tag": "log_message", "level": "info", "content": {"message": "info"}},
  120. ),
  121. (
  122. LogMessage.error(content="error_content"),
  123. {"tag": "log_message", "level": "error", "content": "error_content"},
  124. ),
  125. ]
  126. )
  127. def output_message(self, request):
  128. return request.param
  129. def test_send_message(self, read_writer, stdout_read, output_message):
  130. original_message, expected_json = output_message
  131. read_writer.send_message(original_message)
  132. assert StdReadWriter.read_message(stdout_read) == expected_json