Browse Source

Add unit tests, fix variable names

tags/v0.2.0
Jérôme Deuchnord 5 years ago
parent
commit
a8dfe3bd11
No known key found for this signature in database GPG Key ID: BC6F3C345B7D33B0
7 changed files with 59 additions and 13 deletions
  1. +3
    -1
      .github/workflows/pythonapp.yml
  2. +11
    -0
      CHANGELOG.md
  3. +1
    -1
      kosmorrolib/data.py
  4. +8
    -8
      kosmorrolib/dumper.py
  5. +3
    -3
      kosmorrolib/ephemerides.py
  6. +1
    -0
      test/__init__.py
  7. +32
    -0
      test/dumper.py

+ 3
- 1
.github/workflows/pythonapp.yml View File

@@ -4,7 +4,6 @@ on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
@@ -17,6 +16,9 @@ jobs:
run: |
pip install --upgrade pip pipenv
pipenv sync -d
- name: Unit tests
run: |
pipenv run python -m unittest -v test
- name: Lint
run: |
pipenv run pylint kosmorro *.py kosmorrolib/*.py

+ 11
- 0
CHANGELOG.md View File

@@ -6,7 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Update Numpy to v1.17.4
- Add JSON output (#6)

### Changed



### Removed



## [0.1.0] - 2019-11-10



+ 1
- 1
kosmorrolib/data.py View File

@@ -55,7 +55,7 @@ class AsterEphemerides:
culmination_time: Union[Time, None],
set_time: Union[Time, None]):
self.rise_time = rise_time
self.maximum_time = culmination_time
self.culmination_time = culmination_time
self.set_time = set_time




+ 8
- 8
kosmorrolib/dumper.py View File

@@ -26,7 +26,7 @@ from .data import Object, AsterEphemerides, MOON_PHASES


class Dumper(ABC):
def __init__(self, ephemeris, date: datetime.date = datetime.date.today()):
def __init__(self, ephemeris: dict, date: datetime.date = datetime.date.today()):
self.ephemeris = ephemeris
self.date = date

@@ -62,7 +62,7 @@ class JsonDumper(Dumper):
class TextDumper(Dumper):
def to_string(self):
return '\n\n'.join(['Ephemerides of %s' % self.date.strftime('%A %B %d, %Y'),
self.get_asters(self.ephemeris['planets']),
self.get_asters(self.ephemeris['details']),
self.get_moon(self.ephemeris['moon_phase']),
'Note: All the hours are given in UTC.'])

@@ -78,21 +78,21 @@ class TextDumper(Dumper):
else:
planet_rise = '-'

if aster.ephemerides.maximum_time is not None:
planet_maximum = aster.ephemerides.maximum_time.utc_strftime('%H:%M')
if aster.ephemerides.culmination_time is not None:
planet_culmination = aster.ephemerides.culmination_time.utc_strftime('%H:%M')
else:
planet_maximum = '-'
planet_culmination = '-'

if aster.ephemerides.set_time is not None:
planet_set = aster.ephemerides.set_time.utc_strftime('%H:%M')
else:
planet_set = '-'

data.append([name, planet_rise, planet_maximum, planet_set])
data.append([name, planet_rise, planet_culmination, planet_set])

return tabulate(data, headers=['Planet', 'Rise time', 'Culmination time', 'Set time'], tablefmt='simple',
stralign='center', colalign=('left',))

@staticmethod
def get_moon(moon):
return 'Moon phase: %s' % MOON_PHASES[moon['phase']]
def get_moon(moon_phase: str) -> str:
return 'Moon phase: %s' % MOON_PHASES[moon_phase]

+ 3
- 3
kosmorrolib/ephemerides.py View File

@@ -42,13 +42,13 @@ class EphemeridesComputer:
return {'rise': sunrise, 'set': sunset}

@staticmethod
def get_moon(year, month, day) -> dict:
def get_moon(year, month, day) -> str:
time1 = get_timescale().utc(year, month, day - 10)
time2 = get_timescale().utc(year, month, day)

_, moon_phase = almanac.find_discrete(time1, time2, almanac.moon_phases(get_skf_objects()))

return {'phase': skyfield_to_moon_phase(moon_phase[-1])}
return skyfield_to_moon_phase(moon_phase[-1])

@staticmethod
def get_asters_ephemerides_for_aster(aster, date: datetime.date, position: Position) -> Object:
@@ -90,7 +90,7 @@ class EphemeridesComputer:

def compute_ephemerides_for_day(self, year: int, month: int, day: int) -> dict:
return {'moon_phase': self.get_moon(year, month, day),
'planets': [self.get_asters_ephemerides_for_aster(aster, datetime.date(year, month, day), self.position)
'details': [self.get_asters_ephemerides_for_aster(aster, datetime.date(year, month, day), self.position)
for aster in ASTERS]}

def compute_ephemerides_for_month(self, year: int, month: int) -> [dict]:


+ 1
- 0
test/__init__.py View File

@@ -0,0 +1 @@
from .dumper import *

+ 32
- 0
test/dumper.py View File

@@ -0,0 +1,32 @@
import unittest
from kosmorrolib.data import AsterEphemerides, Planet
from kosmorrolib.dumper import JsonDumper


class DumperTestCase(unittest.TestCase):
def test_json_dumper_returns_correct_json(self):
data = self._get_data()
self.assertEqual('{\n'
' "moon_phase": "FULL_MOON",\n'
' "details": [\n'
' {\n'
' "name": "Mars",\n'
' "ephemerides": {\n'
' "rise_time": null,\n'
' "culmination_time": null,\n'
' "set_time": null\n'
' }\n'
' }\n'
' ]\n'
'}', JsonDumper(data).to_string())

@staticmethod
def _get_data():
return {
'moon_phase': 'FULL_MOON',
'details': [Planet('Mars', 'MARS', AsterEphemerides(None, None, None))]
}


if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save