Skip to content

Commit 854a9ee

Browse files
committed
[tests] Updated tests for caching in estimate location
Signed-off-by: DragnEmperor <[email protected]>
1 parent 49d8689 commit 854a9ee

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

openwisp_controller/config/whois/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ def trigger_whois_lookup(self):
143143
new_ip_address=new_ip,
144144
)
145145
)
146-
# `add_existing` is `True` to handle the case when WHOIS already exists
147-
# as in that case WHOIS lookup is not triggered but we still need to
146+
# To handle the case when WHOIS already exists as in that case
147+
# WHOIS lookup is not triggered but we still need to
148148
# manage estimated locations.
149149
elif self._need_estimated_location_management(new_ip):
150150
transaction.on_commit(

openwisp_controller/config/whois/tests_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ def _task_called(self, mocked_task, task_name="WHOIS lookup"):
7070
with self.subTest(
7171
f"{task_name} task called when last_ip is changed and is public"
7272
):
73-
with mock.patch("django.core.cache.cache.get") as mocked_get:
73+
with mock.patch("django.core.cache.cache.get") as mocked_get, mock.patch(
74+
"django.core.cache.cache.set"
75+
) as mocked_set:
7476
device.last_ip = "172.217.22.10"
7577
device.save()
7678
mocked_task.assert_called()
77-
# The cache `get` is called twice, once for `whois_enabled` and
78-
# once for `estimated_location_enabled`
79-
mocked_get.assert_called()
79+
mocked_set.assert_not_called()
80+
mocked_get.assert_called_once()
8081
mocked_task.reset_mock()
8182

8283
with self.subTest(f"{task_name} task not called when last_ip is private"):

openwisp_controller/geo/estimated_location/test_estimated_location.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,20 @@ def test_estimated_location_task_called(
135135
with self.subTest(
136136
"Estimated location task called when last_ip has related WhoIsInfo"
137137
):
138-
device.organization.config_settings.whois_enabled = True
139-
device.organization.config_settings.estimated_location_enabled = True
140-
device.organization.config_settings.save()
141-
device.last_ip = "172.217.22.14"
142-
self._create_whois_info(ip_address=device.last_ip)
143-
device.save()
144-
mocked_estimated_location_task.assert_called()
138+
with mock.patch("django.core.cache.cache.get") as mocked_get, mock.patch(
139+
"django.core.cache.cache.set"
140+
) as mocked_set:
141+
device.organization.config_settings.whois_enabled = True
142+
device.organization.config_settings.estimated_location_enabled = True
143+
device.organization.config_settings.save()
144+
device.last_ip = "172.217.22.14"
145+
self._create_whois_info(ip_address=device.last_ip)
146+
device.save()
147+
mocked_set.assert_not_called()
148+
# The cache `get` is called twice, once for `whois_enabled` and
149+
# once for `estimated_location_enabled`
150+
mocked_get.assert_called()
151+
mocked_estimated_location_task.assert_called()
145152
mocked_estimated_location_task.reset_mock()
146153

147154
with self.subTest(

0 commit comments

Comments
 (0)