Skip to content

Commit

Permalink
python: add tests for railjson_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
shenriotpro authored and ElysaSrc committed Jan 5, 2024
1 parent f2e96c2 commit 53f282c
Show file tree
Hide file tree
Showing 15 changed files with 1,232 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ coverage:
threshold: 0.5%
flags:
- tests
railjson_generator:
target: auto
threshold: 0.5%
flags:
- railjson_generator
patch:
default: off

Expand All @@ -48,3 +53,6 @@ flags:
tests:
paths:
- tests/
railjson_generator:
paths:
- python/railjson_generator/
16 changes: 16 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ jobs:
run: |
cd python/railjson_generator
poetry run pytype -j auto
- name: Pytest
run: |
cd python/railjson_generator
poetry run pytest --cov --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
name: codecov
flags: railjson_generator
directory: ./python/railjson_generator
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
verbose: true
files: coverage.xml

check_integration_tests:
runs-on: ubuntu-latest
Expand Down
30 changes: 29 additions & 1 deletion python/railjson_generator/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# RAILJSON GENERATOR

Use poetry to install dependencies:

```sh
poetry install
```

## Running generation scripts

To run a generation script, pass its output directory as its first argument:
Expand All @@ -15,7 +21,7 @@ This library provides an helper to generate multiple infrastructures at once:
poetry run python3 -m railjson_generator /tmp/all_infras scripts/*.py
```

## How to use
## API

### Infra Builder

Expand Down Expand Up @@ -82,3 +88,25 @@ Route can either be manually created, or generated using `generate_routes`, and
## Example

You can find a complete example [here](./railjson_generator/scripts/examples/example_script.py).

## Testing

```sh
poetry run pytest
```

## Linting

Use pflake8 and pytype to check for style issues and potential errors.

```sh
$ poetry run pflake8 --config ./pyproject.toml
$ poetry run pytype -j auto
```

Use black and isort to fix formatting.

```sh
$ poetry run black .
$ poetry run isort .
```
162 changes: 160 additions & 2 deletions python/railjson_generator/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions python/railjson_generator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ authors = ["OSRD <contact@osrd.fr>"]
[tool.poetry.dependencies]
python = ">=3.9,<3.12"
osrd-schemas = { path = "../osrd_schemas/", develop = false }
pytest = "^7.4.3"
pytest-cov = "^4.1.0"

[tool.poetry.group.dev.dependencies]
black = "^22.12.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from dataclasses import dataclass, field
from os import PathLike
from typing import List

from osrd_schemas import external_generated_inputs

from railjson_generator.schema.infra.range_elements import TrackRange
from railjson_generator.schema.infra.track_section import TrackSection


@dataclass
Expand All @@ -12,10 +14,12 @@ class ElectricalProfile:
power_class: str
track_ranges: List[TrackRange] = field(default_factory=list)

def add_track_range(self, track, begin, end):
def add_track_range(self, track: TrackSection, begin: float, end: float):
"""Build a track range and add it to the profile."""
self.track_ranges.append(TrackRange(track=track, begin=begin, end=end))

def to_rjs(self):
"""Return the corresponding railjson object."""
return external_generated_inputs.ElectricalProfile(
value=self.value,
power_class=self.power_class,
Expand All @@ -28,14 +32,17 @@ class ExternalGeneratedInputs:
electrical_profiles: List[ElectricalProfile] = field(default_factory=list)

def add_electrical_profile(self, *args, **kwargs) -> ElectricalProfile:
"""Build an electrical profile, add it to the inputs, and return it."""
self.electrical_profiles.append(ElectricalProfile(*args, **kwargs))
return self.electrical_profiles[-1]

def save(self, path):
def save(self, path: PathLike):
"""Write to the path as railjson."""
with open(path, "w") as f:
f.write(self.to_rjs().model_dump_json(indent=2))

def to_rjs(self):
"""Return the corresponding railjson `ElectricalProfileSet`."""
return external_generated_inputs.ElectricalProfileSet(
levels=[profile.to_rjs() for profile in self.electrical_profiles],
level_order={
Expand Down
Loading

0 comments on commit 53f282c

Please sign in to comment.