# Path: mass_flask_api/schemas/analysis_system_instance.py
# class AnalysisSystemInstanceSchema(BaseSchema):
#     url = URLFor('.analysis_system_instance_detail', uuid='<uuid>', _external=True)
#     analysis_system = ForeignReferenceField(endpoint='.analysis_system_detail', queryset=AnalysisSystem.objects(), query_parameter='identifier_name')
#     is_online = Boolean()
#     scheduled_analyses_count = Method("get_scheduled_analyses_count")
# 
#     class Meta(BaseSchema.Meta):
#         model = AnalysisSystemInstance
#         dump_only = [
#             'id',
#             '_cls',
#             'last_seen',
#             'scheduled_analyses_count'
#         ]
# 
#     def get_scheduled_analyses_count(self, obj):
#         analyses_count = ScheduledAnalysis.objects(analysis_system_instance=obj).count()
#         return analyses_count
# 
# Path: mass_flask_api/schemas/sample_relation.py
# class SampleRelationSchema(BaseSchema):
#     url = URLFor('.sample_relation_detail', id='<id>', _external=True)
#     sample = ForeignReferenceField(endpoint='.sample_detail', queryset=Sample.objects(), query_parameter='id')
#     other = ForeignReferenceField(endpoint='.sample_detail', queryset=Sample.objects(), query_parameter='id')
# 
#     class Meta(BaseSchema.Meta):
#         model = SampleRelation
#         dump_only = [
#                 'id',
#                 '_cls'
#                 ]
# 
# Path: mass_flask_core/models/sample.py
# class Sample(db.Document, CommentsMixin):
#     delivery_date = DateTimeField(default=TimeFunctions.get_timestamp, required=True)
#     first_seen = DateTimeField(default=TimeFunctions.get_timestamp, required=True)
#     tags = ListField(StringField(regex=r'^[\w:\-\_\/\+\.]+$'))
#     dispatched_to = ListField(ReferenceField(AnalysisSystem))
#     created_by = GenericReferenceField()
#     tlp_level = TLPLevelField(verbose_name='TLP Level', help_text='Privacy level of this sample', required=True)
# 
#     meta = {
#         'allow_inheritance': True,
#         'ordering': ['-delivery_date'],
#         'indexes': ['delivery_date'],
#         'queryset_class': SampleQuerySet
#     }
# 
#     def __repr__(self):
#         return '[{}] {}'.format(str(self.__class__.__name__), str(self.id))
# 
#     def __str__(self):
#         return self.__repr__()
# 
#     @property
#     def title(self):
#         return self.id
# 
#     def add_tags(self, tags):
#         self.tags = ListFunctions.merge_lists_without_duplicates(self.tags, tags)
#         self.save()
# 
#     def _update(self, **kwargs):
#         if 'first_seen' in kwargs and kwargs['first_seen'] < self.first_seen:
#             self.first_seen = kwargs['first_seen']
#         if 'tlp_level' in kwargs and not self.tlp_level:
#             self.tlp_level = kwargs['tlp_level']
#         if current_authenticated_entity.is_authenticated and not self.created_by:
#             self.created_by = current_authenticated_entity._get_current_object()
#         if 'tags' in kwargs:
#             tags = kwargs['tags']
#         else:
#             tags = []
#         self.tags = ListFunctions.merge_lists_without_duplicates(self.tags, tags)
# 
#     @classmethod
#     def create_or_update(cls, **kwargs):
#         raise ValidationError('Samples of the base type Sample should not be instantiated directly.')
# 
# Path: mass_flask_core/models/analysis_system.py
# class AnalysisSystem(db.Document):
#     identifier_name = StringField(min_length=3, max_length=50, unique=True, required=True)
#     verbose_name = StringField(max_length=200, required=True)
#     information_text = StringField()
#     tag_filter_expression = StringField(max_length=400, required=True, default='')
# 
#     meta = {
#         'ordering': ['identifier_name'],
#         'indexes': ['identifier_name']
#     }
# 
#     def __repr__(self):
#         return '[AnalysisSystem] {}'.format(self.identifier_name)
# 
#     def __str__(self):
#         return self.__repr__()
# 
# Path: mass_flask_core/tests/flask_test_case.py
# class FlaskTestCase(TestCase):
#     @staticmethod
#     def _clean_test_database():
#         conn = connection.get_connection()
#         db = connection.get_db()
#         conn.drop_database(db)
# 
#     def __call__(self, result=None):
#         self._pre_setup()
#         super(FlaskTestCase, self).__call__(result)
#         self._post_tearDown()
# 
#     def _pre_setup(self):
#         bootstrap_mass_flask()
#         if not 'MASS_TESTING' in app.config or app.config['MASS_TESTING'] is False:
#             raise RuntimeError('Running unit test without TESTING=True in configuration. Aborting.')
#         self.app = app
#         self.client = app.test_client()
#         self._ctx = self.app.test_request_context()
#         self._ctx.push()
# 
#     def _post_tearDown(self):
#         self._ctx.pop()
#         self._clean_test_database()


