Skip to content

Commit

Permalink
[fix] Fixed test_get_or_create_integrity_error test
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Oct 6, 2023
1 parent acb16bf commit 3cf28cc
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions openwisp_monitoring/monitoring/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,22 @@ def test_get_or_create_renamed_object(self):
self.assertEqual(m2.name, m.name)
self.assertFalse(created)

@patch.object(
Metric.objects,
'get',
side_effect=[
Metric.DoesNotExist,
Metric(name='lan', configuration='test_metric'),
],
)
@patch.object(Metric, 'save', side_effect=IntegrityError)
def test_get_or_create_integrity_error(self, mocked_save, mocked_get):
metric, _ = Metric._get_or_create(name='lan', configuration='test_metric')
mocked_save.assert_called_once()
self.assertEqual(mocked_get.call_count, 2)
self.assertEqual(metric.name, 'lan')
self.assertEqual(metric.configuration, 'test_metric')
def test_get_or_create_integrity_error(self):
with patch.object(
Metric.objects,
'get',
side_effect=[
Metric.DoesNotExist,
Metric(name='lan', configuration='test_metric'),
],
) as mocked_get, patch.object(
Metric, 'save', side_effect=IntegrityError
) as mocked_save:
metric, _ = Metric._get_or_create(name='lan', configuration='test_metric')
mocked_save.assert_called_once()
self.assertEqual(mocked_get.call_count, 2)
self.assertEqual(metric.name, 'lan')
self.assertEqual(metric.configuration, 'test_metric')

def test_metric_write_wrong_related_fields(self):
m = self._create_general_metric(name='ping', configuration='ping')
Expand Down

0 comments on commit 3cf28cc

Please sign in to comment.