"""Pytest conftest module.""" import os from typing import NamedTuple from uuid import uuid4 import boto3 from constants.queries import create_project from lambdacommon.graphql.graphql import GraphQLConnector import pytest from dotenv import load_dotenv # noqa load_dotenv() # noqa from config import graphql_gateway, LAMBDA_NAME # noqa class TestRunData(NamedTuple): """Test run data class.""" bucket: str key: str data_directory: str vendor_id: int subaccount_id: int s3_client: boto3.client lambda_client: boto3.client graphql_client: GraphQLConnector class GraphQLResult(NamedTuple): """GraphQL result class.""" payload: dict result: dict @pytest.fixture() def data_directory(): """Return data directory.""" return os.path.abspath( os.path.join(os.path.dirname(__file__), '../data/') ) @pytest.fixture() def lambda_client(): """Lambda client.""" return boto3.client('lambda', region_name='us-east-1') @pytest.fixture() def s3_client(): """Lambda client.""" return boto3.client('s3', region_name='us-east-1') @pytest.fixture() def lambda_name(): """Lambda name.""" return f'{LAMBDA_NAME}-qa:qa' @pytest.fixture() def graphql_client(): """Graphql client.""" return graphql_gateway @pytest.fixture() def vendor(): """Test vendor.""" return os.getenv('VENDOR_ID') @pytest.fixture() def subaccount(): """Test subaccount.""" return os.getenv('SUBACCOUNT_ID') @pytest.fixture() def project(vendor, subaccount): """Fixture to create project.""" payload = { 'data': { 'projectCode': str(uuid4())[:18], 'name': 'FAKE project name', 'artistId': 2348776, 'description': 'FAKE project description', 'accountId': int(vendor), 'subaccountId': int(subaccount) } } result = graphql_gateway.execute( create_project, payload )['data']['createProject'] return GraphQLResult(payload, result) @pytest.fixture() def test_workspace_setup(graphql_client, lambda_client, s3_client, data_directory, vendor, subaccount): """Test workspace setup.""" bucket = os.getenv('test_bucket', 'qa-ddex-ingester') key = f'integration_tests/{uuid4()}/' s3_client.put_object(Bucket=bucket, Key=key) yield TestRunData( bucket, key, data_directory, vendor, subaccount, lambda_client=lambda_client, s3_client=s3_client, graphql_client=graphql_client ) s3 = boto3.resource('s3') bucket = s3.Bucket(bucket) # To delete all file objects under test workspace bucket.objects.filter(Prefix=key).delete()