# Path: djangoevents/exceptions.py
# class EventSchemaError(DjangoeventsError):
#     pass
# 
# Path: djangoevents/domain.py
# class BaseAggregate(EventSourcedEntity):
#     """
#     `EventSourcedEntity` with saner mutator routing & naming:
# 
#     >>> class Asset(BaseAggregate):
#     >>>    class Created(BaseAggregate.Created):
#     >>>        def mutate(event, klass):
#     >>>            return klass(...)
#     >>>
#     >>>    class Updated(DomainEvent):
#     >>>        def mutate(event, instance):
#     >>>            instance.connect = True
#     >>>            return instance
#     """
# 
#     @classmethod
#     def is_abstract_class(cls):
#         return is_abstract(cls)
# 
#     @classmethod
#     def mutate(cls, aggregate=None, event=None):
#         if aggregate:
#             aggregate._validate_originator(event)
# 
#         if not hasattr(event, 'mutate_event'):
#             msg = "{} does not provide a mutate_event() method.".format(event.__class__)
#             raise NotImplementedError(msg)
# 
#         aggregate = event.mutate_event(event, aggregate or cls)
#         aggregate._increment_version()
#         return aggregate
# 
#     @classmethod
#     def create_for_event(cls, event):
#         aggregate = cls(
#             entity_id=event.entity_id,
#             domain_event_id=event.domain_event_id,
#             entity_version=event.entity_version,
#         )
#         return aggregate
# 
# Path: djangoevents/domain.py
# class BaseAggregate(EventSourcedEntity):
# class BaseEntity(BaseAggregate):
#     def is_abstract_class(cls):
#     def mutate(cls, aggregate=None, event=None):
#     def create_for_event(cls, event):
#     def is_abstract_class(cls):
#     def mutate(cls, entity=None, event=None):
# 
# Path: djangoevents/tests/test_domain.py
# class SampleEntity(BaseEntity):
#     class Created(BaseEntity.Created):
#         def mutate_event(self, event, klass):
#             return klass(entity_id=event.entity_id,
#                          entity_version=event.entity_version,
#                          domain_event_id=event.domain_event_id)
# 
#     class Updated(DomainEvent):
#         def mutate_event(self, event, entity):
#             entity.is_dirty = True
#             return entity
# 
#     class Closed(DomainEvent):
#         # no mutate_event
#         pass
# 
#     def __init__(self, is_dirty=False, **kwargs):
#         super().__init__(**kwargs)
#         self.is_dirty = is_dirty
# 
# Path: djangoevents/schema.py
# def set_event_version(aggregate_cls, event_cls, avro_dir=None):
#     avro_dir = avro_dir or get_avro_dir()
#     event_cls.version = _find_highest_event_version_based_on_schemas(aggregate_cls, event_cls, avro_dir)


import avro.schema
import djangoevents.schema as schema
import json
import os
import pytest
import shutil
import tempfile
from djangoevents.exceptions import EventSchemaError
from djangoevents.domain import BaseAggregate
from djangoevents.domain import DomainEvent
from io import StringIO
from django.test import override_settings
from unittest import mock
from .test_domain import SampleEntity
from ..schema import set_event_version
            "name": "name",
            "type": "string",
            "doc": "name of the project"
        }
    ]
})


@override_settings(BASE_DIR='/path/to/proj/src/')
@mock.patch.object(schema, 'list_concrete_aggregates', return_value=[Project])
@mock.patch.object(schema, 'load_event_schema')
def test_load_all_event_schemas(load_schema, load_aggs):
    schema.load_all_event_schemas()
    load_schema.assert_called_once_with(Project, Project.Created)


@override_settings(BASE_DIR='/path/to/proj/src/')
@mock.patch.object(schema, 'list_concrete_aggregates', return_value=[Project])
def test_load_all_event_schemas_missing_specs(list_aggs):
    with pytest.raises(EventSchemaError) as e:
        schema.load_all_event_schemas()

    path = "/path/to/proj/avro/project/v1_project_created.json"
    msg = "No event schema found for: {cls} (expecting file at:{path}).".format(cls=Project.Created, path=path)
    assert msg in str(e.value)


@override_settings(BASE_DIR='/path/to/proj/src/')
def test_valid_event_to_schema_path():
    SampleEntity.Created.version = None

