|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Use of this source code is governed by a BSD-style |
| 4 | +# license that can be found in the LICENSE file or at |
| 5 | +# https://developers.google.com/open-source/licenses/bsd |
| 6 | + |
| 7 | +from .models import Event |
| 8 | +from django.test import TransactionTestCase |
| 9 | +import datetime |
| 10 | +import unittest |
| 11 | +from django.utils import timezone |
| 12 | +from google.api_core.exceptions import OutOfRange |
| 13 | +from django.db import connection |
| 14 | +from django_spanner import USE_EMULATOR |
| 15 | +from tests.system.django_spanner.utils import ( |
| 16 | + setup_instance, |
| 17 | + teardown_instance, |
| 18 | + setup_database, |
| 19 | + teardown_database, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@unittest.skipIf( |
| 24 | + USE_EMULATOR, "Check Constraint is not implemented in emulator." |
| 25 | +) |
| 26 | +class TestCheckConstraint(TransactionTestCase): |
| 27 | + @classmethod |
| 28 | + def setUpClass(cls): |
| 29 | + setup_instance() |
| 30 | + setup_database() |
| 31 | + with connection.schema_editor() as editor: |
| 32 | + # Create the table |
| 33 | + editor.create_model(Event) |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def tearDownClass(cls): |
| 37 | + with connection.schema_editor() as editor: |
| 38 | + # delete the table |
| 39 | + editor.delete_model(Event) |
| 40 | + teardown_database() |
| 41 | + teardown_instance() |
| 42 | + |
| 43 | + def test_insert_valid_value(self): |
| 44 | + """ |
| 45 | + Tests model object creation with Event model. |
| 46 | + """ |
| 47 | + now = timezone.now() |
| 48 | + now_plus_10 = now + datetime.timedelta(minutes=10) |
| 49 | + event_valid = Event(start_date=now, end_date=now_plus_10) |
| 50 | + event_valid.save() |
| 51 | + qs1 = Event.objects.filter().values("start_date") |
| 52 | + self.assertEqual(qs1[0]["start_date"], now) |
| 53 | + # Delete data from Event table. |
| 54 | + Event.objects.all().delete() |
| 55 | + |
| 56 | + def test_insert_invalid_value(self): |
| 57 | + """ |
| 58 | + Tests model object creation with invalid data in Event model. |
| 59 | + """ |
| 60 | + now = timezone.now() |
| 61 | + now_minus_1_day = now - timezone.timedelta(days=1) |
| 62 | + event_invalid = Event(start_date=now, end_date=now_minus_1_day) |
| 63 | + with self.assertRaises(OutOfRange): |
| 64 | + event_invalid.save() |
0 commit comments