55from click .testing import CliRunner
66from onegov .core .utils import module_path
77from onegov .event .cli import cli
8+ from onegov .event .models import Event
89from os import path
910from unittest .mock import MagicMock
1011from unittest .mock import patch
1112
1213
14+ from typing import TYPE_CHECKING
15+ if TYPE_CHECKING :
16+ from onegov .core .orm import SessionManager
17+
18+
1319def test_import_ical (cfg_path : str , temporary_directory : str ) -> None :
1420 runner = CliRunner ()
1521
@@ -140,6 +146,7 @@ def test_import_ical(cfg_path: str, temporary_directory: str) -> None:
140146def test_import_guidle (
141147 cfg_path : str ,
142148 temporary_directory : str ,
149+ session_manager : SessionManager ,
143150 xml : str
144151) -> None :
145152
@@ -157,7 +164,7 @@ def test_import_guidle(
157164 ])
158165 assert result .exit_code == 0
159166 assert "Events successfully imported" in result .output
160- assert "4 added, 0 updated, 0 deleted" in result .output
167+ assert "6 added, 0 updated, 0 deleted" in result .output
161168
162169 # Reimport, not changed due to last_update
163170 with patch ('onegov.event.cli.niquests.get' , return_value = response ):
@@ -190,10 +197,14 @@ def test_import_guidle(
190197 ], 'y' )
191198 assert result .exit_code == 0
192199
193- # Create tagmap
200+ # Create tagmap, mapping 'Konzert Pop / Rock / Jazz' to two tags
194201 tagmap = path .join (temporary_directory , 'tagmap.csv' )
195202 with open (tagmap , 'w' ) as f :
196- f .write ("Konzert Pop / Rock / Jazz,Konzert\n Sport,Sport" )
203+ f .write (
204+ "Konzert Pop / Rock / Jazz,Konzert\n "
205+ "Konzert Pop / Rock / Jazz,Musik\n "
206+ "Sport,Sport"
207+ )
197208
198209 # Re-import with tagmap
199210 with patch ('onegov.event.cli.niquests.get' , return_value = response ):
@@ -205,8 +216,31 @@ def test_import_guidle(
205216 ])
206217 assert result .exit_code == 0
207218 assert "Events successfully imported" in result .output
208- assert "Tags not in tagmap: \" Kulinarik\" !"
209- assert "4 added, 0 updated, 0 deleted" in result .output
219+ assert "Tags not in tagmap:" in result .output
220+ assert '"Kulinarik"' in result .output
221+ assert '"Theater"' in result .output
222+ assert "6 added, 0 updated, 0 deleted" in result .output
223+
224+ # 'Konzert Pop / Rock / Jazz' maps to two tags, both of which are applied,
225+ # while the unmapped 'Kulinarik' is dropped without dropping the event
226+ session_manager .set_current_schema ('foo-bar' )
227+ events = session_manager .session ().query (Event ).all ()
228+ assert len (events ) == 6
229+
230+ tagged = [e for e in events if e .tags ]
231+ untagged = [e for e in events if not e .tags ]
232+
233+ # the four schedules of the offer with a mapped tag keep both target tags
234+ assert len (tagged ) == 4
235+ assert all (sorted (e .tags ) == ['Konzert' , 'Musik' ] for e in tagged )
236+
237+ # the offer without any tags and the offer whose only tag is not in the
238+ # tagmap are both imported anyway, just without any tags
239+ assert len (untagged ) == 2
240+ assert {e .title for e in untagged } == {
241+ 'Vortrag ohne Tags' ,
242+ 'Lesung mit unbekanntem Tag' ,
243+ }
210244
211245
212246@pytest .mark .parametrize ("xml" , [
0 commit comments