# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

from decimal import Decimal

import pytest
from voluptuous import Required

import czc.constants as constants
from czc.domain_events import create_event, create_schema
from tests.sample_data import CORRELATION_ID


@pytest.mark.unit
def test_schema_with_default_attributes():
    data_schema = {
        Required('bogus'): str,
    }
    schema = create_schema('ns', constants.FEATURE_NAME, 'bogus-event-happened', data_schema)
    event_dict = create_event(schema, {'bogus': 'foobar'})
    assert event_dict is not None
    assert event_dict['correlationId']
    assert event_dict['feature'] == constants.FEATURE_NAME
    assert event_dict['eventName'] == 'bogus-event-happened'
    assert event_dict['version']
    assert event_dict['data']['bogus'] == 'foobar'
    assert event_dict['id']
    assert event_dict['timestamp']


@pytest.mark.unit
def test_schema_attribute_overrides():
    data_schema = {
        Required('bogus'): str,
    }
    schema = create_schema('ns', constants.FEATURE_NAME, 'bogus-event-happened', data_schema, correlation_id=CORRELATION_ID,
                           version=17, timestamp=Decimal('1542037297.1230303'), event_id='foobarjones')
    event_dict = create_event(schema, {'bogus': 'foobar'})
    assert event_dict is not None
    assert event_dict['correlationId'] == CORRELATION_ID
    assert event_dict['feature'] == constants.FEATURE_NAME
    assert event_dict['eventName'] == 'bogus-event-happened'
    assert event_dict['version'] == 17
    assert event_dict['data']['bogus'] == 'foobar'
    assert event_dict['id'] == 'foobarjones'
    assert event_dict['timestamp'] == '1542037297.1230303'


@pytest.mark.unit
def test_schema_roundtrip():
    data_schema = {
        Required('bogus'): str,
    }
    schema = create_schema('ns', constants.FEATURE_NAME, 'bogus-event-happened', data_schema)
    event_dict = create_event(schema, {'bogus': 'foobar'})
    assert event_dict is not None
    event_dict['data']['bogus'] = 'barfoo'
    schema(event_dict)  # This should not throw an exception
