def test_no_terms_loaded(self, create_input_metadatav1):
        terms = []
        serialized_record = create_input_metadatav1({'terms': terms})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'terms' in deserialized_metadata
        assert deserialized_metadata['terms'] == terms

        serialized_record2 = create_input_metadatav1()

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record2)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'terms' in deserialized_metadata
        assert deserialized_metadata['terms'] == terms

        serialized_record3 = create_input_metadatav1(
            {'terms': [None, {}, {
                'data': None
            }, {
                'data': {}
            }, '']})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record3)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'terms' in deserialized_metadata
        assert deserialized_metadata['terms'] == []
    def test_coalesce_terms_loaded(self, create_input_metadatav1):
        terms = [{
            'source': 'MeSH',
            'value': 'Cognitive Neuroscience',
            'id': 'D000066494'
        }, {
            'source': 'FAST',
            'value': 'Glucagonoma',
            'id': '943672'
        }]

        serialized_record = create_input_metadatav1({
            'mesh_terms': [{
                'data': terms[0]
            }],
            'fast_terms': [{
                'data': terms[1]
            }]
        })

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'terms' in deserialized_metadata
        assert 'mesh_terms' not in deserialized_metadata
        assert 'fast_terms' not in deserialized_metadata
        assert deserialized_metadata['terms'] == terms
    def test_description_too_short_returns_error(self,
                                                 create_input_metadatav1):
        serialized_record = create_input_metadatav1({'description': 'A '})

        unmarshalled_record = MetadataSchemaV1().load(serialized_record)

        assert 'description' in unmarshalled_record.errors
    def test_authors_loaded(self, create_input_metadatav1):
        authors = [
            {
                'first_name': 'John',
                'middle_name': 'Jacob',
                'last_name': 'Smith'
            },
            {
                'first_name': 'Jane',
                'middle_name': 'Janet',
                'last_name': 'Doe',
                'full_name': 'Doe, Jane J.'  # Should be overwritten
            }
        ]
        serialized_record = create_input_metadatav1({'authors': authors})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'authors' in deserialized_metadata
        assert deserialized_metadata['authors'][0] == {
            'first_name': 'John',
            'middle_name': 'Jacob',
            'last_name': 'Smith',
            'full_name': 'Smith, John Jacob'
        }
        assert deserialized_metadata['authors'][1] == {
            'first_name': 'Jane',
            'middle_name': 'Janet',
            'last_name': 'Doe',
            'full_name': 'Doe, Jane Janet'
        }
    def test_invalid_permissions_returns_errors(self, create_input_metadatav1):
        serialized_record = create_input_metadatav1(
            {'permissions': 'foo_view'})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert 'permissions' in unmarshalled_metadata.errors
    def test_extra_key_is_ignored(self, create_input_metadatav1):
        serialized_record = create_input_metadatav1({'foo': 'bar'})

        unmarshalled_record = MetadataSchemaV1().load(serialized_record)
        loaded_record = unmarshalled_record.data

        # marshmallow does not care about additional keys
        assert 'foo' not in unmarshalled_record.errors
        assert 'foo' not in loaded_record
    def test_permissions_loaded(self, create_input_metadatav1):
        serialized_record = create_input_metadatav1(
            {'permissions': 'restricted_view'})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert deserialized_metadata['permissions'] == 'restricted_view'
    def test_incorrect_format_terms_returns_error(self,
                                                  create_input_metadatav1):
        terms = ["bar"]
        serialized_record = create_input_metadatav1({'terms': terms})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert 'terms' in unmarshalled_metadata.errors
        assert deserialized_metadata['terms'] == [{}]
    def test_resource_type_loaded(self, create_input_metadatav1):
        serialized_record = create_input_metadatav1(
            {'resource_type': {
                'general': 'other',
                'specific': 'other'
            }})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'resource_type' in deserialized_metadata
    def test_missing_keys_return_errors(self):
        serialized_record = {'foo': 'bar'}

        unmarshalled_record = MetadataSchemaV1().load(serialized_record)

        required_keys = [
            'title', 'description', 'authors', 'resource_type', 'license',
            'permissions'
        ]
        assert set(unmarshalled_record.errors.keys()) == set(required_keys)
        assert (unmarshalled_record.errors['title'] == [
            'Missing data for required field.'
        ])
    def test_one_term_loaded(self, create_input_metadatav1):
        terms = [{
            'source': 'MeSH',
            'value': 'Cognitive Neuroscience',
            'id': 'D000066494'
        }]
        serialized_record = create_input_metadatav1(
            {'terms': [{
                'data': term
            } for term in terms]})

        unmarshalled_metadata = MetadataSchemaV1().load(serialized_record)
        deserialized_metadata = unmarshalled_metadata.data

        assert not unmarshalled_metadata.errors
        assert 'terms' in deserialized_metadata
        assert deserialized_metadata['terms'] == terms
    def test_empty_required_key_returns_errors(self, create_input_metadatav1):
        serialized_record = create_input_metadatav1({'title': None})

        unmarshalled_record = MetadataSchemaV1().load(serialized_record)

        assert 'title' in unmarshalled_record.errors