Skip to content

Commit 4f50c63

Browse files
TrangPhamThu Trang Pham
andauthored
WIP: 2021 02 24 dockerize (#18)
* Use cache for most fields and admin form for m2m files * MR comments/clean up * Cache should obey exclude and fields * Some more tests and docs * Only use cache for image files * Even more tests and handle save as new * fix test * More tests * minor refactor * Improve test coverage * Adding tests for fieldsets * Added cache timeout * Added another test for an edge case * Fix issue with ManagementForm tampered with * Update cache to only set when form is_multipart * Even more testing changes * Dockerize * Setting up tests in docker * Update github actions * Got first integration test to work * Refactor a bit * Fix github action yml * Use docker-compose up -d in github actions * combine coveralls * Updated readme * Clean up code * Remove dup code from rebase Co-authored-by: Thu Trang Pham <thu@joinmodernhealth.com>
1 parent 06d3e1a commit 4f50c63

File tree

13 files changed

+182
-30
lines changed

13 files changed

+182
-30
lines changed

.dockerignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
__pycache__/
2+
*.py[cod]
3+
4+
build/
5+
dist/
6+
sdist/
7+
.eggs/
8+
*.egg-info/
9+
.DS_Store
10+
11+
# Editor settings
12+
.vscode/
13+
14+
# Unit test / coverage reports
15+
htmlcov/
16+
.tox/
17+
.coverage
18+
.cache
19+
nosetests.xml
20+
coverage.xml
21+
22+
# Translations
23+
*.mo
24+
*.pot
25+
26+
# Django stuff:
27+
*.log
28+
*.db
29+
30+
# Sphinx documentation
31+
docs/_build/
32+
33+
# pycharm
34+
.idea/
35+
36+
tmp/

.github/workflows/test.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,24 @@ jobs:
4949
parallel: true
5050
flag-name: Unit Test
5151

52+
integration-test:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v2
56+
- name: Build Docker
57+
run: docker-compose build
58+
- name: Start Docker
59+
run: docker-compose up -d
60+
- name: Integration Test
61+
run: docker-compose run web make test-all
62+
- name: Coveralls
63+
uses: AndreMiras/coveralls-python-action@develop
64+
with:
65+
parallel: true
66+
flag-name: Integration Test
67+
5268
coveralls:
53-
needs: test
69+
needs: [test, integration-test]
5470
runs-on: ubuntu-latest
5571
steps:
5672
- name: Coveralls Finished

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
django-admin-confirm-3.8
1+
django-admin-confirm-3.8.0

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3
2+
ENV PYTHONUNBUFFERED=1
3+
ENV USE_DOCKER=true
4+
WORKDIR /code
5+
COPY requirements.txt /code/
6+
RUN pip install -r requirements.txt
7+
COPY . /code/

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ run:
22
./tests/manage.py runserver
33

44
test:
5+
coverage run --source admin_confirm --branch -m pytest --ignore=admin_confirm/tests/integration
6+
coverage report -m
7+
8+
test-all:
59
coverage run --source admin_confirm --branch -m pytest
610
coverage report -m
711

12+
t:
13+
python -m pytest --last-failed -x
14+
815
check-readme:
916
python -m readme_renderer README.md -o /tmp/README.html
1017

README.md

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
AdminConfirmMixin is a mixin for ModelAdmin to add confirmations to change, add and actions.
77

8-
![Screenshot of Change Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot.png)
8+
![Screenshot of Change Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/302e02b1e483fd41e9a6f0b6803b45cd34c866cf/screenshot.png)
99

10-
![Screenshot of Add Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot_confirm_add.png)
10+
![Screenshot of Add Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/302e02b1e483fd41e9a6f0b6803b45cd34c866cf/screenshot_confirm_add.png)
1111

12-
![Screenshot of Action Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot_confirm_action.png)
12+
![Screenshot of Action Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/302e02b1e483fd41e9a6f0b6803b45cd34c866cf/screenshot_confirm_action.png)
1313

1414
It can be configured to add a confirmation page on ModelAdmin upon:
1515

@@ -139,18 +139,21 @@ Your appreciation is also very welcome :) Feel free to:
139139

140140
### Local Development Setup
141141

142+
**Local:**
143+
_You can skip this and just use docker if you want_
144+
142145
Install pyenv
143-
Install python 3.8
146+
pyenv install 3.8.0
144147

145-
Create virtualenv via pyenv
148+
Create **virtualenv** via pyenv
146149

147150
```
148-
pyenv vituralenv 3.8 django-admin-confirm-3.8
151+
pyenv vituralenv 3.8.0 django-admin-confirm-3.8.0
149152
```
150153

151-
Now your terminal should have `(django-admin-confirm-3.8)` prefix, because `.python-version` should have auto switch your virtual env
154+
Now your terminal should have `(django-admin-confirm-3.8.0)` prefix, because `.python-version` should have auto switch your virtual env
152155

153-
Run migrations and create a superuser and run the server
156+
Run **migrations** and create a superuser and run the server
154157

155158
```
156159
./tests/manage.py migrate
@@ -160,43 +163,78 @@ Run migrations and create a superuser and run the server
160163

161164
You should be able to see the test app at `localhost:8000/admin`
162165

163-
Running tests:
166+
**Running tests:**
164167

168+
```sh
169+
make test # Runs unit tests with coverage locally without integration tests
170+
make test-all # Runs unit tests + integration tests, requires extra setup to run locally
165171
```
166-
make test
167-
```
168172

