def test_wrong_tsv_length(): exception = None try: transform("two\tfields") except SnowplowEventTransformationException as sete: exception = sete assert(exception.message == "Expected 131 fields, received 2 fields.")
def test_multiple_malformed_fields(): exception = None malformed_fields_tsv = '\t' * 52 + 'bad_contexts' + '\t' * 50 + 'bad_dvce_ismobile' + '\t' * 28 try: transform(malformed_fields_tsv) except SnowplowEventTransformationException as sete: exception = sete assert(len(exception.error_messages) == 2)
def test_malformed_field(): exception = None malformed_fields_tsv = '\t' * 110 + 'bad_tax_base' + '\t' * 20 try: transform(malformed_fields_tsv) except SnowplowEventTransformationException as sete: exception = sete assert(exception.message == "Unexpected exception parsing field with key tr_tax_base and value bad_tax_base: ValueError('could not convert string to float: bad_tax_base',)")
def test_malformed_field(): exception = None malformed_fields_tsv = '\t' * 110 + 'bad_tax_base' + '\t' * 20 try: transform(malformed_fields_tsv) except SnowplowEventTransformationException as sete: exception = sete assert(exception.message.startswith( "Unexpected exception parsing field with key tr_tax_base and value bad_tax_base"))
def test_multiple_malformed_fields(): exception = None malformed_fields_tsv = '\t' * 52 + 'bad_contexts' + '\t' * 50 + 'bad_dvce_ismobile' + '\t' * 28 try: transform(malformed_fields_tsv) except SnowplowEventTransformationException as sete: exception = sete assert(exception.error_messages == [ "Unexpected exception parsing field with key contexts and value bad_contexts: ValueError('No JSON object could be decoded',)", "Invalid value bad_dvce_ismobile for field dvce_ismobile" ])
def handler(event, context): # Load gotrdon context with open('.context', 'r') as f: gordon_context = json.loads(f.read()) print("Received event: " + json.dumps(event, indent=2)) client = boto3.client('dynamodb') for record in event.get('Records', []): # Convert the raw event into a snowplow event raw_event = base64.b64decode(record['kinesis']['data']) try: snowplow_event = event_transformer.transform(raw_event) except snowplow_event_transformation_exception.SnowplowEventTransformationException as e: for error_message in e.error_messages: print(error_message) return "error!" # Update dynamodb with the new value for impressions response = client.update_item( TableName=gordon_context['dynamodb_table'], Key={ 'message_id': { 'S': snowplow_event.get('se_label', '12345') } }, ReturnValues='ALL_NEW', ReturnConsumedCapacity='NONE', ReturnItemCollectionMetrics='NONE', UpdateExpression='ADD {} :val'.format(snowplow_event.get('se_category', 'impression')), ExpressionAttributeValues={ ':val': { 'N': snowplow_event.get('se_value', '1'), } } ) print response return "ok!"
def test_unicode_tsv(): actual = transform(unicode(tsv)) assert(actual==expected)
def test_transform(): actual = transform(tsv) assert(actual==expected)