Skip to content

Commit

Permalink
Moved test data to a single directory shared by all modules.
Browse files Browse the repository at this point in the history
Added a new library for accessing the test data that can be reused from
all tests that need it.
  • Loading branch information
ondrasej committed Jan 17, 2024
1 parent c974247 commit b7739a7
Show file tree
Hide file tree
Showing 51 changed files with 110 additions and 81 deletions.
17 changes: 4 additions & 13 deletions python/cfr/analysis/analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@

from . import analysis
from ..json import cfr_json
from ..testdata import testdata


# Provides easy access to files under `./testdata`. See `_json()` below for
# example use.
# TODO(ondrasej): Move the test data loading code to a separate library.
_TESTDATA = resources.files(__package__).joinpath("testdata")


def _json(path: str):
return json.loads(_TESTDATA.joinpath(path).read_bytes())


_SCENARIO: cfr_json.OptimizeToursRequest = _json(
_SCENARIO: cfr_json.OptimizeToursRequest = testdata.json(
"moderate/scenario.merged_request.60s.180s.json"
)
_SOLUTION: cfr_json.OptimizeToursResponse = _json(
_SOLUTION: cfr_json.OptimizeToursResponse = testdata.json(
"moderate/scenario.merged_response.60s.180s.json"
)
_PARKING_JSON = _json("moderate/parking.json")
_PARKING_JSON = testdata.json("moderate/parking.json")


class GroupGlobalVisits(unittest.TestCase):
Expand Down
4 changes: 4 additions & 0 deletions python/cfr/testdata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Google LLC. All Rights Reserved.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE file or at https://opensource.org/licenses/MIT.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions python/cfr/testdata/testdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Google LLC. All Rights Reserved.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE file or at https://opensource.org/licenses/MIT.


"""Provides easy access to the test data in JSON format."""

from importlib import resources
import json as json_lib

# Provides easy access to files under `./testdata`. See `_json()` below for
# example use.
_TESTDATA = resources.files(__package__)


def json(path: str):
"""Parses a JSON file at `path` and returns it as a dict/list structure.
Args:
path: The path of the JSON file, relative to the package of this module.
Returns:
The JSON data structure.
"""
return json_lib.loads(_TESTDATA.joinpath(path).read_bytes())
144 changes: 76 additions & 68 deletions python/cfr/two_step_routing/two_step_routing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@
import json
import unittest

from ..testdata import testdata
from ..json import cfr_json
from . import two_step_routing


# Provides easy access to files under `./testdata`. See `_json()` below for
# example use.
_TESTDATA = resources.files(__package__).joinpath("testdata")


def _json(path: str):
"""Parses a JSON file at `path` and returns it as a dict/list structure.
Args:
path: The path of the JSON file, relative to `./testdata`.
Returns:
The JSON data structure.
"""
return json.loads(_TESTDATA.joinpath(path).read_bytes())


class ParkingLocationTest(unittest.TestCase):
"""Tests for ParkingLocation."""

Expand Down Expand Up @@ -190,54 +174,58 @@ class PlannerTest(unittest.TestCase):
local_model_grouping=two_step_routing.LocalModelGrouping.PARKING,
)

_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json("small/request.json")
_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"small/request.json"
)
_PARKING_LOCATIONS, _PARKING_FOR_SHIPMENT = (
two_step_routing.load_parking_from_json(_json("small/parking.json"))
two_step_routing.load_parking_from_json(
testdata.json("small/parking.json")
)
)

# The expected local model request created by the two-step planner for the
# base request defined above.
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"small/expected_local_request.json"
)

# An example response from the CFR solver for _EXPECTED_LOCAL_REQUEST_JSON.
# Fields that are not needed by the two-step solver were removed from the
# response to make it shorter.
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"small/local_response.json"
)

_EXPECTED_LOCAL_REQUEST_GROUP_BY_PARKING_JSON: (
cfr_json.OptimizeToursRequest
) = _json("small/expected_local_request_group_by_parking.json")
) = testdata.json("small/expected_local_request_group_by_parking.json")

# The expected global model request created by the two-step planner for the
# base request defined above, using _EXPECTED_LOCAL_REQUEST_JSON as the
# solution of the local model.
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"small/expected_global_request.json"
)

# An example response from the CFR solver for _EXPECTED_GLOBAL_REQUEST_JSON.
# Fields that are not needed by the two-step solver were removed from the
# response to make it shorter.
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"small/global_response.json"
)

