Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
* Included tests for the "Planning" object. #41
Browse files Browse the repository at this point in the history
  • Loading branch information
garciparedes committed Sep 1, 2019
1 parent a8b7298 commit 3c23f1a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
16 changes: 16 additions & 0 deletions jinete/models/plannings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
Set,
Dict,
Any,
Iterator,
)
from .routes import (
Route,
)
from .trips import (
Trip,
)
from .planned_trips import (
PlannedTrip,
)
from uuid import (
UUID,
)
Expand Down Expand Up @@ -46,6 +53,15 @@ def __iter__(self):
def loaded_routes(self):
return set(route for route in self.routes if route.loaded)

@property
def planned_trips(self) -> Iterator[PlannedTrip]:
for route in self.routes:
yield from route.planned_trips

@property
def trips(self) -> Iterator[Trip]:
yield from (planned_trip.trip for planned_trip in self.planned_trips)

def as_dict(self) -> Dict[str, Any]:
return {
'uuid': self.uuid,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_construction(self):
def test_deepcopy(self):
job = generate_one_job(True)
copied_job = deepcopy(job)
self.assertTrue(job, copied_job)
self.assertEqual(job, copied_job)


if __name__ == '__main__':
Expand Down
58 changes: 58 additions & 0 deletions tests/test_models/test_plannings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import annotations

import unittest
from copy import deepcopy
from typing import TYPE_CHECKING

import itertools as it

import jinete as jit

from tests.utils import (
generate_routes,
)

if TYPE_CHECKING:
from typing import (
Set,
List
)


class TestPlanning(unittest.TestCase):
routes: Set[jit.Route]
loaded_routes: Set[jit.Route]
planned_trips: List[jit.PlannedTrip]
trips: List[jit.Trip]

@classmethod
def setUpClass(cls) -> None:
cls.routes = generate_routes(3)
cls.loaded_routes = set(route for route in cls.routes if route.loaded)
cls.planned_trips = list(it.chain.from_iterable(route.planned_trips for route in cls.routes))
cls.trips = list(planned_trip.trip for planned_trip in cls.planned_trips)

def test_construction(self):
planning = jit.Planning(self.routes)

self.assertIsInstance(planning, jit.Planning)
self.assertEqual(planning.routes, self.routes)
self.assertEqual(planning.loaded_routes, self.loaded_routes)
self.assertEqual(list(planning.planned_trips), self.planned_trips)
self.assertEqual(list(planning.trips), self.trips)

def test_deepcopy(self):
planning = jit.Planning(self.routes)

copied_planning = deepcopy(planning)
self.assertNotEqual(planning, copied_planning)
self.assertNotEqual(id(planning), id(copied_planning))
self.assertNotEqual(planning.uuid, copied_planning.uuid)
self.assertEqual(len(planning.routes), len(copied_planning.routes))
self.assertTrue(planning.routes.isdisjoint(copied_planning.routes))
for route in planning.routes:
assert any(set(route.trips).isdisjoint(copied_route.trips) for copied_route in copied_planning)


if __name__ == '__main__':
unittest.main()
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def generate_one_job(trips_count: int = None, trips_count_min: int = 1, trips_co
return job


def generate_one_planning(routes_count: int = None, routes_count_min: int = 1, routes_count_max: int = 100,
*args, **kwargs) -> jit.Planning:
if routes_count is None:
routes_count = randint(routes_count_min, routes_count_max)
trips = generate_routes(routes_count, *args, **kwargs)
planning = jit.Planning(trips)
return planning


def generate_jobs(n: int, *args, **kwargs) -> Set[jit.Job]:
return {
generate_one_job(*args, **kwargs) for _ in range(n)
Expand Down

0 comments on commit 3c23f1a

Please sign in to comment.