Skip to content

Commit 2bc61a2

Browse files
author
chenyumic
authored
Added tests for Django samples (GoogleCloudPlatform#4086)
* Added tests for Django samples * Use a different env var * Minor fixes * Use pytest.ini instead for the App Engine standard (2.7) test
1 parent 0221d37 commit 2bc61a2

File tree

18 files changed

+289
-13
lines changed

18 files changed

+289
-13
lines changed

appengine/flexible/django_cloudsql/mysite/settings.py

+9
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@
116116
DATABASES['default']['HOST'] = '127.0.0.1'
117117
# [END dbconfig]
118118

119+
# Use a in-memory sqlite3 database when testing in CI systems
120+
if os.getenv('TRAMPOLINE_CI', None):
121+
DATABASES = {
122+
'default': {
123+
'ENGINE': 'django.db.backends.sqlite3',
124+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
125+
}
126+
}
127+
119128
# Internationalization
120129
# https://docs.djangoproject.com/en/1.8/topics/i18n/
121130

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
'ignored_versions': ["2.7"],
26+
27+
# An envvar key for determining the project id to use. Change it
28+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
29+
# build specific Cloud project. You can also use your own string
30+
# to use your own Cloud project.
31+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
32+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
33+
34+
# A dictionary you want to inject into your test. Don't put any
35+
# secrets here. These values will override predefined values.
36+
'envs': {
37+
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
38+
},
39+
}

appengine/standard/django/polls/tests.py renamed to appengine/flexible/django_cloudsql/polls/test_polls.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015 Google Inc. All rights reserved.
1+
# Copyright 2020 Google LLC. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Uncomment these imports and add tests here
15+
from django.test import Client, TestCase # noqa: 401
1616

17-
# from django import http
18-
# from django.test import TestCase
1917

20-
# from . import views
18+
class PollViewTests(TestCase):
19+
def test_index_view(self):
20+
response = self.client.get('/')
21+
assert response.status_code == 200
22+
assert 'Hello, world' in str(response.content)
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==5.3.2
2+
pytest-django==3.9.0

appengine/standard/django/mysite/settings.py

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@
134134
}
135135
# [END db_setup]
136136

137+
# Use a in-memory sqlite3 database when testing in CI systems
138+
if os.getenv('TRAMPOLINE_CI', None):
139+
DATABASES = {
140+
'default': {
141+
'ENGINE': 'django.db.backends.sqlite3',
142+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
143+
}
144+
}
145+
137146
# Internationalization
138147
# https://docs.djangoproject.com/en/1.8/topics/i18n/
139148

appengine/flexible/django_cloudsql/polls/tests.py renamed to appengine/standard/django/polls/test_polls.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015 Google Inc.
1+
# Copyright 2020 Google LLC. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,4 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Create your tests here.
15+
from django.test import Client, TestCase # noqa: 401
16+
17+
18+
class PollViewTests(TestCase):
19+
def test_index_view(self):
20+
response = self.client.get('/')
21+
assert response.status_code == 200
22+
assert 'Hello, world' in str(response.content)

appengine/standard/django/pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
DJANGO_SETTINGS_MODULE=mysite.settings
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==4.6.9
2+
pytest-django==3.9.0

appengine/standard_python3/django/mysite/settings.py

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@
118118
}
119119
# [END db_setup]
120120

121+
# Use a in-memory sqlite3 database when testing in CI systems
122+
if os.getenv('TRAMPOLINE_CI', None):
123+
DATABASES = {
124+
'default': {
125+
'ENGINE': 'django.db.backends.sqlite3',
126+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
127+
}
128+
}
121129

