Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

301 lignes
16 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_latex_dumper(self):
  153. latex = _LatexDumper(self._get_ephemerides(), self._get_moon_phase(), self._get_events(),
  154. 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_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  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(None, self._get_moon_phase(), self._get_events(),
  167. date=date(2019, 10, 14)).to_string()
  168. self.assertRegex(latex, 'Monday October 14, 2019')
  169. self.assertRegex(latex, 'Full Moon')
  170. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  171. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  172. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  173. self.assertNotRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  174. self.assertNotRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  175. def test_latex_dumper_without_events(self):
  176. latex = _LatexDumper(self._get_ephemerides(), self._get_moon_phase(), [], date=date(2019, 10, 14)).to_string()
  177. self.assertRegex(latex, 'Monday October 14, 2019')
  178. self.assertRegex(latex, 'Full Moon')
  179. self.assertRegex(latex, r'\\object\{Mars\}\{-\}\{-\}\{-\}')
  180. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  181. self.assertNotRegex(latex, r'\\section{\\sffamily Expected events}')
  182. def test_latex_dumper_with_graph(self):
  183. latex = _LatexDumper(self._get_ephemerides(True), self._get_moon_phase(), self._get_events(),
  184. date=date(2019, 10, 14), show_graph=True).to_string()
  185. self.assertRegex(latex, 'Monday October 14, 2019')
  186. self.assertRegex(latex, 'Full Moon')
  187. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  188. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  189. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{23\}\{0\}\{08:00\}\{23:00\}')
  190. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  191. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  192. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  193. self._get_events(), date=date(2019, 10, 14)).to_string()
  194. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  195. def test_latex_dumper_with_graph_but_without_rise_time(self):
  196. ephemerides = self._get_ephemerides(True)
  197. ephemerides[0].rise_time = None
  198. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  199. date=date(2019, 10, 14), show_graph=True).to_string()
  200. self.assertRegex(latex, 'Monday October 14, 2019')
  201. self.assertRegex(latex, 'Full Moon')
  202. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  203. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  204. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{0\}\{0\}\{23\}\{0\}\{\}\{23:00\}')
  205. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  206. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  207. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  208. self._get_events(), date=date(2019, 10, 14)).to_string()
  209. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  210. def test_latex_dumper_with_graph_but_without_set_time(self):
  211. ephemerides = self._get_ephemerides(True)
  212. ephemerides[0].set_time = None
  213. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  214. date=date(2019, 10, 14), show_graph=True).to_string()
  215. self.assertRegex(latex, 'Monday October 14, 2019')
  216. self.assertRegex(latex, 'Full Moon')
  217. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  218. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  219. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{24\}\{0\}\{08:00\}\{\}')
  220. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  221. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  222. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  223. self._get_events(), date=date(2019, 10, 14)).to_string()
  224. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  225. def test_latex_dumper_with_graph_but_mars_sets_tomorrow(self):
  226. ephemerides = self._get_ephemerides(True)
  227. ephemerides[0].set_time = datetime(2019, 10, 15, 1)
  228. latex = _LatexDumper(ephemerides, self._get_moon_phase(), self._get_events(),
  229. date=date(2019, 10, 14), show_graph=True).to_string()
  230. self.assertRegex(latex, 'Monday October 14, 2019')
  231. self.assertRegex(latex, 'Full Moon')
  232. self.assertRegex(latex, r'\\section{\\sffamily Expected events}')
  233. self.assertRegex(latex, r'\\section{\\sffamily Ephemerides of the day}')
  234. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{8\}\{0\}\{24\}\{0\}\{08:00\}\{\}')
  235. self.assertRegex(latex, r'\\graphobject\{18\}\{gray\}\{0\}\{0\}\{1\}\{0\}\{\}\{Oct 15, 01:00\}')
  236. self.assertRegex(latex, r'\\event\{23:00\}\{Mars is in opposition\}')
  237. self.assertRegex(latex, r"\\event\{12:00\}\{Venus's largest elongation \(42.0°\)\}")
  238. latex = _LatexDumper(self._get_ephemerides(aster_rise_set=True), self._get_moon_phase(),
  239. self._get_events(), date=date(2019, 10, 14)).to_string()
  240. self.assertRegex(latex, r'\\object\{Mars\}\{08:00\}\{13:00\}\{23:00\}')
  241. def test_get_moon_with_moon_phase_none(self):
  242. dumper = TextDumper()
  243. self.assertEqual('Moon phase is unavailable for this date.', dumper.get_moon(None))
  244. @staticmethod
  245. def _get_ephemerides(aster_rise_set=False) -> [AsterEphemerides]:
  246. rise_time = datetime(2019, 10, 14, 8) if aster_rise_set else None
  247. culmination_time = datetime(2019, 10, 14, 13) if aster_rise_set else None
  248. set_time = datetime(2019, 10, 14, 23) if aster_rise_set else None
  249. return [AsterEphemerides(rise_time, culmination_time, set_time, Planet('Mars', 'MARS'))]
  250. @staticmethod
  251. def _get_moon_phase():
  252. return MoonPhase('FULL_MOON', datetime(2019, 10, 14), datetime(2019, 10, 21))
  253. @staticmethod
  254. def _get_events():
  255. return [Event('OPPOSITION',
  256. [Planet('Mars', 'MARS')],
  257. datetime(2019, 10, 14, 23, 00)),
  258. Event('MAXIMAL_ELONGATION',
  259. [Planet('Venus', 'VENUS')],
  260. datetime(2019, 10, 14, 12, 00), details='42.0°'),
  261. ]
  262. if __name__ == '__main__':
  263. unittest.main()