169-
Testing new changes on test project:
173+
Use `python -m pytest` if you want to pass in arguments
174+
175+
`make t` is a short cut to run without coverage, last-failed, and fail fast
176+
177+
Testing local changes on test project:
170178

171179
```
172180
pip install -e .
173181
make run
174182
```
175183

184+
**Docker:**
185+
186+
Instead of local set-up, you can also use docker.
187+
188+
Install docker-compose (or Docker Desktop which installs this for you)
189+
190+
```
191+
docker-compose build
192+
docker-compose up -d
193+
```
194+
195+
You should now be able to see the app running on `localhost:8000`
196+
197+
If you haven't already done migrations and created a superuser, you'll want to do it here
198+
199+
```
200+
docker-compose exec web tests/manage.py migrate
201+
docker-compose exec web tests/manage.py createsuperuser
202+
```
203+
204+
Running tests in docker:
205+
206+
```
207+
docker-compose exec -T web make test-all
208+
```
209+
210+
The integration tests are set up within docker. I recommend running the integration tests only in docker.
211+
212+
Docker is also set to mirror local folder so that you can edit code/tests and don't have to rebuild to run new code/tests.
213+
176214
### Release process
177215

178216
Honestly this part is just for my reference. But who knows :) maybe we'll have another maintainer in the future.
179217

180218
Run tests, check coverage, check readme
181219

182220
```
183-
make test
221+
docker-compose exec -T web make test-all
184222
make check-readme
185223
```
186224

187225
Update version in `setup.py`
188226

189227
```
190228
make package
191-
make upload-testpypi
229+
make upload-testpypi VERSION=<VERSION>
192230
```
193231

194232
Install new version locally
195233
First you have to uninstall if you used `pip install -e` earlier
196234

197235
```
198236
pip uninstall django_admin_confirm
199-
make install-testpypi
237+
make install-testpypi VERSION=<VERSION>
200238
```
201239

202240
Update version in `requirements.txt`

admin_confirm/templates/admin/change_confirmation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</div>
4949
{% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1">{% endif %}
5050
{% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}">{% endif %}
51-
{% if form.is_multipart %}<input type="hidden" name=CONFIRMATION_RECEIVED value="True">{% endif %}
51+
{% if form.is_multipart %}<input type="hidden" name="_confirmation_received" value="True">{% endif %}
5252
<div class="submit-row">
5353
<input type="submit" value="{% trans 'Yes, I’m sure' %}" name="{{ submit_name }}">
5454
<p class="deletelink-box">

admin_confirm/tests/helpers.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.test import TestCase, RequestFactory
33
from django.contrib.auth.models import User
44

5-
65
class AdminConfirmTestCase(TestCase):
76
"""
87
Helper TestCase class and common associated assertions
@@ -43,7 +42,7 @@ def _assertSubmitHtml(
4342
self.assertNotIn("_confirm_change", rendered_content)
4443

4544
confirmation_received_html = (
46-
'<input type="hidden" name=CONFIRMATION_RECEIVED value="True">'
45+
'<input type="hidden" name="_confirmation_received" value="True">'
4746
)
4847

4948
if multipart_form:
@@ -56,3 +55,31 @@ def _assertSimpleFieldFormHtml(self, rendered_content, fields):
5655
for k, v in fields.items():
5756
self.assertIn(f'name="{k}"', rendered_content)
5857
self.assertIn(f'value="{v}"', rendered_content)
58+
59+
def _assertFormsetsFormHtml(self, rendered_content, inlines):
60+
for inline in inlines:
61+
for field in inline.fields:
62+
self.assertIn("apple", rendered_content)
63+
64+
65+
66+
import socket
67+
from django.test import LiveServerTestCase
68+
from selenium import webdriver
69+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
70+
71+
72+
class AdminConfirmIntegrationTestCase(LiveServerTestCase):
73+
@classmethod
74+
def setUpClass(cls):
75+
cls.host = socket.gethostbyname(socket.gethostname())
76+
cls.selenium = webdriver.Remote(
77+
command_executor="http://selenium:4444/wd/hub",
78+
desired_capabilities=DesiredCapabilities.FIREFOX,
79+
)
80+
super().setUpClass()
81+
82+
@classmethod
83+
def tearDownClass(cls):
84+
cls.selenium.quit()
85+
super().tearDownClass()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from admin_confirm.tests.helpers import AdminConfirmIntegrationTestCase
2+
3+
4+
class SmokeTest(AdminConfirmIntegrationTestCase):
5+
def test_load_admin(self):
6+
self.selenium.get(self.live_server_url+'/admin/')
7+
self.assertIn('Django', self.selenium.title)

admin_confirm/tests/unit/test_admin_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from tests.market.models import GeneralManager, ShoppingMall, Town
77
from tests.factories import ShopFactory
88

9-
from admin_confirm.constants import CACHE_KEYS, CONFIRMATION_RECEIVED
9+
from admin_confirm.constants import CACHE_KEYS
1010

1111

1212
@mock.patch.object(ShoppingMallAdmin, "inlines", [])

0 commit comments

Comments
 (0)