from flask import url_for
from mixer.backend.mongoengine import mixer
from mass_flask_api.schemas import AnalysisSystemInstanceSchema, SampleRelationSchema
from mass_flask_core.models import AnalysisSystem, Sample
from mass_flask_core.tests import FlaskTestCase



class SchemaTestCase(FlaskTestCase):
    def test_foreign_reference_analysis_system(self):
        system = mixer.blend(AnalysisSystem, identifier_name='testsystem')
        system.save()
        input_data = {
            'analysis_system': url_for('mass_flask_api.analysis_system_detail', identifier_name='testsystem', _external=True),
            'uuid': '9e1502c8-56ad-4ffd-bc03-caa34cb6c768'
        }
        with self.app.test_request_context(url_for('mass_flask_api.analysis_system_instance')):

# Path: mass_flask_api/schemas/analysis_system_instance.py
# class AnalysisSystemInstanceSchema(BaseSchema):
#     url = URLFor('.analysis_system_instance_detail', uuid='<uuid>', _external=True)
#     analysis_system = ForeignReferenceField(endpoint='.analysis_system_detail', queryset=AnalysisSystem.objects(), query_parameter='identifier_name')
#     is_online = Boolean()
#     scheduled_analyses_count = Method("get_scheduled_analyses_count")
# 
#     class Meta(BaseSchema.Meta):
#         model = AnalysisSystemInstance
#         dump_only = [
#             'id',
#             '_cls',
#             'last_seen',
#             'scheduled_analyses_count'
#         ]
# 
#     def get_scheduled_analyses_count(self, obj):
#         analyses_count = ScheduledAnalysis.objects(analysis_system_instance=obj).count()
#         return analyses_count
# 
# Path: mass_flask_api/schemas/sample_relation.py
# class SampleRelationSchema(BaseSchema):
#     url = URLFor('.sample_relation_detail', id='<id>', _external=True)
#     sample = ForeignReferenceField(endpoint='.sample_detail', queryset=Sample.objects(), query_parameter='id')
#     other = ForeignReferenceField(endpoint='.sample_detail', queryset=Sample.objects(), query_parameter='id')
# 
#     class Meta(BaseSchema.Meta):
#         model = SampleRelation
#         dump_only = [
#                 'id',
#                 '_cls'
#                 ]
# 
# Path: mass_flask_core/models/sample.py
# class Sample(db.Document, CommentsMixin):
#     delivery_date = DateTimeField(default=TimeFunctions.get_timestamp, required=True)
#     first_seen = DateTimeField(default=TimeFunctions.get_timestamp, required=True)
#     tags = ListField(StringField(regex=r'^[\w:\-\_\/\+\.]+$'))
#     dispatched_to = ListField(ReferenceField(AnalysisSystem))
#     created_by = GenericReferenceField()
#     tlp_level = TLPLevelField(verbose_name='TLP Level', help_text='Privacy level of this sample', required=True)
# 
#     meta = {
#         'allow_inheritance': True,
#         'ordering': ['-delivery_date'],
#         'indexes': ['delivery_date'],
#         'queryset_class': SampleQuerySet
#     }
# 
#     def __repr__(self):
#         return '[{}] {}'.format(str(self.__class__.__name__), str(self.id))
# 
#     def __str__(self):
#         return self.__repr__()
# 
#     @property
#     def title(self):
#         return self.id
# 
#     def add_tags(self, tags):
#         self.tags = ListFunctions.merge_lists_without_duplicates(self.tags, tags)
#         self.save()
# 
#     def _update(self, **kwargs):
#         if 'first_seen' in kwargs and kwargs['first_seen'] < self.first_seen:
#             self.first_seen = kwargs['first_seen']
#         if 'tlp_level' in kwargs and not self.tlp_level:
#             self.tlp_level = kwargs['tlp_level']
#         if current_authenticated_entity.is_authenticated and not self.created_by:
#             self.created_by = current_authenticated_entity._get_current_object()
#         if 'tags' in kwargs:
#             tags = kwargs['tags']
#         else:
#             tags = []
#         self.tags = ListFunctions.merge_lists_without_duplicates(self.tags, tags)
# 
#     @classmethod
#     def create_or_update(cls, **kwargs):
#         raise ValidationError('Samples of the base type Sample should not be instantiated directly.')
# 
# Path: mass_flask_core/models/analysis_system.py
# class AnalysisSystem(db.Document):
#     identifier_name = StringField(min_length=3, max_length=50, unique=True, required=True)
#     verbose_name = StringField(max_length=200, required=True)
#     information_text = StringField()
#     tag_filter_expression = StringField(max_length=400, required=True, default='')
# 
#     meta = {
#         'ordering': ['identifier_name'],
#         'indexes': ['identifier_name']
#     }
# 
#     def __repr__(self):
#         return '[AnalysisSystem] {}'.format(self.identifier_name)
# 
#     def __str__(self):
#         return self.__repr__()
# 
# Path: mass_flask_core/tests/flask_test_case.py
# class FlaskTestCase(TestCase):
#     @staticmethod
#     def _clean_test_database():
#         conn = connection.get_connection()
#         db = connection.get_db()
#         conn.drop_database(db)
# 
#     def __call__(self, result=None):
#         self._pre_setup()
#         super(FlaskTestCase, self).__call__(result)
#         self._post_tearDown()
# 
#     def _pre_setup(self):
#         bootstrap_mass_flask()
#         if not 'MASS_TESTING' in app.config or app.config['MASS_TESTING'] is False:
#             raise RuntimeError('Running unit test without TESTING=True in configuration. Aborting.')
#         self.app = app
#         self.client = app.test_client()
#         self._ctx = self.app.test_request_context()
#         self._ctx.push()
# 
#     def _post_tearDown(self):
#         self._ctx.pop()
#         self._clean_test_database()


# Path: mass_flask_api/tests/test_foreign_reference.py
from flask import url_for
from mixer.backend.mongoengine import mixer
from mass_flask_api.schemas import AnalysisSystemInstanceSchema, SampleRelationSchema
from mass_flask_core.models import AnalysisSystem, Sample
from mass_flask_core.tests import FlaskTestCase




class SchemaTestCase(FlaskTestCase):
    def test_foreign_reference_analysis_system(self):
        system = mixer.blend(AnalysisSystem, identifier_name='testsystem')
        system.save()
        input_data = {
            'analysis_system': url_for('mass_flask_api.analysis_system_detail', identifier_name='testsystem', _external=True),
            'uuid': '9e1502c8-56ad-4ffd-bc03-caa34cb6c768'
        }
        with self.app.test_request_context(url_for('mass_flask_api.analysis_system_instance')):

            schema = AnalysisSystemInstanceSchema().load(input_data)