Skip to content

Commit

Permalink
[specs_tests] specs & test 100% coverage (#14)
Browse files Browse the repository at this point in the history
* [specs_tests] specs & test 100% coverage

* [specs_tests] style

* [specs_tests] setup.py

* [specs_tests] s3 specs & tests
  • Loading branch information
ome9ax authored Aug 13, 2021
1 parent 0e6896b commit 09e8d3b
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 49 deletions.
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ include_package_data = True
target_s3_jsonl = logging.conf

[options.extras_require]
test = pytest-cov
test =
pytest-cov
moto[s3]
lint = flake8
dist = wheel

Expand All @@ -54,7 +56,6 @@ testpaths = tests
branch = True
omit =
venv/*
target_s3_jsonl/s3.py

[coverage:report]
show_missing = True
Expand Down
22 changes: 11 additions & 11 deletions target_s3_jsonl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,26 @@ def persist_lines(messages, config):
raise
message_type = o['type']
if message_type == 'RECORD':
if 'stream' not in o:
if 'stream' not in o: # pragma: no cover
raise Exception("Line is missing required key 'stream': {}".format(message))
stream = o['stream']
if stream not in schemas:
if stream not in schemas: # pragma: no cover
raise Exception('A record for stream {} was encountered before a corresponding schema'.format(stream))

# NOTE: Validate record
record_to_load = o['record']
try:
validators[stream].validate(float_to_decimal(record_to_load))
except Exception as ex:
if type(ex).__name__ == "InvalidOperation":
if type(ex).__name__ == "InvalidOperation": # pragma: no cover
LOGGER.error(
"Data validation failed and cannot load to destination. RECORD: {}\n"
"'multipleOf' validations that allows long precisions are not supported"
" (i.e. with 15 digits or more). Try removing 'multipleOf' methods from JSON schema."
.format(record_to_load))
raise ex

if config.get('add_metadata_columns'):
if config.get('add_metadata_columns'): # pragma: no cover
record_to_load = add_metadata_values_to_record(o, {}, now.timestamp())
else:
record_to_load = remove_metadata_values_from_record(o)
Expand All @@ -209,32 +209,32 @@ def persist_lines(messages, config):

# NOTE: write temporary file
# Use 64Mb default memory buffer
if sys.getsizeof(file_data[stream]['file_data']) > config.get('memory_buffer', 64e6):
if sys.getsizeof(file_data[stream]['file_data']) > config.get('memory_buffer', 64e6): # pragma: no cover
save_file(file_data[stream], open_func)

state = None
elif message_type == 'STATE':
LOGGER.debug('Setting state to {}'.format(o['value']))
state = o['value']
elif message_type == 'SCHEMA':
if 'stream' not in o:
if 'stream' not in o: # pragma: no cover
raise Exception("Line is missing required key 'stream': {}".format(message))
stream = o['stream']

if config.get('add_metadata_columns'):
if config.get('add_metadata_columns'): # pragma: no cover
schemas[stream] = add_metadata_columns_to_schema(o)
else:
schemas[stream] = float_to_decimal(o['schema'])

validators[stream] = Draft4Validator(schemas[stream], format_checker=FormatChecker())

if 'key_properties' not in o:
if 'key_properties' not in o: # pragma: no cover
raise Exception('key_properties field is required')
key_properties[stream] = o['key_properties']
LOGGER.debug('Setting schema for {}'.format(stream))

# NOTE: get the s3 file key
if stream not in file_data:
if stream not in file_data: # pragma: no cover
file_data[stream] = {
'target_key': get_target_key(
o,
Expand All @@ -247,7 +247,7 @@ def persist_lines(messages, config):

elif message_type == 'ACTIVATE_VERSION':
LOGGER.debug('ACTIVATE_VERSION {}'.format(message))
else:
else: # pragma: no cover
LOGGER.warning('Unknown message type {} in message {}'.format(o['type'], o))

for _, file_info in file_data.items():
Expand Down Expand Up @@ -278,5 +278,5 @@ def main():
LOGGER.debug('Exiting normally')


if __name__ == '__main__':
if __name__ == '__main__': # pragma: no cover
main()
10 changes: 5 additions & 5 deletions target_s3_jsonl/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def retry_pattern():


def log_backoff_attempt(details):
LOGGER.info("Error detected communicating with Amazon, triggering backoff: %d try", details.get("tries"))
LOGGER.info("Error detected communicating with Amazon, triggering backoff: %d try", details.get("tries")) # pragma: no cover


@retry_pattern()
Expand All @@ -41,9 +41,9 @@ def create_client(config):
)
# AWS Profile based authentication
else:
aws_session = boto3.session.Session(profile_name=aws_profile)
aws_session = boto3.session.Session(profile_name=aws_profile) # pragma: no cover
if aws_endpoint_url:
s3 = aws_session.client('s3', endpoint_url=aws_endpoint_url)
s3 = aws_session.client('s3', endpoint_url=aws_endpoint_url) # pragma: no cover
else:
s3 = aws_session.client('s3')
return s3
Expand All @@ -59,7 +59,7 @@ def upload_file(filename, s3_client, bucket, s3_key,
encryption_desc = ""
encryption_args = None
else:
if encryption_type.lower() == "kms":
if encryption_type.lower() == "kms": # pragma: no cover
encryption_args = {"ServerSideEncryption": "aws:kms"}
if encryption_key:
encryption_desc = (
Expand All @@ -70,7 +70,7 @@ def upload_file(filename, s3_client, bucket, s3_key,
else:
encryption_desc = " using default KMS encryption"
else:
raise NotImplementedError(
raise NotImplementedError( # pragma: no cover
"Encryption type '{}' is not supported. "
"Expected: 'none' or 'KMS'"
.format(encryption_type)
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"local": false,
"local": true,
"add_metadata_columns": false,
"aws_access_key_id": "ACCESS-KEY",
"aws_secret_access_key": "SECRET",
Expand Down
12 changes: 12 additions & 0 deletions tests/resources/config_local_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"local": false,
"add_metadata_columns": false,
"aws_access_key_id": "ACCESS-KEY",
"aws_secret_access_key": "SECRET",
"s3_bucket": "BUCKET",
"temp_dir": "tests/output",
"memory_buffer": 2000000,
"compression": "none",
"timezone_offset": 0,
"naming_convention": "{stream}-{timestamp}.jsonl"
}
11 changes: 11 additions & 0 deletions tests/resources/config_no_bucket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"local": true,
"add_metadata_columns": false,
"aws_access_key_id": "ACCESS-KEY",
"aws_secret_access_key": "SECRET",
"temp_dir": "tests/output",
"memory_buffer": 2000000,
"compression": "none",
"timezone_offset": 0,
"naming_convention": "{stream}-{timestamp}.jsonl"
}
Loading

0 comments on commit 09e8d3b

Please sign in to comment.