# Path: create_test_config.py
# def set_test_config(project, instance):
#     config = configparser.ConfigParser()
#     url = (
#         f"spanner+spanner:///projects/{project}/instances/{instance}/"
#         "databases/compliance-test"
#     )
#     config.add_section("db")
#     config["db"]["default"] = url
# 
#     with open("test.cfg", "w") as configfile:
#         config.write(configfile)


import configparser
import os
import time
from create_test_config import set_test_config
from google.api_core.exceptions import AlreadyExists, ResourceExhausted
from google.cloud.spanner_v1 import Client
from google.cloud.spanner_v1.instance import Instance
    from google.auth.credentials import AnonymousCredentials
    configs = list(CLIENT.list_instance_configs())
    if not USE_EMULATOR:
        # Filter out non "us" locations
        configs = [config for config in configs if "-us-" in config.name]

    instance_config = configs[0].name
    create_time = str(int(time.time()))
    unique_resource_id = "%s%d" % ("-", 1000 * time.time())
    instance_id = (
        "sqlalchemy-dialect-test"
        if USE_EMULATOR
        else "sqlalchemy-test" + unique_resource_id
    )
    labels = {"python-spanner-sqlalchemy-systest": "true", "created": create_time}

    instance = CLIENT.instance(instance_id, instance_config, labels=labels)

    try:
        created_op = instance.create()
        created_op.result(1800)  # block until completion
    except AlreadyExists:
        pass  # instance was already created

    try:
        database = instance.database("compliance-test")
        created_op = database.create()
        created_op.result(1800)
    except AlreadyExists:
        pass  # instance was already created


# Path: create_test_config.py
# def set_test_config(project, instance):
#     config = configparser.ConfigParser()
#     url = (
#         f"spanner+spanner:///projects/{project}/instances/{instance}/"
#         "databases/compliance-test"
#     )
#     config.add_section("db")
#     config["db"]["default"] = url
# 
#     with open("test.cfg", "w") as configfile:
#         config.write(configfile)


# Path: create_test_database.py
import configparser
import os
import time
from create_test_config import set_test_config
from google.api_core.exceptions import AlreadyExists, ResourceExhausted
from google.cloud.spanner_v1 import Client
from google.cloud.spanner_v1.instance import Instance
    from google.auth.credentials import AnonymousCredentials

    configs = list(CLIENT.list_instance_configs())
    if not USE_EMULATOR:
        # Filter out non "us" locations
        configs = [config for config in configs if "-us-" in config.name]

    instance_config = configs[0].name
    create_time = str(int(time.time()))
    unique_resource_id = "%s%d" % ("-", 1000 * time.time())
    instance_id = (
        "sqlalchemy-dialect-test"
        if USE_EMULATOR
        else "sqlalchemy-test" + unique_resource_id
    )
    labels = {"python-spanner-sqlalchemy-systest": "true", "created": create_time}

    instance = CLIENT.instance(instance_id, instance_config, labels=labels)

    try:
        created_op = instance.create()
        created_op.result(1800)  # block until completion
    except AlreadyExists:
        pass  # instance was already created

    try:
        database = instance.database("compliance-test")
        created_op = database.create()
        created_op.result(1800)
    except AlreadyExists:
        pass  # instance was already created


    set_test_config(PROJECT, instance_id)