122130
# Password validation
123131
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
'ignored_versions': ["2.7"],
26+
27+
# An envvar key for determining the project id to use. Change it
28+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
29+
# build specific Cloud project. You can also use your own string
30+
# to use your own Cloud project.
31+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
32+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
33+
34+
# A dictionary you want to inject into your test. Don't put any
35+
# secrets here. These values will override predefined values.
36+
'envs': {
37+
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
38+
},
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2020 Google LLC. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.test import Client, TestCase # noqa: 401
16+
from django.urls import reverse
17+
from django.utils import timezone
18+
19+
from .models import Choice, Question
20+
21+
22+
class PollViewTests(TestCase):
23+
def setUp(self):
24+
question = Question(
25+
question_text="This is a test question",
26+
pub_date=timezone.now()
27+
)
28+
question.save()
29+
self.question = question
30+
31+
choice = Choice(
32+
choice_text="This is a test choice",
33+
votes=0
34+
)
35+
choice.question = question
36+
choice.save()
37+
self.choice = choice
38+
39+
self.client = Client()
40+
41+
def test_index_view(self):
42+
response = self.client.get('/')
43+
assert response.status_code == 200
44+
assert self.question.question_text in str(response.content)
45+
46+
def test_detail_view(self):
47+
response = self.client.get(
48+
reverse('polls:detail', args=(self.question.id,)))
49+
assert response.status_code == 200
50+
assert self.question.question_text in str(response.content)
51+
assert self.choice.choice_text in str(response.content)
52+
53+
def test_results_view(self):
54+
response = self.client.get(
55+
reverse('polls:results', args=(self.question.id,)))
56+
assert response.status_code == 200
57+
assert self.question.question_text in str(response.content)
58+
assert self.choice.choice_text in str(response.content)

appengine/standard_python3/django/polls/tests.py

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==5.3.2
2+
pytest-django==3.9.0

appengine/standard_python37/django/mysite/settings.py

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@
118118
}
119119
# [END db_setup]
120120

121+
# Use a in-memory sqlite3 database when testing in CI systems
122+
if os.getenv('TRAMPOLINE_CI', None):
123+
DATABASES = {
124+
'default': {
125+
'ENGINE': 'django.db.backends.sqlite3',
126+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
127+
}
128+
}
121129

122130
# Password validation
123131
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
'ignored_versions': ["2.7"],
26+
27+
# An envvar key for determining the project id to use. Change it
28+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
29+
# build specific Cloud project. You can also use your own string
30+
# to use your own Cloud project.
31+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
32+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
33+
34+
# A dictionary you want to inject into your test. Don't put any
35+
# secrets here. These values will override predefined values.
36+
'envs': {
37+
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
38+
},
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2020 Google LLC. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.test import Client, TestCase # noqa: 401
16+
from django.urls import reverse
17+
from django.utils import timezone
18+
19+
from .models import Choice, Question
20+
21+
22+
class PollViewTests(TestCase):
23+
def setUp(self):
24+
question = Question(
25+
question_text="This is a test question",
26+
pub_date=timezone.now()
27+
)
28+
question.save()
29+
self.question = question
30+
31+
choice = Choice(
32+
choice_text="This is a test choice",
33+
votes=0
34+
)
35+
choice.question = question
36+
choice.save()
37+
self.choice = choice
38+
39+
self.client = Client()
40+
41+
def test_index_view(self):
42+
response = self.client.get('/')
43+
assert response.status_code == 200
44+
assert self.question.question_text in str(response.content)
45+
46+
def test_detail_view(self):
47+
response = self.client.get(
48+
reverse('polls:detail', args=(self.question.id,)))
49+
assert response.status_code == 200
50+
assert self.question.question_text in str(response.content)
51+
assert self.choice.choice_text in str(response.content)
52+
53+
def test_results_view(self):
54+
response = self.client.get(
55+
reverse('polls:results', args=(self.question.id,)))
56+
assert response.status_code == 200
57+
assert self.question.question_text in str(response.content)
58+
assert self.choice.choice_text in str(response.content)

appengine/standard_python37/django/polls/tests.py

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==5.3.2
2+
pytest-django==3.9.0

0 commit comments

Comments
 (0)