test_storage.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import pytest
  2. from datetime import date, datetime
  3. from email.utils import parsedate_to_datetime
  4. from job_search.job_storage import (
  5. JobStorage,
  6. JobOfferOrigin,
  7. ApplicationProcess,
  8. CompanyKind,
  9. ContractType,
  10. Flexibility,
  11. )
  12. from job_search.read_write import EnhancedJSONEncoder
  13. import json
  14. @pytest.fixture(params=[JobStorage])
  15. def job_storage(request, tmp_path):
  16. return request.param(base_dir=tmp_path)
  17. @pytest.fixture(
  18. params=[
  19. dict(
  20. id="linked_in_3755217595",
  21. url="https://www.linkedin.com/jobs/view/3755217595",
  22. title="Job title",
  23. company="Company",
  24. origin=JobOfferOrigin.LINKED_IN,
  25. application_process=ApplicationProcess.REGULAR,
  26. location="location",
  27. company_domain="domain",
  28. company_url="https://www.linkedin.com/company/the-company/life",
  29. publication_date=date.today(),
  30. ),
  31. dict(
  32. id="linked_in_3755217595",
  33. url="https://www.linkedin.com/jobs/view/3755217595",
  34. title="Job title",
  35. company="Company",
  36. origin=JobOfferOrigin.LINKED_IN,
  37. application_process=ApplicationProcess.REGULAR,
  38. location="location",
  39. company_domain="domain",
  40. company_url="https://www.linkedin.com/company/the-company/life",
  41. publication_date=date.today(),
  42. skills=["skill1", "skill2"],
  43. tags=["tag1", "tag2"],
  44. xp_required=2,
  45. company_kind=CompanyKind.REGULAR,
  46. comment="comment",
  47. description="description",
  48. contract_type=ContractType.CDD,
  49. flexibility=Flexibility.HYBRID,
  50. alternate_url="https://www.anothersite.com/with/the/offer.html",
  51. application_date=date.today(),
  52. application_rejection_date=date.today(),
  53. ),
  54. dict(
  55. id="linked_in_3755217595",
  56. url="https://www.linkedin.com/jobs/view/3755217595",
  57. title="Job title",
  58. company="Company",
  59. origin=JobOfferOrigin.LINKED_IN,
  60. application_process=ApplicationProcess.REGULAR,
  61. location="location",
  62. company_domain="domain",
  63. company_url="https://www.linkedin.com/company/the-company/life",
  64. comment="""
  65. multi
  66. line
  67. comment
  68. """,
  69. publication_date=date.today(),
  70. ),
  71. ]
  72. )
  73. def linked_in_job_offer(request):
  74. return request.param
  75. class TestJobStorage:
  76. def test_job_storage_empty_on_startup(self, job_storage):
  77. assert job_storage.read_all() == {}
  78. def test_job_addition(self, job_storage, linked_in_job_offer):
  79. job_storage.insert_record("job_offer", linked_in_job_offer)
  80. all_items = job_storage.read_all().items()
  81. assert len(all_items) == 1
  82. [(id, stored_job)] = all_items
  83. # Check that a "first seen date" field has been set, that reads as a datetime
  84. assert isinstance(parsedate_to_datetime(stored_job["first_seen_date"]), datetime)
  85. assert id == stored_job["id"]
  86. # Job offers should look as if encoded in JSON and then decoded ...
  87. expected_read = json.loads(json.dumps(linked_in_job_offer, cls=EnhancedJSONEncoder))
  88. # ... with some fields added
  89. expected_read["skills"] = expected_read["skills"] if "skills" in expected_read else []
  90. expected_read["tags"] = expected_read["tags"] if "tags" in expected_read else []
  91. # ... and the integers won't be properly readback
  92. if "xp_required" in expected_read:
  93. expected_read["xp_required"] = str(expected_read["xp_required"])
  94. # Remove automatically-added first_seen_date for comparison with the non-stored version
  95. del stored_job["first_seen_date"]
  96. assert stored_job == expected_read
  97. def test_job_duplicate_addition(self, job_storage, linked_in_job_offer):
  98. job_storage.insert_record("job_offer", linked_in_job_offer)
  99. with pytest.raises(FileExistsError) as excinfo:
  100. job_storage.insert_record("job_offer", linked_in_job_offer)
  101. assert linked_in_job_offer["id"] in str(excinfo.value)