您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

224 行
12 KiB

  1. import unittest
  2. from datetime import date, datetime
  3. from kosmorrolib.data import AsterEphemerides, Planet, MoonPhase, Event
  4. from kosmorrolib.dumper import JsonDumper, TextDumper, _LatexDumper
  5. class DumperTestCase(unittest.TestCase):
  6. def setUp(self) -> None:
  7. self.maxDiff = None
  8. def test_json_dumper_returns_correct_json(self):
  9. self.assertEqual('{\n'
  10. ' "moon_phase": {\n'
  11. ' "next_phase_date": "2019-10-21T00:00:00",\n'
  12. ' "phase": "FULL_MOON",\n'
  13. ' "date": "2019-10-14T00:00:00"\n'
  14. ' },\n'
  15. ' "events": [\n'
  16. ' {\n'
  17. ' "event_type": "OPPOSITION",\n'
  18. ' "objects": [\n'
  19. ' "Mars"\n'
  20. ' ],\n'
  21. ' "start_time": "2019-10-14T23:00:00",\n'
  22. ' "end_time": null,\n'
  23. ' "details": null\n'
  24. ' },\n'
  25. ' {\n'
  26. ' "event_type": "MAXIMAL_ELONGATION",\n'
  27. ' "objects": [\n'
  28. ' "Venus"\n'
  29. ' ],\n'
  30. ' "start_time": "2019-10-14T12:00:00",\n'
  31. ' "end_time": null,\n'
  32. ' "details": "42.0\\u00b0"\n'
  33. ' }\n'
  34. ' ],\n'
  35. ' "ephemerides": [\n'
  36. ' {\n'
  37. ' "object": "Mars",\n'
  38. ' "details": {\n'
  39. ' "rise_time": null,\n'
  40. ' "culmination_time": null,\n'
  41. ' "set_time": null\n'
  42. ' }\n'
  43. ' }\n'
  44. ' ]\n'
  45. '}', JsonDumper(self._get_data(), self._get_events()).to_string())
  46. data = self._get_data(aster_rise_set=True)
  47. self.assertEqual('{\n'
  48. ' "moon_phase": {\n'
  49. ' "next_phase_date": "2019-10-21T00:00:00",\n'
  50. ' "phase": "FULL_MOON",\n'
  51. ' "date": "2019-10-14T00:00:00"\n'
  52. ' },\n'
  53. ' "events": [\n'
  54. ' {\n'
  55. ' "event_type": "OPPOSITION",\n'
  56. ' "objects": [\n'
  57. ' "Mars"\n'
  58. ' ],\n'
  59. ' "start_time": "2019-10-14T23:00:00",\n'
  60. ' "end_time": null,\n'
  61. ' "details": null\n'
  62. ' },\n'
  63. ' {\n'
  64. ' "event_type": "MAXIMAL_ELONGATION",\n'
  65. ' "objects": [\n'
  66. ' "Venus"\n'
  67. ' ],\n'
  68. ' "start_time": "2019-10-14T12:00:00",\n'
  69. ' "end_time": null,\n'
  70. ' "details": "42.0\\u00b0"\n'
  71. ' }\n'
  72. ' ],\n'
  73. ' "ephemerides": [\n'
  74. ' {\n'
  75. ' "object": "Mars",\n'
  76. ' "details": {\n'
  77. ' "rise_time": "2019-10-14T08:00:00",\n'
  78. ' "culmination_time": "2019-10-14T13:00:00",\n'
  79. ' "set_time": "2019-10-14T23:00:00"\n'
  80. ' }\n'
  81. ' }\n'
  82. ' ]\n'
  83. '}', JsonDumper(data,
  84. self._get_events()
  85. ).to_string())
  86. def test_text_dumper_without_events(self):
  87. ephemerides = self._get_data()
  88. self.assertEqual('Monday October 14, 2019\n\n'
  89. 'Object Rise time Culmination time Set time\n'
  90. '-------- ----------- ------------------ ----------\n'
  91. 'Mars - - -\n\n'
  92. 'Moon phase: Full Moon\n'
  93. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  94. 'Note: All the hours are given in UTC.',
  95. TextDumper(ephemerides, [], date=date(2019, 10, 14), with_colors=False).to_string())
  96. ephemerides = self._get_data(aster_rise_set=True)
  97. self.assertEqual('Monday October 14, 2019\n\n'
  98. 'Object Rise time Culmination time Set time\n'
  99. '-------- ----------- ------------------ ----------\n'
  100. 'Mars 08:00 13:00 23:00\n\n'
  101. 'Moon phase: Full Moon\n'
  102. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  103. 'Note: All the hours are given in UTC.',
  104. TextDumper(ephemerides, [], date=date(2019, 10, 14), with_colors=False).to_string())
  105. def test_text_dumper_with_events(self):
  106. ephemerides = self._get_data()
  107. self.assertEqual("Monday October 14, 2019\n\n"
  108. "Object Rise time Culmination time Set time\n"
  109. "-------- ----------- ------------------ ----------\n"
  110. "Mars - - -\n\n"
  111. "Moon phase: Full Moon\n"
  112. "Last Quarter on Monday October 21, 2019 at 00:00\n\n"
  113. "Expected events:\n"
  114. "23:00 Mars is in opposition\n"
  115. "12:00 Venus's largest elongation (42.0°)\n\n"
  116. "Note: All the hours are given in UTC.",
  117. TextDumper(ephemerides, self._get_events(), date=date(2019, 10, 14), with_colors=False).to_string())
  118. def test_text_dumper_without_ephemerides_and_with_events(self):
  119. ephemerides = self._get_data(False)
  120. self.assertEqual('Monday October 14, 2019\n\n'
  121. 'Moon phase: Full Moon\n'
  122. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  123. 'Expected events:\n'
  124. '23:00 Mars is in opposition\n'
  125. "12:00 Venus's largest elongation (42.0°)\n\n"
  126. 'Note: All the hours are given in UTC.',
  127. TextDumper(ephemerides, self._get_events(), date=date(2019, 10, 14), with_colors=False).to_string())
  128. def test_timezone_is_taken_in_account(self):
  129. ephemerides = self._get_data(aster_rise_set=True)
  130. self.assertEqual('Monday October 14, 2019\n\n'
  131. 'Object Rise time Culmination time Set time\n'
  132. '-------- ----------- ------------------ -------------\n'
  133. 'Mars 09:00 14:00 Oct 15, 00:00\n\n'
  134. 'Moon phase: Full Moon\n'
  135. 'Last Quarter on Monday October 21, 2019 at 01:00\n\n'
  136. 'Expected events:\n'
  137. 'Oct 15, 00:00 Mars is in opposition\n'
  138. "13:00 Venus's largest elongation (42.0°)\n\n"
  139. 'Note: All the hours are given in the UTC+1 timezone.',
  140. TextDumper(ephemerides, self._get_events(), date=date(2019, 10, 14), with_colors=False, timezone=1).to_string())
  141. ephemerides = self._get_data(aster_rise_set=True)
  142. self.assertEqual('Monday October 14, 2019\n\n'
  143. 'Object Rise time Culmination time Set time\n'
  144. '-------- ----------- ------------------ ----------\n'
  145. 'Mars 07:00 12:00 22:00\n\n'
  146. 'Moon phase: Full Moon\n'
  147. 'Last Quarter on Sunday October 20, 2019 at 23:00\n\n'
  148. 'Expected events:\n'
  149. '22:00 Mars is in opposition\n'
  150. "11:00 Venus's largest elongation (42.0°)\n\n"
  151. 'Note: All the hours are given in the UTC-1 timezone.',
  152. TextDumper(ephemerides, self._get_events(), date=date(2019, 10, 14), with_colors=False, timezone=-1).to_string())
  153. def test_latex_dumper(self):
  154. latex = _LatexDumper(self._get_data(), self._get_events(), date=date(2019, 10, 14)).to_string()
  155. self.assertRegex(latex, 'Monday October 14, 2019')
  156. self.assertRegex(latex, 'Full Moon')
  157. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  158. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  159. self.assertRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  160. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  161. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  162. latex = _LatexDumper(self._get_data(aster_rise_set=True),
  163. self._get_events(), date=date(2019, 10, 14)).to_string()
  164. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  165. def test_latex_dumper_without_ephemerides(self):
  166. latex = _LatexDumper(self._get_data(False), self._get_events(), date=date(2019, 10, 14)).to_string()
  167. self.assertRegex(latex, 'Monday October 14, 2019')
  168. self.assertRegex(latex, 'Full Moon')
  169. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  170. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  171. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  172. self.assertNotRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  173. self.assertNotRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  174. def test_latex_dumper_without_events(self):
  175. latex = _LatexDumper(self._get_data(), [], date=date(2019, 10, 14)).to_string()
  176. self.assertRegex(latex, 'Monday October 14, 2019')
  177. self.assertRegex(latex, 'Full Moon')
  178. self.assertRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  179. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  180. self.assertNotRegex(latex, r'\\section{\\sffamily Expected events}')
  181. @staticmethod
  182. def _get_data(has_ephemerides: bool = True, aster_rise_set=False):
  183. rise_time = datetime(2019, 10, 14, 8) if aster_rise_set else None
  184. culmination_time = datetime(2019, 10, 14, 13) if aster_rise_set else None
  185. set_time = datetime(2019, 10, 14, 23) if aster_rise_set else None
  186. return {
  187. 'moon_phase': MoonPhase('FULL_MOON', datetime(2019, 10, 14), datetime(2019, 10, 21)),
  188. 'details': [Planet('Mars', 'MARS',
  189. AsterEphemerides(rise_time, culmination_time, set_time))] if has_ephemerides else []
  190. }
  191. @staticmethod
  192. def _get_events():
  193. return [Event('OPPOSITION',
  194. [Planet('Mars', 'MARS')],
  195. datetime(2019, 10, 14, 23, 00)),
  196. Event('MAXIMAL_ELONGATION',
  197. [Planet('Venus', 'VENUS')],
  198. datetime(2019, 10, 14, 12, 00), details='42.0°'),
  199. ]
  200. if __name__ == '__main__':
  201. unittest.main()