# Path: djangoevents/exceptions.py
# class EventSchemaError(DjangoeventsError):
#     pass
# 
# Path: djangoevents/domain.py
# class BaseAggregate(EventSourcedEntity):
#     """
#     `EventSourcedEntity` with saner mutator routing & naming:
# 
#     >>> class Asset(BaseAggregate):
#     >>>    class Created(BaseAggregate.Created):
#     >>>        def mutate(event, klass):
#     >>>            return klass(...)
#     >>>
#     >>>    class Updated(DomainEvent):
#     >>>        def mutate(event, instance):
#     >>>            instance.connect = True
#     >>>            return instance
#     """
# 
#     @classmethod
#     def is_abstract_class(cls):
#         return is_abstract(cls)
# 
#     @classmethod
#     def mutate(cls, aggregate=None, event=None):
#         if aggregate:
#             aggregate._validate_originator(event)
# 
#         if not hasattr(event, 'mutate_event'):
#             msg = "{} does not provide a mutate_event() method.".format(event.__class__)
#             raise NotImplementedError(msg)
# 
#         aggregate = event.mutate_event(event, aggregate or cls)
#         aggregate._increment_version()
#         return aggregate
# 
#     @classmethod
#     def create_for_event(cls, event):
#         aggregate = cls(
#             entity_id=event.entity_id,
#             domain_event_id=event.domain_event_id,
#             entity_version=event.entity_version,
#         )
#         return aggregate
# 
# Path: djangoevents/domain.py
# class BaseAggregate(EventSourcedEntity):
# class BaseEntity(BaseAggregate):
#     def is_abstract_class(cls):
#     def mutate(cls, aggregate=None, event=None):
#     def create_for_event(cls, event):
#     def is_abstract_class(cls):
#     def mutate(cls, entity=None, event=None):
# 
# Path: djangoevents/tests/test_domain.py
# class SampleEntity(BaseEntity):
#     class Created(BaseEntity.Created):
#         def mutate_event(self, event, klass):
#             return klass(entity_id=event.entity_id,
#                          entity_version=event.entity_version,
#                          domain_event_id=event.domain_event_id)
# 
#     class Updated(DomainEvent):
#         def mutate_event(self, event, entity):
#             entity.is_dirty = True
#             return entity
# 
#     class Closed(DomainEvent):
#         # no mutate_event
#         pass
# 
#     def __init__(self, is_dirty=False, **kwargs):
#         super().__init__(**kwargs)
#         self.is_dirty = is_dirty
# 
# Path: djangoevents/schema.py
# def set_event_version(aggregate_cls, event_cls, avro_dir=None):
#     avro_dir = avro_dir or get_avro_dir()
#     event_cls.version = _find_highest_event_version_based_on_schemas(aggregate_cls, event_cls, avro_dir)


# Path: djangoevents/tests/test_schema.py
import avro.schema
import djangoevents.schema as schema
import json
import os
import pytest
import shutil
import tempfile
from djangoevents.exceptions import EventSchemaError
from djangoevents.domain import BaseAggregate
from djangoevents.domain import DomainEvent
from io import StringIO
from django.test import override_settings
from unittest import mock
from .test_domain import SampleEntity
from ..schema import set_event_version

            "name": "name",
            "type": "string",
            "doc": "name of the project"
        }
    ]
})


@override_settings(BASE_DIR='/path/to/proj/src/')
@mock.patch.object(schema, 'list_concrete_aggregates', return_value=[Project])
@mock.patch.object(schema, 'load_event_schema')
def test_load_all_event_schemas(load_schema, load_aggs):
    schema.load_all_event_schemas()
    load_schema.assert_called_once_with(Project, Project.Created)


@override_settings(BASE_DIR='/path/to/proj/src/')
@mock.patch.object(schema, 'list_concrete_aggregates', return_value=[Project])
def test_load_all_event_schemas_missing_specs(list_aggs):
    with pytest.raises(EventSchemaError) as e:
        schema.load_all_event_schemas()

    path = "/path/to/proj/avro/project/v1_project_created.json"
    msg = "No event schema found for: {cls} (expecting file at:{path}).".format(cls=Project.Created, path=path)
    assert msg in str(e.value)


@override_settings(BASE_DIR='/path/to/proj/src/')
def test_valid_event_to_schema_path():
    SampleEntity.Created.version = None

    avro_path = schema.event_to_schema_path(aggregate_cls=SampleEntity, event_cls=SampleEntity.Created)