Skip to content

Commit

Permalink
Add execution test for entity alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen committed Nov 16, 2023
1 parent ee5a65f commit c6ba398
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.spinetoolbox/local/
.spinetoolbox/items/
*.bak*
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"project": {
"version": 12,
"description": "",
"settings": {
"enable_execute_all": true
},
"specifications": {
"Exporter": [
{
"type": "path",
"relative": true,
"path": ".spinetoolbox/specifications/Exporter/entities.json"
}
]
},
"connections": [
{
"name": "from Source to Export entities",
"from": [
"Source",
"right"
],
"to": [
"Export entities",
"left"
],
"options": {
"require_scenario_filter": true
},
"filter_settings": {
"known_filters": {
"db_url@Source": {
"scenario_filter": {
"scenario": true
}
}
},
"auto_online": true
}
}
],
"jumps": []
},
"items": {
"Source": {
"type": "Data Store",
"description": "",
"x": -130.22135416666669,
"y": 20.03405448717949,
"url": {
"dialect": "sqlite",
"host": "",
"port": "",
"database": {
"type": "path",
"relative": true,
"path": ".spinetoolbox/items/source/Source.sqlite"
}
}
},
"Export entities": {
"type": "Exporter",
"description": "",
"x": 33.05618990384616,
"y": 20.03405448717949,
"output_time_stamps": false,
"cancel_on_error": true,
"output_labels": [
{
"in_label": "db_url@Source",
"out_label": "entities"
}
],
"specification": "entities"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"item_type": "Exporter",
"output_format": "csv",
"name": "entities",
"description": "",
"mappings": {
"Mapping (1)": {
"type": "entities",
"mapping": [
{
"map_type": "EntityClass",
"position": 0
},
{
"map_type": "Entity",
"position": 1
}
],
"enabled": true,
"always_export_header": true,
"group_fn": "no_group",
"use_fixed_table_name": false
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import subprocess
import sys
from pathlib import Path
import shutil
import unittest

from spinedb_api import DatabaseMapping


class ExportEntitiesWithEntityAlternatives(unittest.TestCase):
_root_path = Path(__file__).parent
_database_path = _root_path / ".spinetoolbox" / "items" / "source" / "Source.sqlite"
_exporter_output_path = _root_path / ".spinetoolbox" / "items" / "export_entities" / "output"

def setUp(self):
if self._exporter_output_path.exists():
shutil.rmtree(self._exporter_output_path)
self._database_path.parent.mkdir(parents=True, exist_ok=True)
if self._database_path.exists():
self._database_path.unlink()
url = "sqlite:///" + str(self._database_path)
with DatabaseMapping(url, create=True) as db_map:
self._assert_item_added(db_map.add_entity_class_item(name="Object"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="none_none"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="true_none"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="none_true"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="true_true"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="false_true"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="true_false"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="false_false"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="none_false"))
self._assert_item_added(db_map.add_entity_item(class_name="Object", name="false_none"))
self._assert_item_added(db_map.add_scenario_item(name="scenario"))
self._assert_item_added(db_map.add_alternative_item(name="first"))
self._assert_item_added(db_map.add_alternative_item(name="second"))
self._assert_item_added(
db_map.add_scenario_alternative_item(scenario_name="scenario", alternative_name="first", rank=0)
)
self._assert_item_added(
db_map.add_scenario_alternative_item(scenario_name="scenario", alternative_name="second", rank=1)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("true_none",), alternative_name="first", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("none_true",), alternative_name="second", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("true_true",), alternative_name="first", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("true_true",), alternative_name="second", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("false_true",), alternative_name="first", active=False
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("false_true",), alternative_name="second", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("true_false",), alternative_name="first", active=True
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("true_false",), alternative_name="second", active=False
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("false_false",), alternative_name="first", active=False
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("false_false",), alternative_name="second", active=False
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("none_false",), alternative_name="second", active=False
)
)
self._assert_item_added(
db_map.add_entity_alternative_item(
entity_class_name="Object", entity_byname=("false_none",), alternative_name="first", active=False
)
)
db_map.commit_session("Add test data.")

def _assert_item_added(self, result):
self.assertIsNone(result[1])
self.assertIsNotNone(result[0])

def test_execution(self):
this_file = Path(__file__)
completed = subprocess.run((sys.executable, "-m", "spinetoolbox", "--execute-only", str(this_file.parent)))
self.assertEqual(completed.returncode, 0)
self.assertTrue(self._exporter_output_path.exists())
self.assertEqual(len(list(self._exporter_output_path.iterdir())), 1)
output_dir = next(iter(self._exporter_output_path.iterdir()))
filter_id = (output_dir / ".filter_id").read_text(encoding="utf-8").splitlines()
self.assertEqual(len(filter_id), 1)
self.assertEqual(filter_id, ["scenario - Source"])
entities = (output_dir / "entities.csv").read_text(encoding="utf-8").splitlines()
expected = ["Object,none_none", "Object,none_true", "Object,true_none", "Object,true_true", "Object,false_true"]
self.assertCountEqual(entities, expected)


if __name__ == '__main__':
unittest.main()

0 comments on commit c6ba398

Please sign in to comment.