Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit 0b641bd

Browse files
rnegronjpadilla
authored andcommitted
Starting tests (#43)
* Add failing test suite * Update tests and configurations * Move pytest to dev dependency, add Dockerfile.file and test service
1 parent 321222a commit 0b641bd

File tree

15 files changed

+479
-124
lines changed

15 files changed

+479
-124
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ jobs:
3838
- run:
3939
name: Run flake8
4040
command: pipenv run flake8
41+
- run:
42+
name: Run tests
43+
command: pipenv run pytest

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ force_grid_wrap=0
66
combine_as_imports=True
77
line_length=88
88
skip=.venv,migrations
9-
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytz,requests,rest_framework
9+
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytest,pytz,requests,rest_framework

CONTRIBUTING.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ If you want to go the usual route and run the project locally, though:
8787
- Install pre-commit hooks: `pipenv run pre-commit install`
8888
- Setup environment file: `cp example.env .env`
8989
- Install docker-compose: https://docs.docker.com/compose/install/
90-
- Build and start containers: `docker-compose up -d`
90+
- Build and start containers, including test container: `docker-compose up -d`
9191
- Create super user: `docker-compose exec web python manage.py createsuperuser`
9292
- Scrape some contracts: `docker-compose exec web python manage.py scrape_contracts --limit 100`
9393
- open http://localhost:8000
9494

9595
And you should be ready to go!
9696

97+
You can use this command to run tests on demand: `docker-compose up test`
98+
9799
## Contribute Documentation
98100

99101
Documentation is a super important, critical part of this project. Docs are how we keep track of what we're doing, how, and why. It's how we stay on the same page about our policies. And it's how we tell others everything they need in order to be able to use this project -- or contribute to it. So thank you in advance.
@@ -133,6 +135,7 @@ To contribute code:
133135
* Write tests that verify that your contribution works as expected.
134136
* Write clear, concise commit message(s).
135137
* Dependency updates, additions, or removals must be in individual commits.
138+
* Ensure old tests are still passing.
136139
* Go to https://github.com/TheIndexingProject/contratospr-api/pulls and open a new pull request with your changes.
137140
* If your PR is connected to an open issue, add a line in your PR's description that says `Fixes: #123`, where `#123` is the number of the issue you're fixing.
138141

Dockerfile.local

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM python:3.7
2+
3+
ENV LANG en_US.utf8
4+
5+
RUN pip install pipenv==2018.11.26
6+
7+
WORKDIR /app/
8+
9+
COPY Pipfile Pipfile.lock /app/
10+
11+
# Install application requirements and dev requirements
12+
RUN pipenv install --dev
13+
14+
# Bundle app source
15+
COPY . /app/

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ isort = "==4.3.4"
99
"flake8" = "*"
1010
pre-commit = "*"
1111
seed-isort-config = "*"
12+
pytest-django = "*"
1213

1314
[packages]
1415
dj-database-url = "==0.5.0"

Pipfile.lock

Lines changed: 176 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/docker-entrypoint

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ case "$1" in
3434
./bin/worker --loglevel=DEBUG --concurrency=2
3535
;;
3636

37+
"start-test")
38+
pipenv run pytest
39+
;;
40+
3741
*)
3842
exec "$@"
3943
;;

contratospr/api/tests/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from rest_framework.reverse import reverse
2+
from rest_framework.test import APITestCase
3+
from rest_framework.views import status
4+
5+
6+
class TestViews(APITestCase):
7+
def test_root_view_url(self):
8+
url = reverse("v1:api-root")
9+
response = self.client.get(url)
10+
self.assertEqual(response.status_code, status.HTTP_200_OK)
11+
12+
def test_home_page_view_url(self):
13+
response = self.client.get("/v1/pages/home/")
14+
self.assertEqual(response.status_code, status.HTTP_200_OK)
15+
16+
def test_home_page_view_response(self):
17+
response = self.client.get("/v1/pages/home/")
18+
19+
for key in (
20+
"fiscal_year",
21+
"recent_contracts",
22+
"contractors",
23+
"entities",
24+
"contracts_count",
25+
"contracts_total",
26+
):
27+
self.assertIn(key, response.data)
28+
29+
def test_trends_general_view_url(self):
30+
response = self.client.get("/v1/pages/trends/general/")
31+
self.assertEqual(response.status_code, status.HTTP_200_OK)
32+
33+
def test_test_trends_services_view_url(self):
34+
response = self.client.get("/v1/pages/trends/services/")
35+
self.assertEqual(response.status_code, status.HTTP_200_OK)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from unittest import mock
2+
3+
from django.core.files import File
4+
from rest_framework.reverse import reverse
5+
from rest_framework.test import APITestCase
6+
from rest_framework.views import status
7+
8+
from contratospr.contracts import models
9+
10+
11+
class TestContractViewSet(APITestCase):
12+
def test_viewset_list_url(self):
13+
url = reverse("v1:contract-list")
14+
response = self.client.get(url)
15+
self.assertEqual(response.status_code, status.HTTP_200_OK)
16+
17+
18+
class TestContractorViewSet(APITestCase):
19+
def test_viewset_list_url(self):
20+
url = reverse("v1:contractor-list")
21+
response = self.client.get(url)
22+
self.assertEqual(response.status_code, status.HTTP_200_OK)
23+
24+
25+
class TestDocumentViewSet(APITestCase):
26+
@classmethod
27+
def setUpTestData(cls):
28+
file_mock = mock.MagicMock(spec=File)
29+
file_mock.name = "test_file.pdf"
30+
cls.test_document = models.Document(file=file_mock, source_id=1)
31+
cls.test_document.save()
32+
33+
def test_viewset_detail_url(self):
34+
url = reverse("v1:document-detail", args=[self.test_document.pk])
35+
response = self.client.get(url)
36+
self.assertEqual(response.status_code, status.HTTP_200_OK)
37+
38+
39+
class TestEntityViewSet(APITestCase):
40+
def test_viewset_list_url(self):
41+
url = reverse("v1:entity-list")
42+
response = self.client.get(url)
43+
self.assertEqual(response.status_code, status.HTTP_200_OK)
44+
45+
def test_can_obtain_instance(self):
46+
entity = models.Entity(name="Test Entity", source_id=1)
47+
entity.save()
48+
url = reverse("v1:entity-detail", args=[entity.slug])
49+
response = self.client.get(url)
50+
for key in ("contracts_total", "contracts_count"):
51+
self.assertIn(key, response.data)
52+
53+
54+
class TestServiceGroupViewSet(APITestCase):
55+
def test_viewset_list_url(self):
56+
url = reverse("v1:servicegroup-list")
57+
response = self.client.get(url)
58+
self.assertEqual(response.status_code, status.HTTP_200_OK)
59+
60+
61+
class TestServiceViewSet(APITestCase):
62+
def test_viewset_list_url(self):
63+
url = reverse("v1:service-list")
64+
response = self.client.get(url)
65+
self.assertEqual(response.status_code, status.HTTP_200_OK)

0 commit comments

Comments
 (0)