Skip to content

Commit

Permalink
Added tests for Django samples (#4086)
Browse files Browse the repository at this point in the history
* Added tests for Django samples

* Use a different env var

* Minor fixes

* Use pytest.ini instead for the App Engine standard (2.7) test
  • Loading branch information
chenyumic authored Jun 15, 2020
1 parent 0221d37 commit 2bc61a2
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 13 deletions.
9 changes: 9 additions & 0 deletions appengine/flexible/django_cloudsql/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@
DATABASES['default']['HOST'] = '127.0.0.1'
# [END dbconfig]

# Use a in-memory sqlite3 database when testing in CI systems
if os.getenv('TRAMPOLINE_CI', None):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

Expand Down
39 changes: 39 additions & 0 deletions appengine/flexible/django_cloudsql/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be inported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
},
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google Inc. All rights reserved.
# Copyright 2020 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

# from django import http
# from django.test import TestCase

# from . import views
class PollViewTests(TestCase):
def test_index_view(self):
response = self.client.get('/')
assert response.status_code == 200
assert 'Hello, world' in str(response.content)
1 change: 1 addition & 0 deletions appengine/flexible/django_cloudsql/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==5.3.2
pytest-django==3.9.0
9 changes: 9 additions & 0 deletions appengine/standard/django/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@
}
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
if os.getenv('TRAMPOLINE_CI', None):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google Inc.
# Copyright 2020 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,4 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Create your tests here.
from django.test import Client, TestCase # noqa: 401


class PollViewTests(TestCase):
def test_index_view(self):
response = self.client.get('/')
assert response.status_code == 200
assert 'Hello, world' in str(response.content)
2 changes: 2 additions & 0 deletions appengine/standard/django/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
DJANGO_SETTINGS_MODULE=mysite.settings
1 change: 1 addition & 0 deletions appengine/standard/django/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==4.6.9
pytest-django==3.9.0
8 changes: 8 additions & 0 deletions appengine/standard_python3/django/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
}
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
if os.getenv('TRAMPOLINE_CI', None):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
}
}

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Expand Down
39 changes: 39 additions & 0 deletions appengine/standard_python3/django/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be inported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
},
}
58 changes: 58 additions & 0 deletions appengine/standard_python3/django/polls/test_polls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.test import Client, TestCase # noqa: 401
from django.urls import reverse
from django.utils import timezone

from .models import Choice, Question


class PollViewTests(TestCase):
def setUp(self):
question = Question(
question_text="This is a test question",
pub_date=timezone.now()
)
question.save()
self.question = question

choice = Choice(
choice_text="This is a test choice",
votes=0
)
choice.question = question
choice.save()
self.choice = choice

self.client = Client()

def test_index_view(self):
response = self.client.get('/')
assert response.status_code == 200
assert self.question.question_text in str(response.content)

def test_detail_view(self):
response = self.client.get(
reverse('polls:detail', args=(self.question.id,)))
assert response.status_code == 200
assert self.question.question_text in str(response.content)
assert self.choice.choice_text in str(response.content)

def test_results_view(self):
response = self.client.get(
reverse('polls:results', args=(self.question.id,)))
assert response.status_code == 200
assert self.question.question_text in str(response.content)
assert self.choice.choice_text in str(response.content)
3 changes: 0 additions & 3 deletions appengine/standard_python3/django/polls/tests.py

This file was deleted.

1 change: 1 addition & 0 deletions appengine/standard_python3/django/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==5.3.2
pytest-django==3.9.0
8 changes: 8 additions & 0 deletions appengine/standard_python37/django/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
}
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
if os.getenv('TRAMPOLINE_CI', None):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
}
}

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Expand Down
39 changes: 39 additions & 0 deletions appengine/standard_python37/django/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be inported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {
'DJANGO_SETTINGS_MODULE': 'mysite.settings'
},
}
58 changes: 58 additions & 0 deletions appengine/standard_python37/django/polls/test_polls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.test import Client, TestCase # noqa: 401
from django.urls import reverse
from django.utils import timezone

from .models import Choice, Question


class PollViewTests(TestCase):
def setUp(self):
question = Question(
question_text="This is a test question",
pub_date=timezone.now()
)
question.save()
self.question = question

choice = Choice(
choice_text="This is a test choice",
votes=0
)
choice.question = question
choice.save()
self.choice = choice

self.client = Client()

def test_index_view(self):
response = self.client.get('/')
assert response.status_code == 200
assert self.question.question_text in str(response.content)

def test_detail_view(self):
response = self.client.get(
reverse('polls:detail', args=(self.question.id,)))
assert response.status_code == 200
assert self.question.question_text in str(response.content)
assert self.choice.choice_text in str(response.content)

def test_results_view(self):
response = self.client.get(
reverse('polls:results', args=(self.question.id,)))
assert response.status_code == 200
assert self.question.question_text in str(response.content)
assert self.choice.choice_text in str(response.content)
3 changes: 0 additions & 3 deletions appengine/standard_python37/django/polls/tests.py

This file was deleted.

1 change: 1 addition & 0 deletions appengine/standard_python37/django/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==5.3.2
pytest-django==3.9.0

0 comments on commit 2bc61a2

Please sign in to comment.