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

328 行
18 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. ' "ephemerides": [\n'
  11. ' {\n'
  12. ' "object": {\n'
  13. ' "name": "Mars",\n'
  14. ' "type": "planet",\n'
  15. ' "radius": null\n'
  16. ' },\n'
  17. ' "rise_time": null,\n'
  18. ' "culmination_time": null,\n'
  19. ' "set_time": null\n'
  20. ' }\n'
  21. ' ],\n'
  22. ' "moon_phase": {\n'
  23. ' "phase": "FULL_MOON",\n'
  24. ' "time": "2019-10-14T00:00:00",\n'
  25. ' "next": {\n'
  26. ' "phase": "LAST_QUARTER",\n'
  27. ' "time": "2019-10-21T00:00:00"\n'
  28. ' }\n'
  29. ' },\n'
  30. ' "events": [\n'
  31. ' {\n'
  32. ' "objects": [\n'
  33. ' {\n'
  34. ' "name": "Mars",\n'
  35. ' "type": "planet",\n'
  36. ' "radius": null\n'
  37. ' }\n'
  38. ' ],\n'
  39. ' "event": "OPPOSITION",\n'
  40. ' "starts_at": "2019-10-14T23:00:00",\n'
  41. ' "ends_at": null,\n'
  42. ' "details": null\n'
  43. ' },\n'
  44. ' {\n'
  45. ' "objects": [\n'
  46. ' {\n'
  47. ' "name": "Venus",\n'
  48. ' "type": "planet",\n'
  49. ' "radius": null\n'
  50. ' }\n'
  51. ' ],\n'
  52. ' "event": "MAXIMAL_ELONGATION",\n'
  53. ' "starts_at": "2019-10-14T12:00:00",\n'
  54. ' "ends_at": null,\n'
  55. ' "details": "42.0\\u00b0"\n'
  56. ' }\n'
  57. ' ]\n'
  58. '}', JsonDumper(self._get_ephemerides(), self._get_moon_phase(), self._get_events()).to_string())
  59. self.assertEqual('{\n'
  60. ' "ephemerides": [\n'
  61. ' {\n'
  62. ' "object": {\n'
  63. ' "name": "Mars",\n'
  64. ' "type": "planet",\n'
  65. ' "radius": null\n'
  66. ' },\n'
  67. ' "rise_time": "2019-10-14T08:00:00",\n'
  68. ' "culmination_time": "2019-10-14T13:00:00",\n'
  69. ' "set_time": "2019-10-14T23:00:00"\n'
  70. ' }\n'
  71. ' ],\n'
  72. ' "moon_phase": {\n'
  73. ' "phase": "FULL_MOON",\n'
  74. ' "time": "2019-10-14T00:00:00",\n'
  75. ' "next": {\n'
  76. ' "phase": "LAST_QUARTER",\n'
  77. ' "time": "2019-10-21T00:00:00"\n'
  78. ' }\n'
  79. ' },\n'
  80. ' "events": [\n'
  81. ' {\n'
  82. ' "objects": [\n'
  83. ' {\n'
  84. ' "name": "Mars",\n'
  85. ' "type": "planet",\n'
  86. ' "radius": null\n'
  87. ' }\n'
  88. ' ],\n'
  89. ' "event": "OPPOSITION",\n'
  90. ' "starts_at": "2019-10-14T23:00:00",\n'
  91. ' "ends_at": null,\n'
  92. ' "details": null\n'
  93. ' },\n'
  94. ' {\n'
  95. ' "objects": [\n'
  96. ' {\n'
  97. ' "name": "Venus",\n'
  98. ' "type": "planet",\n'
  99. ' "radius": null\n'
  100. ' }\n'
  101. ' ],\n'
  102. ' "event": "MAXIMAL_ELONGATION",\n'
  103. ' "starts_at": "2019-10-14T12:00:00",\n'
  104. ' "ends_at": null,\n'
  105. ' "details": "42.0\\u00b0"\n'
  106. ' }\n'
  107. ' ]\n'
  108. '}', JsonDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  109. self._get_events()).to_string())
  110. def test_text_dumper_without_events(self):
  111. ephemerides = self._get_ephemerides()
  112. self.assertEqual('Monday October 14, 2019\n\n'
  113. 'Object Rise time Culmination time Set time\n'
  114. '-------- ----------- ------------------ ----------\n'
  115. 'Mars - - -\n\n'
  116. 'Moon phase: Full Moon\n'
  117. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  118. 'Note: All the hours are given in UTC.',
  119. TextDumper(ephemerides, self._get_moon_phase(), [], date=date(2019, 10, 14), with_colors=False).to_string())
  120. ephemerides = self._get_ephemerides(aster_rise_set=True)
  121. self.assertEqual('Monday October 14, 2019\n\n'
  122. 'Object Rise time Culmination time Set time\n'
  123. '-------- ----------- ------------------ ----------\n'
  124. 'Mars 08:00 13:00 23:00\n\n'
  125. 'Moon phase: Full Moon\n'
  126. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  127. 'Note: All the hours are given in UTC.',
  128. TextDumper(ephemerides, self._get_moon_phase(), [], date=date(2019, 10, 14), with_colors=False).to_string())
  129. def test_text_dumper_with_events(self):
  130. ephemerides = self._get_ephemerides()
  131. self.assertEqual("Monday October 14, 2019\n\n"
  132. "Object Rise time Culmination time Set time\n"
  133. "-------- ----------- ------------------ ----------\n"
  134. "Mars - - -\n\n"
  135. "Moon phase: Full Moon\n"
  136. "Last Quarter on Monday October 21, 2019 at 00:00\n\n"
  137. "Expected events:\n"
  138. "23:00 Mars is in opposition\n"
  139. "12:00 Venus's largest elongation (42.0°)\n\n"
  140. "Note: All the hours are given in UTC.",
  141. TextDumper(ephemerides, self._get_moon_phase(), self._get_events(), date=date(2019, 10, 14), with_colors=False).to_string())
  142. def test_text_dumper_without_ephemerides_and_with_events(self):
  143. self.assertEqual('Monday October 14, 2019\n\n'
  144. 'Moon phase: Full Moon\n'
  145. 'Last Quarter on Monday October 21, 2019 at 00:00\n\n'
  146. 'Expected events:\n'
  147. '23:00 Mars is in opposition\n'
  148. "12:00 Venus's largest elongation (42.0°)\n\n"
  149. 'Note: All the hours are given in UTC.',
  150. TextDumper(None, self._get_moon_phase(), self._get_events(),
  151. date=date(2019, 10, 14), with_colors=False).to_string())
  152. def test_timezone_is_taken_in_account(self):
  153. ephemerides = self._get_ephemerides(aster_rise_set=True)
  154. self.assertEqual('Monday October 14, 2019\n\n'
  155. 'Object Rise time Culmination time Set time\n'
  156. '-------- ----------- ------------------ -------------\n'
  157. 'Mars 09:00 14:00 Oct 15, 00:00\n\n'
  158. 'Moon phase: Full Moon\n'
  159. 'Last Quarter on Monday October 21, 2019 at 01:00\n\n'
  160. 'Expected events:\n'
  161. 'Oct 15, 00:00 Mars is in opposition\n'
  162. "13:00 Venus's largest elongation (42.0°)\n\n"
  163. 'Note: All the hours are given in the UTC+1 timezone.',
  164. TextDumper(ephemerides, self._get_moon_phase(), self._get_events(), date=date(2019, 10, 14),
  165. with_colors=False, timezone=1).to_string())
  166. ephemerides = self._get_ephemerides(aster_rise_set=True)
  167. self.assertEqual('Monday October 14, 2019\n\n'
  168. 'Object Rise time Culmination time Set time\n'
  169. '-------- ----------- ------------------ ----------\n'
  170. 'Mars 07:00 12:00 22:00\n\n'
  171. 'Moon phase: Full Moon\n'
  172. 'Last Quarter on Sunday October 20, 2019 at 23:00\n\n'
  173. 'Expected events:\n'
  174. '22:00 Mars is in opposition\n'
  175. "11:00 Venus's largest elongation (42.0°)\n\n"
  176. 'Note: All the hours are given in the UTC-1 timezone.',
  177. TextDumper(ephemerides, self._get_moon_phase(), self._get_events(), date=date(2019, 10, 14),
  178. with_colors=False, timezone=-1).to_string())
  179. def test_latex_dumper(self):
  180. latex = _LatexDumper(self._get_ephemerides(), self._get_moon_phase(), self._get_events(),
  181. date=date(2019, 10, 14)).to_string()
  182. self.assertRegex(latex, 'Monday October 14, 2019')
  183. self.assertRegex(latex, 'Full Moon')
  184. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  185. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  186. self.assertRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  187. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  188. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  189. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  190. self._get_events(), date=date(2019, 10, 14)).to_string()
  191. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  192. def test_latex_dumper_without_ephemerides(self):
  193. latex = _LatexDumper(None, self._get_moon_phase(), self._get_events(),
  194. date=date(2019, 10, 14)).to_string()
  195. self.assertRegex(latex, 'Monday October 14, 2019')
  196. self.assertRegex(latex, 'Full Moon')
  197. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  198. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  199. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  200. self.assertNotRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  201. self.assertNotRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  202. def test_latex_dumper_without_events(self):
  203. latex = _LatexDumper(self._get_ephemerides(), self._get_moon_phase(), [], date=date(2019, 10, 14)).to_string()
  204. self.assertRegex(latex, 'Monday October 14, 2019')
  205. self.assertRegex(latex, 'Full Moon')
  206. self.assertRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  207. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  208. self.assertNotRegex(latex, r'\\section{\\sffamily Expected events}')
  209. def test_latex_dumper_with_graph(self):
  210. latex = _LatexDumper(self._get_ephemerides(True), self._get_moon_phase(), self._get_events(),
  211. date=date(2019, 10, 14), show_graph=True).to_string()
  212. self.assertRegex(latex, 'Monday October 14, 2019')
  213. self.assertRegex(latex, 'Full Moon')
  214. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  215. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  216. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{23\}\{0\}\{08:00\}\{23:00\}')
  217. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  218. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  219. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  220. self._get_events(), date=date(2019, 10, 14)).to_string()
  221. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  222. def test_latex_dumper_with_graph_but_without_rise_time(self):
  223. ephemerides = self._get_ephemerides(True)
  224. ephemerides[0].rise_time = None
  225. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  226. date=date(2019, 10, 14), show_graph=True).to_string()
  227. self.assertRegex(latex, 'Monday October 14, 2019')
  228. self.assertRegex(latex, 'Full Moon')
  229. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  230. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  231. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{0\}\{0\}\{23\}\{0\}\{\}\{23:00\}')
  232. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  233. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  234. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  235. self._get_events(), date=date(2019, 10, 14)).to_string()
  236. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  237. def test_latex_dumper_with_graph_but_without_set_time(self):
  238. ephemerides = self._get_ephemerides(True)
  239. ephemerides[0].set_time = None
  240. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  241. date=date(2019, 10, 14), show_graph=True).to_string()
  242. self.assertRegex(latex, 'Monday October 14, 2019')
  243. self.assertRegex(latex, 'Full Moon')
  244. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  245. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  246. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{24\}\{0\}\{08:00\}\{\}')
  247. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  248. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  249. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  250. self._get_events(), date=date(2019, 10, 14)).to_string()
  251. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  252. def test_latex_dumper_with_graph_but_mars_sets_tomorrow(self):
  253. ephemerides = self._get_ephemerides(True)
  254. ephemerides[0].set_time = datetime(2019, 10, 15, 1)
  255. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  256. date=date(2019, 10, 14), show_graph=True).to_string()
  257. self.assertRegex(latex, 'Monday October 14, 2019')
  258. self.assertRegex(latex, 'Full Moon')
  259. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  260. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  261. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{24\}\{0\}\{08:00\}\{\}')
  262. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{0\}\{0\}\{1\}\{0\}\{\}\{Oct 15, 01:00\}')
  263. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  264. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  265. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  266. self._get_events(), date=date(2019, 10, 14)).to_string()
  267. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  268. @staticmethod
  269. def _get_ephemerides(aster_rise_set=False) -> [AsterEphemerides]:
  270. rise_time = datetime(2019, 10, 14, 8) if aster_rise_set else None
  271. culmination_time = datetime(2019, 10, 14, 13) if aster_rise_set else None
  272. set_time = datetime(2019, 10, 14, 23) if aster_rise_set else None
  273. return [AsterEphemerides(rise_time, culmination_time, set_time, Planet('Mars', 'MARS'))]
  274. @staticmethod
  275. def _get_moon_phase():
  276. return MoonPhase('FULL_MOON', datetime(2019, 10, 14), datetime(2019, 10, 21))
  277. @staticmethod
  278. def _get_events():
  279. return [Event('OPPOSITION',
  280. [Planet('Mars', 'MARS')],
  281. datetime(2019, 10, 14, 23, 00)),
  282. Event('MAXIMAL_ELONGATION',
  283. [Planet('Venus', 'VENUS')],
  284. datetime(2019, 10, 14, 12, 00), details='42.0°'),
  285. ]
  286. if __name__ == '__main__':
  287. unittest.main()