# The expected merged model request created by the two-step planner for the
# base request defined above, using _EXPECTED_LOCAL_REQUEST and
# _EXPECTED_GLOBAL_REQUEST as the solutions of the local and global models.
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"small/expected_merged_request.json"
)

# The expected merged model response creatd by the two-step planner for the
# base request defined above, using _EXPECTED_LOCAL_REQUEST and
# _EXPECTED_GLOBAL_REQUEST as the solutions of the local and global models.
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
"small/expected_merged_response.json"
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = (
testdata.json("small/expected_merged_response.json")
)

def validate_response(
Expand Down Expand Up @@ -423,14 +411,14 @@ class PlannerTestMergedModel(PlannerTest):

_LOCAL_RESPONSE_WITH_SKIPPED_SHIPMENTS_JSON: (
cfr_json.OptimizeToursResponse
) = _json("small/local_response_with_skipped_shipments.json")
) = testdata.json("small/local_response_with_skipped_shipments.json")
_GLOBAL_RESPONSE_WITH_SKIPPED_SHIPMENTS_JSON: (
cfr_json.OptimizeToursResponse
) = _json("small/global_response_with_skipped_shipments.json")
) = testdata.json("small/global_response_with_skipped_shipments.json")
_EXPECTED_MERGED_REQUEST_WITH_SKIPPED_SHIPMENTS_JSON: (
cfr_json.OptimizeToursRequest
) = _json("small/expected_merged_request_with_skipped_shipments.json")
_EXPECTED_MERGED_RESPONSE_WITH_SKIPPED_SHIPMENTS_JSON = _json(
) = testdata.json("small/expected_merged_request_with_skipped_shipments.json")
_EXPECTED_MERGED_RESPONSE_WITH_SKIPPED_SHIPMENTS_JSON = testdata.json(
"small/expected_merged_response_with_skipped_shipments.json"
)

Expand Down Expand Up @@ -482,12 +470,14 @@ def test_make_merged_request_and_response_with_skipped_shipments(self):


class PlannerTestRefinedLocalModel(PlannerTest):
_EXPECTED_LOCAL_REFINEMENT_REQUEST: cfr_json.OptimizeToursRequest = _json(
"small/expected_local_refinement_request.json"
_EXPECTED_LOCAL_REFINEMENT_REQUEST: cfr_json.OptimizeToursRequest = (
testdata.json("small/expected_local_refinement_request.json")
)
_EXPECTED_LOCAL_REFINEMENT_REQUEST_WITH_RELOAD_COST: (
cfr_json.OptimizeToursRequest
) = _json("small/expected_local_refinement_request_with_reload_costs.json")
) = testdata.json(
"small/expected_local_refinement_request_with_reload_costs.json"
)

def test_local_refinement_model(self):
planner = two_step_routing.Planner(
Expand Down Expand Up @@ -524,17 +514,17 @@ def test_local_refinement_model_with_reload_cost(self):


class PlannerTestIntegratedModels(PlannerTest):
_LOCAL_REFINEMENT_RESPONSE: cfr_json.OptimizeToursResponse = _json(
_LOCAL_REFINEMENT_RESPONSE: cfr_json.OptimizeToursResponse = testdata.json(
"small/local_refinement_response.json"
)
_EXPECTED_INTEGRATED_LOCAL_REQUEST: cfr_json.OptimizeToursRequest = _json(
"small/expected_integrated_local_request.json"
_EXPECTED_INTEGRATED_LOCAL_REQUEST: cfr_json.OptimizeToursRequest = (
testdata.json("small/expected_integrated_local_request.json")
)
_EXPECTED_INTEGRATED_LOCAL_RESPONSE: cfr_json.OptimizeToursResponse = _json(
"small/expected_integrated_local_response.json"
_EXPECTED_INTEGRATED_LOCAL_RESPONSE: cfr_json.OptimizeToursResponse = (
testdata.json("small/expected_integrated_local_response.json")
)
_EXPECTED_INTEGRATED_GLOBAL_REQUEST: cfr_json.OptimizeToursRequest = _json(
"small/expected_integrated_global_request.json"
_EXPECTED_INTEGRATED_GLOBAL_REQUEST: cfr_json.OptimizeToursRequest = (
testdata.json("small/expected_integrated_global_request.json")
)

def test_integrated_models(self):
Expand Down Expand Up @@ -575,27 +565,31 @@ class PlannerTestWithPlaceId(unittest.TestCase):
min_average_shipments_per_round=1,
)

_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json("place_id/scenario.json")
_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"place_id/scenario.json"
)
_PARKING_LOCATIONS, _PARKING_FOR_SHIPMENT = (
two_step_routing.load_parking_from_json(_json("place_id/parking.json"))
two_step_routing.load_parking_from_json(
testdata.json("place_id/parking.json")
)
)
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursResponse = _json(
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"place_id/scenario.local_request.json"
)
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"place_id/scenario.local_response.60s.json"
)
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"place_id/scenario.global_request.60s.json"
)
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"place_id/scenario.global_response.60s.60s.json"
)
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"place_id/scenario.merged_request.60s.60s.json"
)
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
"place_id/scenario.merged_response.60s.60s.json"
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = (
testdata.json("place_id/scenario.merged_response.60s.60s.json")
)

