Skip to content

Commit

Permalink
fix: only insert types history if it's not empty (#128)
Browse files Browse the repository at this point in the history
closes #127
  • Loading branch information
lchen-2101 authored Mar 19, 2024
1 parent 0f4b8a4 commit 1da51f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/regtech_user_fi_management/entities/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def _insert_history(
hist["changeset"] = changes
types = [t.as_db_dict() for t in target.sbl_institution_types]
connection.execute(fi_history.insert().values(hist))
connection.execute(mapping_history.insert().values(types))
if types:
connection.execute(mapping_history.insert().values(types))

return _insert_history

Expand Down
18 changes: 18 additions & 0 deletions tests/entities/test_listeners.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
import pytest
from unittest.mock import Mock, call
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -66,6 +67,23 @@ def test_fi_history_listener(self, mocker: MockerFixture):
self.fi_history.insert.assert_called_once()
self.mapping_history.insert.assert_called_once()

def test_fi_history_listener_no_types(self, mocker: MockerFixture):
inspect_mock = mocker.patch("regtech_user_fi_management.entities.listeners.inspect")
attr_mock1: AttributeState = Mock(AttributeState)
attr_mock1.key = "name"
attr_mock2: AttributeState = Mock(AttributeState)
attr_mock2.key = "event_time"
state_mock: InstanceState = Mock(InstanceState)
state_mock.attrs = [attr_mock1, attr_mock2]
inspect_mock.return_value = state_mock
fi_listener = _setup_fi_history(self.fi_history, self.mapping_history)
no_types = deepcopy(self.target)
no_types.sbl_institution_types = []
fi_listener(self.mapper, self.connection, no_types)
inspect_mock.assert_called_once_with(no_types)
self.fi_history.insert.assert_called_once()
self.mapping_history.insert.assert_not_called()

def _get_fi_inspect_mock(self):
fi_attr_mock: AttributeState = Mock(AttributeState)
fi_attr_mock.key = "sbl_institution_types"
Expand Down

0 comments on commit 1da51f5

Please sign in to comment.