Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RealizationConfigDomainDetector file opening #622

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class RealizationConfigDomainDetector(ItemDataDomainDetector):

def detect(self, **_) -> DataDomain:
try:
real_obj = ngen.config.realization.NgenRealization(**json.load(self._item))
real_obj = ngen.config.realization.NgenRealization(**json.load(self._item.open()))
except Exception as e:
raise DmodRuntimeError(f"{self.__class__.__name__} failed detect due to {e.__class__.__name__}: {e!s}")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest
from datetime import datetime

from dmod.core.meta_data import DataFormat, StandardDatasetIndex
from ngen.config.realization import NgenRealization
from ..modeldata.data.item_domain_detector import RealizationConfigDomainDetector
from . import find_git_root_dir


class TestRealizationConfigDomainDetector(unittest.TestCase):

def setUp(self):
datetime_format: str = "%Y-%m-%d %H:%M:%S"

self.detector_subclass = RealizationConfigDomainDetector
self.expected_data_format = DataFormat.NGEN_REALIZATION_CONFIG

# Setup example 0
self.example_data = {0: find_git_root_dir().joinpath("data/example_realization_configs/ex_realization_config_03.json")}
self.example_begins = {0: datetime.strptime("2016-01-01 00:00:00", datetime_format)}
self.example_ends = {0: datetime.strptime("2016-01-31 23:00:00", datetime_format)}

def test_get_data_format_0_a(self):
""" Test that we get the correct data format for this subclass type. """
self.assertEqual(self.expected_data_format, self.detector_subclass.get_data_format())

def test_detect_0_a(self):
""" Test that detect returns a domain with the right data format. """
ex_idx = 0

detector = self.detector_subclass(item=self.example_data[ex_idx])
domain = detector.detect()
self.assertEqual(self.expected_data_format, domain.data_format)

def test_detect_0_b(self):
""" Test that detect returns a domain with the right begin time. """
ex_idx = 0

detector = self.detector_subclass(item=self.example_data[ex_idx])
domain = detector.detect()
time_range = domain.continuous_restrictions[StandardDatasetIndex.TIME]
self.assertEqual(self.example_begins[ex_idx], time_range.begin)

def test_detect_0_c(self):
""" Test that detect returns a domain with the right end time. """
ex_idx = 0

detector = self.detector_subclass(item=self.example_data[ex_idx])
domain = detector.detect()
time_range = domain.continuous_restrictions[StandardDatasetIndex.TIME]
self.assertEqual(self.example_ends[ex_idx], time_range.end)

def test_detect_0_d(self):
""" Test that detect returns a domain with "global"" catchment id restriction. """
ex_idx = 0

detector = self.detector_subclass(item=self.example_data[ex_idx])
domain = detector.detect()
self.assertTrue(len(domain.discrete_restrictions[StandardDatasetIndex.CATCHMENT_ID].values) == 0)
Loading