def test_local_model(self):
Expand Down Expand Up @@ -643,42 +637,56 @@ class PlannerTestWithBreaks(unittest.TestCase):
min_average_shipments_per_round=1,
)

_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json("breaks/scenario.json")
_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"breaks/scenario.json"
)
_PARKING_LOCATIONS, _PARKING_FOR_SHIPMENT = (
two_step_routing.load_parking_from_json(_json("breaks/parking.json"))
two_step_routing.load_parking_from_json(
testdata.json("breaks/parking.json")
)
)
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_LOCAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"breaks/scenario.local_request.json"
)
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_LOCAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"breaks/scenario.local_response.120s.json"
)
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_GLOBAL_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"breaks/scenario.global_request.120s.json"
)
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
_GLOBAL_RESPONSE_JSON: cfr_json.OptimizeToursResponse = testdata.json(
"breaks/scenario.global_response.120s.240s.json"
)
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = _json(
_EXPECTED_MERGED_REQUEST_JSON: cfr_json.OptimizeToursRequest = testdata.json(
"breaks/scenario.merged_request.120s.240s.json"
)
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
"breaks/scenario.merged_response.120s.240s.json"
_EXPECTED_MERGED_RESPONSE_JSON: cfr_json.OptimizeToursResponse = (
testdata.json("breaks/scenario.merged_response.120s.240s.json")
)
_EXPECTED_LOCAL_REFINEMENT_REQUEST_JSON: cfr_json.OptimizeToursRequest = (
_json("breaks/scenario.refined_1.local_request.120s.240s.120s.120s.json")
testdata.json(
"breaks/scenario.refined_1.local_request.120s.240s.120s.120s.json"
)
)
_LOCAL_REFINEMENT_RESPONSE_JSON: cfr_json.OptimizeToursResponse = _json(
"breaks/scenario.refined_1.local_response.120s.240s.120s.120s.json"
_LOCAL_REFINEMENT_RESPONSE_JSON: cfr_json.OptimizeToursResponse = (
testdata.json(
"breaks/scenario.refined_1.local_response.120s.240s.120s.120s.json"
)
)
_EXPECTED_INTEGRATED_LOCAL_REQUEST: cfr_json.OptimizeToursRequest = _json(
"breaks/scenario.refined_1.integrated_local_request.120s.240s.120s.120s.json"
_EXPECTED_INTEGRATED_LOCAL_REQUEST: cfr_json.OptimizeToursRequest = (
testdata.json(
"breaks/scenario.refined_1.integrated_local_request.120s.240s.120s.120s.json"
)
)
_EXPECTED_INTEGRATED_LOCAL_RESPONSE: cfr_json.OptimizeToursResponse = _json(
"breaks/scenario.refined_1.integrated_local_response.120s.240s.120s.120s.json"
_EXPECTED_INTEGRATED_LOCAL_RESPONSE: cfr_json.OptimizeToursResponse = (
testdata.json(
"breaks/scenario.refined_1.integrated_local_response.120s.240s.120s.120s.json"
)
)
_EXPECTED_INTEGRATED_GLOBAL_REQUEST: cfr_json.OptimizeToursRequest = _json(
"breaks/scenario.refined_1.integrated_global_request.120s.240s.120s.120s.json"
_EXPECTED_INTEGRATED_GLOBAL_REQUEST: cfr_json.OptimizeToursRequest = (
testdata.json(
"breaks/scenario.refined_1.integrated_global_request.120s.240s.120s.120s.json"
)
)

def setUp(self):
Expand Down

0 comments on commit b7739a7

Please sign in to comment.