Skip to content

Commit 92f259b

Browse files
committed
Document testing views
1 parent ed028da commit 92f259b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/md/testing-views.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Testing views
2+
3+
> [!NOTE]
4+
> This page assumes familiarity with writing automated tests in Python.
5+
6+
When writing tests for Janeway views, rely on the Django test client (`django.test.Client`) unless the nature of your test requires dealing with requests objects explicitly.
7+
8+
Here are tips for avoiding common pitfalls and saving debugging time.
9+
10+
## Use domain mode explicitly
11+
12+
To call the Django test client, you have to write out the URL. But in Janeway, URLs are interpreted differently based on the `URL_CONFIG` setting. `URL_CONFIG` can be set to `"path"` or `"domain"`. In most cases, it is best to set `"domain"` using a setting override in a decorator, so you do not have to write out the journal code or repository short name in the URL.
13+
14+
```py
15+
from django.test import override_settings
16+
17+
@override_settings(URL_CONFIG="domain")
18+
def my_test(self):
19+
pass
20+
```
21+
22+
## Name the test server
23+
24+
When Janeway is using domain mode, the domain of the website you are testing (press, journal, repository) becomes important. So it is a good idea to explicitly pass the name of the target domain as `SERVER_NAME` when calling the client.
25+
26+
```py
27+
response = self.client.get(
28+
"/profile/",
29+
SERVER_NAME="www.example.org",
30+
)
31+
```
32+
33+
## When to clear the "script prefix"
34+
35+
In path mode, Janeway makes use of `django.urls.base.set_script_prefix` to insert the journal code into request URLs. However, this path prefix does not go away when another request is made in domain mode. You have to manually clear it by using `clear_script_prefix` in the `tearDown` method of any test case where path mode is used.
36+
37+
```py
38+
from django.urls.base import clear_script_prefix
39+
40+
def tearDown(self):
41+
clear_script_prefix()
42+
```
43+
44+
If the offending test cannot be found, you can remove the prefix in the `setUp` of any affected test cases.
45+
46+
```py
47+
from django.urls.base import clear_script_prefix
48+
49+
def setUp(self):
50+
clear_script_prefix()
51+
```
52+
53+
## How to clear the cache
54+
55+
Sometimes a test is affected by cached data. If needed you can clear the main
56+
cache in your setup method or the main test:
57+
58+
```py
59+
from utils.shared import clear_cache
60+
61+
clear_cache()
62+
```

0 commit comments

Comments
 (0)