# Path: gdcdatamodel/validators/json_validators.py
# class GDCJSONValidator(object):
#     def __init__(self):
#         self.schemas = gdcdictionary
# 
#     def iter_errors(self, doc):
#         # Note whenever gdcdictionary use a newer version of jsonschema
#         # we need to update the Validator
#         validator = Draft4Validator(self.schemas.schema[doc['type']])
#         return validator.iter_errors(doc)
# 
#     def record_errors(self, entities):
#         for entity in entities:
#             json_doc = entity.doc
#             if 'type' not in json_doc:
#                 entity.record_error(
#                     "'type' is a required property", keys=['type'])
#                 break
#             if json_doc['type'] not in self.schemas.schema:
#                 entity.record_error(
#                     "specified type: {} is not in the current data model"
#                     .format(json_doc['type']), keys=['type'])
#                 break
#             for error in self.iter_errors(json_doc):
#                 # the key will be  property.sub property for nested properties
#                 errors = [str(e) for e in error.path if error.path]
#                 keys = ['.'.join(errors)] if errors else []
#                 if not keys:
#                     keys = get_keys(error.message)
#                 message = error.message
#                 if error.context:
#                     message += ': {}'.format(' and '.join([c.message for c in error.context]))
#                 entity.record_error(message, keys=keys)
#             # additional validators go here
# 
# Path: gdcdatamodel/validators/graph_validators.py
# class GDCGraphValidator(object):
#     '''
#     Validator that validates entities' relationship with existing nodes in
#     database.
# 
#     '''
#     def __init__(self):
#         self.schemas = gdcdictionary
#         self.required_validators = {
#             'links_validator': GDCLinksValidator(),
#             'uniqueKeys_validator': GDCUniqueKeysValidator()
#         }
#         self.optional_validators = {}
# 
#     def record_errors(self, graph, entities):
#         for validator in self.required_validators.values():
#             validator.validate(entities, graph)
# 
#         for entity in entities:
#             schema = self.schemas.schema[entity.node.label]
#             validators = schema.get('validators')
#             if validators:
#                 for validator_name in validators:
#                     self.optional_validators[validator_name].validate()
# 
# Path: test/conftest.py
# class BaseTestCase(unittest.TestCase):
#     def setUp(self):
#         truncate(self.g.engine)
# 
#     def tearDown(self):
#         truncate(self.g.engine)


import uuid
from copy import copy
from gdcdatamodel.validators import GDCJSONValidator, GDCGraphValidator
from gdcdatamodel.models import *
from test.conftest import BaseTestCase
        super(TestValidators, self).setUp()

        self.graph_validator = GDCGraphValidator()
        self.json_validator = GDCJSONValidator()
        self.entities = [MockSubmissionEntity()]

    def test_json_validator_with_insufficient_properties(self):
        self.entities[0].doc = {'type': 'aliquot',
                                'centers': {'submitter_id': 'test'}}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(self.entities[0].errors[0]['keys'], ['submitter_id'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_wrong_node_type(self):
        self.entities[0].doc = {'type': 'aliquo'}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(self.entities[0].errors[0]['keys'], ['type'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_wrong_property_type(self):
        self.entities[0].doc = {'type': 'aliquot',
                                'submitter_id': 1, 'centers': {'submitter_id': 'test'}}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(['submitter_id'], self.entities[0].errors[0]['keys'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_multiple_errors(self):
        self.entities[0].doc = {'type': 'aliquot', 'submitter_id': 1,
                                'test': 'test',
                                'centers': {'submitter_id': 'test'}}

# Path: gdcdatamodel/validators/json_validators.py
# class GDCJSONValidator(object):
#     def __init__(self):
#         self.schemas = gdcdictionary
# 
#     def iter_errors(self, doc):
#         # Note whenever gdcdictionary use a newer version of jsonschema
#         # we need to update the Validator
#         validator = Draft4Validator(self.schemas.schema[doc['type']])
#         return validator.iter_errors(doc)
# 
#     def record_errors(self, entities):
#         for entity in entities:
#             json_doc = entity.doc
#             if 'type' not in json_doc:
#                 entity.record_error(
#                     "'type' is a required property", keys=['type'])
#                 break
#             if json_doc['type'] not in self.schemas.schema:
#                 entity.record_error(
#                     "specified type: {} is not in the current data model"
#                     .format(json_doc['type']), keys=['type'])
#                 break
#             for error in self.iter_errors(json_doc):
#                 # the key will be  property.sub property for nested properties
#                 errors = [str(e) for e in error.path if error.path]
#                 keys = ['.'.join(errors)] if errors else []
#                 if not keys:
#                     keys = get_keys(error.message)
#                 message = error.message
#                 if error.context:
#                     message += ': {}'.format(' and '.join([c.message for c in error.context]))
#                 entity.record_error(message, keys=keys)
#             # additional validators go here
# 
# Path: gdcdatamodel/validators/graph_validators.py
# class GDCGraphValidator(object):
#     '''
#     Validator that validates entities' relationship with existing nodes in
#     database.
# 
#     '''
#     def __init__(self):
#         self.schemas = gdcdictionary
#         self.required_validators = {
#             'links_validator': GDCLinksValidator(),
#             'uniqueKeys_validator': GDCUniqueKeysValidator()
#         }
#         self.optional_validators = {}
# 
#     def record_errors(self, graph, entities):
#         for validator in self.required_validators.values():
#             validator.validate(entities, graph)
# 
#         for entity in entities:
#             schema = self.schemas.schema[entity.node.label]
#             validators = schema.get('validators')
#             if validators:
#                 for validator_name in validators:
#                     self.optional_validators[validator_name].validate()
# 
# Path: test/conftest.py
# class BaseTestCase(unittest.TestCase):
#     def setUp(self):
#         truncate(self.g.engine)
# 
#     def tearDown(self):
#         truncate(self.g.engine)


# Path: test/test_validators.py
import uuid
from copy import copy
from gdcdatamodel.validators import GDCJSONValidator, GDCGraphValidator
from gdcdatamodel.models import *
from test.conftest import BaseTestCase

        super(TestValidators, self).setUp()

        self.graph_validator = GDCGraphValidator()
        self.json_validator = GDCJSONValidator()
        self.entities = [MockSubmissionEntity()]

    def test_json_validator_with_insufficient_properties(self):
        self.entities[0].doc = {'type': 'aliquot',
                                'centers': {'submitter_id': 'test'}}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(self.entities[0].errors[0]['keys'], ['submitter_id'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_wrong_node_type(self):
        self.entities[0].doc = {'type': 'aliquo'}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(self.entities[0].errors[0]['keys'], ['type'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_wrong_property_type(self):
        self.entities[0].doc = {'type': 'aliquot',
                                'submitter_id': 1, 'centers': {'submitter_id': 'test'}}
        self.json_validator.record_errors(self.entities)
        self.assertEqual(['submitter_id'], self.entities[0].errors[0]['keys'])
        self.assertEqual(1, len(self.entities[0].errors))

    def test_json_validator_with_multiple_errors(self):
        self.entities[0].doc = {'type': 'aliquot', 'submitter_id': 1,
                                'test': 'test',
                                'centers': {'submitter_id': 'test'}}

        self.json_validator.record_errors(self.entities)