Skip to content

Commit

Permalink
tests: added integration tests (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-chumagin authored Oct 2, 2023
1 parent ec6d9ba commit a2257cd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 50 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ services:
- S3_HOST=$S3_HOST
- LAMBDA_HOST=data-test
- LAMBDA_PORT=8080
- BUCKET=dqg-settings-local
depends_on:
- data-test
89 changes: 39 additions & 50 deletions tests/integration_tests/data_test/test_data_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from jsonschema import validate
import boto3
import pytest
import requests
import json
import os
import test_utils as test_utils

schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -52,65 +51,55 @@
}


@pytest.fixture(scope="function")
def s3_test_data(request):
@pytest.fixture
def run_lambda(request):
bucket_name = "dataqa"
file_name = request.param
file_path = f"{bucket_name}/{file_name}"
s3 = _create_boto_s3_resource()
_upload_file_to_s3(s3, bucket_name, file_path, file_name)
response = _invoke_lambda(file_path)
s3 = test_utils.create_boto_s3_resource()
test_utils.upload_file_to_s3(s3, bucket_name, file_path, file_name)
response = test_utils.invoke_lambda(file_path)
json_response = json.loads(response.text)
validate(instance=json_response, schema=schema)
yield file_path
_delete_s3_file(s3, bucket_name, file_path)
yield json_response
test_utils.delete_s3_file(s3, bucket_name, file_path)


@pytest.mark.parametrize("s3_test_data",
@pytest.mark.parametrize("run_lambda",
["titanic.csv",
"titanic.parquet",
"titanic.json",
"titanic_nested.json"],
indirect=True)
def test_data_test(s3_test_data: str):
pass


def _delete_s3_file(s3, bucket_name: str, file_path: str):
s3.Object(bucket_name, file_path).delete()


def _upload_file_to_s3(s3, bucket_name: str, file_path: str, file_name: str):
local_path = f"./test_data/{file_name}"
s3.create_bucket(Bucket=bucket_name)
s3.Object(bucket_name, file_path).put(Body=open(local_path, 'rb'))


def _create_boto_s3_resource():
host = os.environ["S3_HOST"]
url = f"http://{host}:4566"
s3 = boto3.resource("s3", endpoint_url=url,
aws_access_key_id="test",
aws_secret_access_key="test")
return s3
def test_data_test(run_lambda: str):
json_response = run_lambda
validate(instance=json_response, schema=schema)


def _invoke_lambda(file_path: str):
lambda_host = os.environ["LAMBDA_HOST"]
lambda_port = os.environ["LAMBDA_PORT"]
lambda_url = f"http://{lambda_host}:{lambda_port}/2015-03-31/functions/function/invocations"
@pytest.mark.parametrize("run_lambda", ['titanic.csv'], indirect=True)
def test_profile_result_check(run_lambda: str):
qa_bucket = os.environ['BUCKET']
gx_dir = "great_expectations/expectations"
s3 = test_utils.create_boto_s3_resource()
json_response = run_lambda
suite_name = json_response['suite_name']
result_path = f"{qa_bucket}/{gx_dir}/{suite_name}.json"
expectations_suite = test_utils.read_json_file(s3, qa_bucket, result_path)
expectations = expectations_suite["expectations"]
expectations_type = [e['expectation_type'] for e in expectations]
assert set(expectations_type) == {
'expect_column_values_to_be_in_type_list',
'expect_column_value_z_scores_to_be_less_than',
'expect_column_values_to_be_unique',
'expect_table_columns_to_match_set',
'expect_column_mean_to_be_between',
'expect_column_values_to_be_between',
'expect_column_stdev_to_be_between',
'expect_column_to_exist',
'expect_column_values_to_be_increasing',
'expect_column_median_to_be_between',
'expect_column_values_to_be_in_set',
'expect_column_quantile_values_to_be_between',
'expect_table_row_count_to_equal',
'expect_column_values_to_not_be_null'}
assert len(expectations) == 73

payload = json.dumps({
"run_name": "local_test",
"source_root": "dataqa",
"source_data": f"{file_path}",
"engine": "s3"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST",
lambda_url,
headers=headers,
data=payload)
return response
50 changes: 50 additions & 0 deletions tests/integration_tests/data_test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import boto3
import requests
import json
import os


def read_json_file(s3, bucket_name, file_path):
content_object = s3.Object(bucket_name, file_path)
file_content = content_object.get()['Body'].read().decode('utf-8')
return json.loads(file_content)


def delete_s3_file(s3, bucket_name: str, file_path: str):
s3.Object(bucket_name, file_path).delete()


def upload_file_to_s3(s3, bucket_name: str, file_path: str, file_name: str):
local_path = f"./test_data/{file_name}"
s3.create_bucket(Bucket=bucket_name)
s3.Object(bucket_name, file_path).put(Body=open(local_path, 'rb'))


def create_boto_s3_resource():
host = os.environ["S3_HOST"]
url = f"http://{host}:4566"
s3 = boto3.resource("s3", endpoint_url=url,
aws_access_key_id="test",
aws_secret_access_key="test")
return s3


def invoke_lambda(file_path: str):
lambda_host = os.environ["LAMBDA_HOST"]
lambda_port = os.environ["LAMBDA_PORT"]
lambda_url = f"http://{lambda_host}:{lambda_port}/2015-03-31/functions/function/invocations"

payload = json.dumps({
"run_name": "local_test",
"source_root": "dataqa",
"source_data": f"{file_path}",
"engine": "s3"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST",
lambda_url,
headers=headers,
data=payload)
return response

0 comments on commit a2257cd

Please sign in to comment.