-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest_replace_links.py
154 lines (137 loc) · 5.63 KB
/
test_replace_links.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pytest
from django.core.management.base import CommandError
from linkcheck.listeners import enable_listeners
from linkcheck.models import Link, Url
from ..utils import get_command_output
def test_replace_links_missing_args():
"""
Ensure that missing args cause an error
"""
with pytest.raises(CommandError) as exc_info:
assert not any(get_command_output("replace_links"))
assert (
str(exc_info.value)
== "Error: the following arguments are required: search, replace"
)
def test_replace_links_missing_replace():
"""
Ensure that a missing replace throws an error
"""
with pytest.raises(CommandError) as exc_info:
assert not any(get_command_output("replace_links", "search"))
assert str(exc_info.value) == "Error: the following arguments are required: replace"
@pytest.mark.django_db
def test_replace_links_non_existing_region(load_test_data):
"""
Ensure that a non existing region slug throws an error
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:type load_test_data: tuple
"""
with pytest.raises(CommandError) as exc_info:
assert not any(
get_command_output(
"replace_links", "replace_links", "search", "--region-slug=non-existing"
)
)
assert str(exc_info.value) == 'Region with slug "non-existing" does not exist.'
@pytest.mark.django_db
def test_replace_links_non_existing_username(load_test_data):
"""
Ensure that a non existing username throws an error
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:type load_test_data: tuple
"""
with pytest.raises(CommandError) as exc_info:
assert not any(
get_command_output(
"replace_links", "replace_links", "search", "--username=non-existing"
)
)
assert str(exc_info.value) == 'User with username "non-existing" does not exist.'
@pytest.mark.order("last")
@pytest.mark.django_db(transaction=True, serialized_rollback=True)
def test_replace_links_dry_run(load_test_data_transactional):
"""
Ensure that dry run works as expected
:param load_test_data_transactional: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data_transactional`)
:type load_test_data_transactional: tuple
"""
test_url = "https://integreat.app/augsburg/de/willkommen/"
search = "/augsburg/"
replace = "/new-slug/"
replaced_url = test_url.replace(search, replace)
assert Url.objects.filter(
url=test_url
).exists(), "Test URL should exist in test data"
assert (
Link.objects.filter(url__url=test_url).count() == 1
), "Test link should exist in test data"
assert not Url.objects.filter(
url=replaced_url
).exists(), "Replaced URL should not exist in test data"
assert not Link.objects.filter(
url__url=replaced_url
).exists(), "Replaced link should not exist in test data"
# Test dry run without --commit
with enable_listeners():
out, err = get_command_output("replace_links", search, replace)
assert (
f'✔ Finished dry-run of replacing "{search}" with "{replace}" in content links.'
in out
)
assert not err
assert Url.objects.filter(
url=test_url
).exists(), "Test URL should not be removed during dry run"
assert (
Link.objects.filter(url__url=test_url).count() == 1
), "Test link should not be removed during dry run"
assert not Url.objects.filter(
url=replaced_url
).exists(), "Replaced URL should not be created during dry run"
assert not Link.objects.filter(
url__url=replaced_url
).exists(), "Replaced link should not be created during dry run"
@pytest.mark.order("last")
@pytest.mark.django_db(transaction=True, serialized_rollback=True)
def test_replace_links_commit(load_test_data_transactional):
"""
Ensure that committing changes to the database works as expected
:param load_test_data_transactional: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data_transactional`)
:type load_test_data_transactional: tuple
"""
test_url = "https://integreat.app/augsburg/de/willkommen/"
search = "/augsburg/"
replace = "/new-slug/"
replaced_url = test_url.replace(search, replace)
assert Url.objects.filter(
url=test_url
).exists(), "Test URL should not be removed during dry run"
assert (
Link.objects.filter(url__url=test_url).count() == 1
), "Test link should not be removed during dry run"
assert not Url.objects.filter(
url=replaced_url
).exists(), "Replaced URL should not be created during dry run"
assert not Link.objects.filter(
url__url=replaced_url
).exists(), "Replaced link should not be created during dry run"
# Now pass --commit to write changes to database
with enable_listeners():
out, err = get_command_output("replace_links", search, replace, "--commit")
assert (
f'✔ Successfully replaced "{search}" with "{replace}" in content links.' in out
)
assert not err
assert not Url.objects.filter(
url=test_url
).exists(), "Test URL should not exist after replacement"
assert not Link.objects.filter(
url__url=test_url
).exists(), "Test link should not exist after replacement"
assert Url.objects.filter(
url=replaced_url
).exists(), "Replaced URL should exist after replacement"
assert (
Link.objects.filter(url__url=replaced_url).count() == 1
), "Replaced link should exist after replacement"