Skip to content

Commit a983b28

Browse files
authored
test: add foreign key tests (#807)
1 parent d2f2e69 commit a983b28

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
on:
2+
push:
3+
branches:
4+
- main
5+
pull_request:
6+
name: foreign-keys-test
7+
jobs:
8+
system-tests:
9+
runs-on: ubuntu-latest
10+
11+
services:
12+
emulator-0:
13+
image: gcr.io/cloud-spanner-emulator/emulator:latest
14+
ports:
15+
- 9010:9010
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v3
20+
- name: Setup Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: 3.8
24+
- name: Run Django foreign key test
25+
run: sh foreign_key_test.sh
26+
env:
27+
SPANNER_EMULATOR_HOST: localhost:9010
28+
GOOGLE_CLOUD_PROJECT: emulator-test-project
29+
GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE: true
30+
SPANNER_TEST_INSTANCE: google-cloud-django-backend-tests

foreign_key_test.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pip install django==3.2
2+
3+
mkdir django_test
4+
cd django_test
5+
6+
django-admin startproject foreign_keys
7+
cd foreign_keys
8+
python manage.py startapp applic
9+
10+
echo "
11+
from django.db import models
12+
13+
14+
class Country(models.Model):
15+
name = models.CharField(max_length=32)
16+
17+
18+
class City(models.Model):
19+
name = models.CharField(max_length=32)
20+
country = models.ForeignKey(Country, on_delete=models.DO_NOTHING)
21+
" > applic/models.py
22+
23+
24+
echo "from django.test import TestCase
25+
from .models import City, Country
26+
27+
28+
class EnqueuedRoutesTest(TestCase):
29+
def setUp(self):
30+
self.country = Country.objects.create(name='Country123')
31+
self.city = City.objects.create(name='City123', country=self.country)
32+
33+
def test_foreign_key(self):
34+
city = City.objects.get(pk=1)
35+
assert city.country == Country.objects.get(pk=1)" > applic/tests.py
36+
37+
sed -i -- 's/INSTALLED_APPS = \[/INSTALLED_APPS = \[\"applic\.apps\.ApplicConfig\"\,/g' foreign_keys/settings.py
38+
39+
python manage.py makemigrations
40+
python manage.py migrate
41+
42+
python manage.py test

0 commit comments

Comments
 (0)