# Path: tests/system/django_spanner/models.py
# class Author(models.Model):
#     first_name = models.CharField(max_length=20)
#     last_name = models.CharField(max_length=20)
#     rating = models.DecimalField()
# 
# class Number(models.Model):
#     num = models.DecimalField()
# 
#     def __str__(self):
#         return str(self.num)
# 
# Path: tests/system/django_spanner/utils.py
# def setup_instance():
#     if USE_EMULATOR:
#         from google.auth.credentials import AnonymousCredentials
# 
#         Config.CLIENT = Client(
#             project=PROJECT_ID, credentials=AnonymousCredentials()
#         )
#     else:
#         Config.CLIENT = Client()
# 
#     retry = RetryErrors(exceptions.ServiceUnavailable)
# 
#     configs = list(retry(Config.CLIENT.list_instance_configs)())
# 
#     instances = retry(_list_instances)()
#     EXISTING_INSTANCES[:] = instances
# 
#     # Delete test instances that are older than an hour.
#     cutoff = int(time.time()) - 1 * 60 * 60
#     instance_pbs = Config.CLIENT.list_instances(
#         "labels.python-spanner-systests:true"
#     )
#     for instance_pb in instance_pbs:
#         instance = Instance.from_pb(instance_pb, Config.CLIENT)
#         if "created" not in instance.labels:
#             continue
#         create_time = int(instance.labels["created"])
#         if create_time > cutoff:
#             continue
#         if not USE_EMULATOR:
#             # Instance cannot be deleted while backups exist.
#             for backup_pb in instance.list_backups():
#                 backup = Backup.from_pb(backup_pb, instance)
#                 backup.delete()
#         instance.delete()
# 
#     if CREATE_INSTANCE:
#         if not USE_EMULATOR:
#             # Defend against back-end returning configs for regions we aren't
#             # actually allowed to use.
#             configs = [config for config in configs if "-us-" in config.name]
# 
#         if not configs:
#             raise ValueError("List instance configs failed in module set up.")
# 
#         Config.INSTANCE_CONFIG = configs[0]
#         config_name = configs[0].name
#         create_time = str(int(time.time()))
#         labels = {"django-spanner-systests": "true", "created": create_time}
# 
#         Config.INSTANCE = Config.CLIENT.instance(
#             INSTANCE_ID, config_name, labels=labels
#         )
#         if not Config.INSTANCE.exists():
#             created_op = Config.INSTANCE.create()
#             created_op.result(
#                 SPANNER_OPERATION_TIMEOUT_IN_SECONDS
#             )  # block until completion
#         else:
#             Config.INSTANCE.reload()
# 
#     else:
#         Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID)
#         Config.INSTANCE.reload()
# 
# def teardown_instance():
#     if CREATE_INSTANCE:
#         Config.INSTANCE.delete()
# 
# def setup_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if not Config.DATABASE.exists():
#         creation = DatabaseCreation(connection)
#         creation._create_test_db(verbosity=0, autoclobber=False, keepdb=True)
# 
#     # Running migrations on the db.
#     call_command("migrate", interactive=False)
# 
# def teardown_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if Config.DATABASE.exists():
#         Config.DATABASE.drop()


from .models import Author, Number
from django.test import TransactionTestCase
from django.db import connection
from decimal import Decimal
from tests.system.django_spanner.utils import (
    setup_instance,
    teardown_instance,
    setup_database,
    teardown_database,
)
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd



class TestDecimal(TransactionTestCase):
    @classmethod
    def setUpClass(cls):
        setup_instance()
        setup_database()
        with connection.schema_editor() as editor:
            # Create the tables

# Path: tests/system/django_spanner/models.py
# class Author(models.Model):
#     first_name = models.CharField(max_length=20)
#     last_name = models.CharField(max_length=20)
#     rating = models.DecimalField()
# 
# class Number(models.Model):
#     num = models.DecimalField()
# 
#     def __str__(self):
#         return str(self.num)
# 
# Path: tests/system/django_spanner/utils.py
# def setup_instance():
#     if USE_EMULATOR:
#         from google.auth.credentials import AnonymousCredentials
# 
#         Config.CLIENT = Client(
#             project=PROJECT_ID, credentials=AnonymousCredentials()
#         )
#     else:
#         Config.CLIENT = Client()
# 
#     retry = RetryErrors(exceptions.ServiceUnavailable)
# 
#     configs = list(retry(Config.CLIENT.list_instance_configs)())
# 
#     instances = retry(_list_instances)()
#     EXISTING_INSTANCES[:] = instances
# 
#     # Delete test instances that are older than an hour.
#     cutoff = int(time.time()) - 1 * 60 * 60
#     instance_pbs = Config.CLIENT.list_instances(
#         "labels.python-spanner-systests:true"
#     )
#     for instance_pb in instance_pbs:
#         instance = Instance.from_pb(instance_pb, Config.CLIENT)
#         if "created" not in instance.labels:
#             continue
#         create_time = int(instance.labels["created"])
#         if create_time > cutoff:
#             continue
#         if not USE_EMULATOR:
#             # Instance cannot be deleted while backups exist.
#             for backup_pb in instance.list_backups():
#                 backup = Backup.from_pb(backup_pb, instance)
#                 backup.delete()
#         instance.delete()
# 
#     if CREATE_INSTANCE:
#         if not USE_EMULATOR:
#             # Defend against back-end returning configs for regions we aren't
#             # actually allowed to use.
#             configs = [config for config in configs if "-us-" in config.name]
# 
#         if not configs:
#             raise ValueError("List instance configs failed in module set up.")
# 
#         Config.INSTANCE_CONFIG = configs[0]
#         config_name = configs[0].name
#         create_time = str(int(time.time()))
#         labels = {"django-spanner-systests": "true", "created": create_time}
# 
#         Config.INSTANCE = Config.CLIENT.instance(
#             INSTANCE_ID, config_name, labels=labels
#         )
#         if not Config.INSTANCE.exists():
#             created_op = Config.INSTANCE.create()
#             created_op.result(
#                 SPANNER_OPERATION_TIMEOUT_IN_SECONDS
#             )  # block until completion
#         else:
#             Config.INSTANCE.reload()
# 
#     else:
#         Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID)
#         Config.INSTANCE.reload()
# 
# def teardown_instance():
#     if CREATE_INSTANCE:
#         Config.INSTANCE.delete()
# 
# def setup_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if not Config.DATABASE.exists():
#         creation = DatabaseCreation(connection)
#         creation._create_test_db(verbosity=0, autoclobber=False, keepdb=True)
# 
#     # Running migrations on the db.
#     call_command("migrate", interactive=False)
# 
# def teardown_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if Config.DATABASE.exists():
#         Config.DATABASE.drop()


# Path: tests/system/django_spanner/test_decimal.py
from .models import Author, Number
from django.test import TransactionTestCase
from django.db import connection
from decimal import Decimal
from tests.system.django_spanner.utils import (
    setup_instance,
    teardown_instance,
    setup_database,
    teardown_database,
)

# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd



class TestDecimal(TransactionTestCase):
    @classmethod
    def setUpClass(cls):
        setup_instance()
        setup_database()
        with connection.schema_editor() as editor:
            # Create the tables

            editor.create_model(Author)