25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
3.3 KiB

  1. import unittest
  2. from datetime import date, datetime
  3. from kosmorrolib import events
  4. from kosmorrolib.data import Event, ASTERS
  5. from kosmorrolib.core import get_timescale
  6. from unittest_data_provider import data_provider
  7. from kosmorrolib.exceptions import OutOfRangeDateError
  8. class EventTestCase(unittest.TestCase):
  9. def setUp(self) -> None:
  10. self.maxDiff = None
  11. def test_event_only_accepts_valid_values(self):
  12. with self.assertRaises(ValueError):
  13. Event('SUPERNOVA', None, get_timescale().now())
  14. expected_events_provider = lambda: (
  15. (date(2020, 2, 7), []),
  16. (date(2020, 10, 13), [Event('OPPOSITION', [ASTERS[4]], datetime(2020, 10, 13, 23, 25))]),
  17. (date(2022, 12, 8), [Event('CONJUNCTION', [ASTERS[1], ASTERS[4]], datetime(2022, 12, 8, 4, 18)),
  18. Event('OPPOSITION', [ASTERS[4]], datetime(2022, 12, 8, 5, 41))]),
  19. (date(2025, 1, 16), [Event('OPPOSITION', [ASTERS[4]], datetime(2025, 1, 16, 2, 38))]),
  20. (date(2027, 2, 19), [Event('OPPOSITION', [ASTERS[4]], datetime(2027, 2, 19, 15, 50))]),
  21. (date(2020, 1, 2), [Event('CONJUNCTION', [ASTERS[2], ASTERS[5]], datetime(2020, 1, 2, 16, 41))]),
  22. (date(2020, 1, 12), [Event('CONJUNCTION', [ASTERS[2], ASTERS[6]], datetime(2020, 1, 12, 9, 51)),
  23. Event('CONJUNCTION', [ASTERS[2], ASTERS[9]], datetime(2020, 1, 12, 10, 13)),
  24. Event('CONJUNCTION', [ASTERS[6], ASTERS[9]], datetime(2020, 1, 12, 16, 57))]),
  25. (date(2020, 2, 10), [Event('MAXIMAL_ELONGATION', [ASTERS[2]], datetime(2020, 2, 10, 13, 46), details='18.2°')]),
  26. (date(2020, 3, 24), [Event('MAXIMAL_ELONGATION', [ASTERS[2]], datetime(2020, 3, 24, 1, 56), details='27.8°'),
  27. Event('MAXIMAL_ELONGATION', [ASTERS[3]], datetime(2020, 3, 24, 21, 58), details='46.1°')]),
  28. (date(2005, 6, 16), [Event('OCCULTATION', [ASTERS[1], ASTERS[5]], datetime(2005, 6, 16, 6, 31))])
  29. )
  30. @data_provider(expected_events_provider)
  31. def test_search_events(self, d: date, expected_events: [Event]):
  32. actual_events = events.search_events(d)
  33. self.assertEqual(len(expected_events), len(actual_events),
  34. 'Expected %d elements, got %d for date %s.' % (len(expected_events),
  35. len(actual_events),
  36. d.isoformat()))
  37. for i, expected_event in enumerate(expected_events):
  38. actual_event = actual_events[i]
  39. # Remove unnecessary precision (seconds and microseconds)
  40. actual_event.start_time = datetime(actual_event.start_time.year,
  41. actual_event.start_time.month,
  42. actual_event.start_time.day,
  43. actual_event.start_time.hour,
  44. actual_event.start_time.minute)
  45. self.assertEqual(expected_event.__dict__, actual_event.__dict__)
  46. def test_get_events_raises_exception_on_out_of_date_range(self):
  47. with self.assertRaises(OutOfRangeDateError):
  48. events.search_events(date(1789, 5, 5))
  49. if __name__ == '__main__':
  50. unittest.main()