diff --git a/appengine/flexible/django_cloudsql/mysite/settings.py b/appengine/flexible/django_cloudsql/mysite/settings.py index 7bded4a47c38..c124f3cc1879 100644 --- a/appengine/flexible/django_cloudsql/mysite/settings.py +++ b/appengine/flexible/django_cloudsql/mysite/settings.py @@ -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/ diff --git a/appengine/flexible/django_cloudsql/noxfile_config.py b/appengine/flexible/django_cloudsql/noxfile_config.py new file mode 100644 index 000000000000..546da53ddec7 --- /dev/null +++ b/appengine/flexible/django_cloudsql/noxfile_config.py @@ -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' + }, +} diff --git a/appengine/standard/django/polls/tests.py b/appengine/flexible/django_cloudsql/polls/test_polls.py similarity index 64% rename from appengine/standard/django/polls/tests.py rename to appengine/flexible/django_cloudsql/polls/test_polls.py index c3b029bad973..4588d730242c 100644 --- a/appengine/standard/django/polls/tests.py +++ b/appengine/flexible/django_cloudsql/polls/test_polls.py @@ -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. @@ -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) diff --git a/appengine/flexible/django_cloudsql/requirements-test.txt b/appengine/flexible/django_cloudsql/requirements-test.txt index 781d4326c947..622cd175afe1 100644 --- a/appengine/flexible/django_cloudsql/requirements-test.txt +++ b/appengine/flexible/django_cloudsql/requirements-test.txt @@ -1 +1,2 @@ pytest==5.3.2 +pytest-django==3.9.0 diff --git a/appengine/standard/django/mysite/settings.py b/appengine/standard/django/mysite/settings.py index 58149c6cabf7..5a4dcbdfcabc 100644 --- a/appengine/standard/django/mysite/settings.py +++ b/appengine/standard/django/mysite/settings.py @@ -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/ diff --git a/appengine/flexible/django_cloudsql/polls/tests.py b/appengine/standard/django/polls/test_polls.py similarity index 64% rename from appengine/flexible/django_cloudsql/polls/tests.py rename to appengine/standard/django/polls/test_polls.py index 052a900bd6f8..4588d730242c 100644 --- a/appengine/flexible/django_cloudsql/polls/tests.py +++ b/appengine/standard/django/polls/test_polls.py @@ -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. @@ -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) diff --git a/appengine/standard/django/pytest.ini b/appengine/standard/django/pytest.ini new file mode 100644 index 000000000000..89b181d6952d --- /dev/null +++ b/appengine/standard/django/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +DJANGO_SETTINGS_MODULE=mysite.settings \ No newline at end of file diff --git a/appengine/standard/django/requirements-test.txt b/appengine/standard/django/requirements-test.txt index 42999750563c..f0d229cb671a 100644 --- a/appengine/standard/django/requirements-test.txt +++ b/appengine/standard/django/requirements-test.txt @@ -1 +1,2 @@ pytest==4.6.9 +pytest-django==3.9.0 diff --git a/appengine/standard_python3/django/mysite/settings.py b/appengine/standard_python3/django/mysite/settings.py index 3ff8446771f8..f7ac261da887 100644 --- a/appengine/standard_python3/django/mysite/settings.py +++ b/appengine/standard_python3/django/mysite/settings.py @@ -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 diff --git a/appengine/standard_python3/django/noxfile_config.py b/appengine/standard_python3/django/noxfile_config.py new file mode 100644 index 000000000000..546da53ddec7 --- /dev/null +++ b/appengine/standard_python3/django/noxfile_config.py @@ -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' + }, +} diff --git a/appengine/standard_python3/django/polls/test_polls.py b/appengine/standard_python3/django/polls/test_polls.py new file mode 100644 index 000000000000..2ef9121899e5 --- /dev/null +++ b/appengine/standard_python3/django/polls/test_polls.py @@ -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) diff --git a/appengine/standard_python3/django/polls/tests.py b/appengine/standard_python3/django/polls/tests.py deleted file mode 100644 index 1848508267c1..000000000000 --- a/appengine/standard_python3/django/polls/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase # noqa: 401 - -# Create your tests here. diff --git a/appengine/standard_python3/django/requirements-test.txt b/appengine/standard_python3/django/requirements-test.txt index 781d4326c947..622cd175afe1 100644 --- a/appengine/standard_python3/django/requirements-test.txt +++ b/appengine/standard_python3/django/requirements-test.txt @@ -1 +1,2 @@ pytest==5.3.2 +pytest-django==3.9.0 diff --git a/appengine/standard_python37/django/mysite/settings.py b/appengine/standard_python37/django/mysite/settings.py index 3ff8446771f8..f7ac261da887 100644 --- a/appengine/standard_python37/django/mysite/settings.py +++ b/appengine/standard_python37/django/mysite/settings.py @@ -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 diff --git a/appengine/standard_python37/django/noxfile_config.py b/appengine/standard_python37/django/noxfile_config.py new file mode 100644 index 000000000000..546da53ddec7 --- /dev/null +++ b/appengine/standard_python37/django/noxfile_config.py @@ -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' + }, +} diff --git a/appengine/standard_python37/django/polls/test_polls.py b/appengine/standard_python37/django/polls/test_polls.py new file mode 100644 index 000000000000..2ef9121899e5 --- /dev/null +++ b/appengine/standard_python37/django/polls/test_polls.py @@ -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) diff --git a/appengine/standard_python37/django/polls/tests.py b/appengine/standard_python37/django/polls/tests.py deleted file mode 100644 index 1848508267c1..000000000000 --- a/appengine/standard_python37/django/polls/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase # noqa: 401 - -# Create your tests here. diff --git a/appengine/standard_python37/django/requirements-test.txt b/appengine/standard_python37/django/requirements-test.txt index 781d4326c947..622cd175afe1 100644 --- a/appengine/standard_python37/django/requirements-test.txt +++ b/appengine/standard_python37/django/requirements-test.txt @@ -1 +1,2 @@ pytest==5.3.2 +pytest-django==3.9.0