Skip to content

Commit

Permalink
Add test for parsing the input from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-quintero committed Dec 7, 2023
1 parent fe685c4 commit d8eae3b
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: python lint
on: [push]
jobs:
sdk-python-lint:
python-lint:
runs-on: ubuntu-latest
steps:
- name: git clone
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: python test
on: [push]
jobs:
python-test:
runs-on: ubuntu-latest
steps:
- name: git clone
uses: actions/checkout@v3

- name: set up Python
uses: actions/setup-python@v4
with:
python-version: "3.12"

- name: install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: unit tests
run: python -m unittest
16 changes: 10 additions & 6 deletions nextmv-py.code-workspace
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
"folders": [
{
"path": "."
}
],
"settings": {
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "test*.py"],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
}
6 changes: 6 additions & 0 deletions nextmv/nextroute/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Schema (class) definitions for the entities in Nextroute."""

from .input import Defaults as Defaults
from .input import DurationGroup as DurationGroup
from .input import Input as Input
from .location import Location as Location
from .stop import AlternateStop as AlternateStop
from .stop import Stop as Stop
from .stop import StopDefaults as StopDefaults
from .vehicle import InitialStop as InitialStop
from .vehicle import Vehicle as Vehicle
from .vehicle import VehicleDefaults as VehicleDefaults
10 changes: 10 additions & 0 deletions nextmv/nextroute/schema/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Base class for data wrangling."""

import json
from dataclasses import asdict, dataclass
from typing import Any

Expand All @@ -14,6 +15,15 @@ def from_dict(cls, data: dict[str, Any]):

return cls(**data)

@classmethod
def from_json(cls, filepath: str):
"""Instantiates the class from a JSON file."""

with open(filepath) as f:
data = json.load(f)

return cls.from_dict(data)

def to_dict(self) -> dict[str, Any]:
"""Converts the class to a dict."""

Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/nextroute/__init__.py
Empty file.
Empty file.
263 changes: 263 additions & 0 deletions tests/nextroute/schema/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
{
"defaults": {
"vehicles": {
"capacity": {
"bunnies": 20,
"rabbits": 10
},
"start_location": {
"lat": 35.791729813680874,
"lon": -78.7401685145487
},
"end_location": {
"lat": 35.791729813680874,
"lon": -78.7401685145487
},
"speed": 10
},
"stops": {
"duration": 300,
"quantity": {
"bunnies": -1,
"rabbits": -1
},
"unplanned_penalty": 200000,
"target_arrival_time": "2023-01-01T10:00:00Z",
"early_arrival_time_penalty": 1.5,
"late_arrival_time_penalty": 1.5
}
},
"stops": [
{
"id": "s1",
"location": {
"lon": -78.90919,
"lat": 35.72389
},
"compatibility_attributes": ["premium"]
},
{
"id": "s2",
"location": {
"lon": -78.813862,
"lat": 35.75712
},
"compatibility_attributes": ["premium"]
},
{
"id": "s3",
"location": {
"lon": -78.92996,
"lat": 35.932795
},
"compatibility_attributes": ["premium"]
},
{
"id": "s4",
"location": {
"lon": -78.505745,
"lat": 35.77772
},
"compatibility_attributes": ["premium"]
},
{
"id": "s5",
"location": {
"lon": -78.75084,
"lat": 35.732995
},
"compatibility_attributes": ["premium"]
},
{
"id": "s6",
"location": {
"lon": -78.788025,
"lat": 35.813025
},
"compatibility_attributes": ["premium"]
},
{
"id": "s7",
"location": {
"lon": -78.749391,
"lat": 35.74261
},
"compatibility_attributes": ["premium"]
},
{
"id": "s8",
"location": {
"lon": -78.94658,
"lat": 36.039135
},
"compatibility_attributes": ["basic"]
},
{
"id": "s9",
"location": {
"lon": -78.64972,
"lat": 35.64796
},
"compatibility_attributes": ["basic"]
},
{
"id": "s10",
"location": {
"lon": -78.747955,
"lat": 35.672955
},
"compatibility_attributes": ["basic"]
},
{
"id": "s11",
"location": {
"lon": -78.83403,
"lat": 35.77013
},
"compatibility_attributes": ["basic"]
},
{
"id": "s12",
"location": {
"lon": -78.864465,
"lat": 35.782855
},
"compatibility_attributes": ["basic"]
},
{
"id": "s13",
"location": {
"lon": -78.952142,
"lat": 35.88029
},
"compatibility_attributes": ["basic"]
},
{
"id": "s14",
"location": {
"lon": -78.52748,
"lat": 35.961465
},
"compatibility_attributes": ["basic"]
},
{
"id": "s15",
"location": {
"lon": -78.89832,
"lat": 35.83202
}
},
{
"id": "s16",
"location": {
"lon": -78.63216,
"lat": 35.83458
}
},
{
"id": "s17",
"location": {
"lon": -78.76063,
"lat": 35.67337
}
},
{
"id": "s18",
"location": {
"lon": -78.911485,
"lat": 36.009015
}
},
{
"id": "s19",
"location": {
"lon": -78.522705,
"lat": 35.93663
}
},
{
"id": "s20",
"location": {
"lon": -78.995162,
"lat": 35.97414
}
},
{
"id": "s21",
"location": {
"lon": -78.50509,
"lat": 35.7606
}
},
{
"id": "s22",
"location": {
"lon": -78.828547,
"lat": 35.962635
},
"precedes": ["s16", "s23"]
},
{
"id": "s23",
"location": {
"lon": -78.60914,
"lat": 35.84616
},
"start_time_window": [
"2023-01-01T09:00:00-06:00",
"2023-01-01T09:30:00-06:00"
]
},
{
"id": "s24",
"location": {
"lon": -78.65521,
"lat": 35.740605
},
"start_time_window": [
"2023-01-01T09:00:00-06:00",
"2023-01-01T09:30:00-06:00"
],
"succeeds": "s25"
},
{
"id": "s25",
"location": {
"lon": -78.92051,
"lat": 35.887575
},
"start_time_window": [
"2023-01-01T09:00:00-06:00",
"2023-01-01T09:30:00-06:00"
],
"precedes": "s26"
},
{
"id": "s26",
"location": {
"lon": -78.84058,
"lat": 35.823865
},
"start_time_window": [
"2023-01-01T09:00:00-06:00",
"2023-01-01T09:30:00-06:00"
]
}
],
"vehicles": [
{
"id": "vehicle-0",
"start_time": "2023-01-01T06:00:00-06:00",
"end_time": "2023-01-01T10:00:00-06:00",
"activation_penalty": 4000,
"compatibility_attributes": ["premium"]
},
{
"id": "vehicle-1",
"start_time": "2023-01-01T10:00:00-06:00",
"end_time": "2023-01-01T16:00:00-06:00",
"max_duration": 21000,
"compatibility_attributes": ["basic"]
}
]
}
21 changes: 21 additions & 0 deletions tests/nextroute/schema/test_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
import os
import unittest

from nextmv.nextroute.schema import Input


class TestInput(unittest.TestCase):
def test_from_json(self):
filepath = os.path.join(os.path.dirname(__file__), "input.json")
input = Input.from_json(filepath)
parsed = input.to_dict()
with open(filepath) as f:
expected = json.load(f)

self.maxDiff = None
self.assertEqual(
parsed,
expected,
"Parsing the JSON into the class and back should yield the same result.",
)
10 changes: 0 additions & 10 deletions tests/test_hello.py

This file was deleted.

0 comments on commit d8eae3b

Please sign in to comment.