Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

dumper.py 16 KiB

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