|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2017-2019- Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Serialization tests for renku models.""" |
| 19 | + |
| 20 | +import datetime |
| 21 | + |
| 22 | +import yaml |
| 23 | + |
| 24 | + |
| 25 | +def test_dataset_serialization(client, dataset, data_file): |
| 26 | + """Test Dataset serialization.""" |
| 27 | + |
| 28 | + def load_dataset(name): |
| 29 | + with open(str(client.dataset_path(name))) as f: |
| 30 | + return yaml.safe_load(f) |
| 31 | + |
| 32 | + d_dict = load_dataset('dataset') |
| 33 | + |
| 34 | + expected_fields = [ |
| 35 | + '_id', '_label', '_project', 'created', 'creator', 'date_published', |
| 36 | + 'description', 'files', 'identifier', 'in_language', 'keywords', |
| 37 | + 'license', 'name', 'path', 'url', 'version' |
| 38 | + ] |
| 39 | + for field in expected_fields: |
| 40 | + assert field in d_dict |
| 41 | + |
| 42 | + assert not d_dict['files'] |
| 43 | + client.add_data_to_dataset(dataset, [str(data_file)]) |
| 44 | + dataset.to_yaml() |
| 45 | + d_dict = load_dataset('dataset') |
| 46 | + assert d_dict['files'] |
| 47 | + |
| 48 | + |
| 49 | +def test_dataset_deserialization(client, dataset): |
| 50 | + """Test Dataset deserialization.""" |
| 51 | + from renku.models.datasets import Dataset |
| 52 | + dataset_ = Dataset.from_yaml(client.dataset_path('dataset'), client=client) |
| 53 | + |
| 54 | + dataset_types = { |
| 55 | + 'created': datetime.datetime, |
| 56 | + 'creator': list, |
| 57 | + 'description': str, |
| 58 | + 'files': list, |
| 59 | + 'identifier': str, |
| 60 | + 'keywords': list, |
| 61 | + } |
| 62 | + |
| 63 | + for attribute, type_ in dataset_types.items(): |
| 64 | + assert type(dataset_.__getattribute__(attribute)) is type_ |
| 65 | + |
| 66 | + creator_types = {'email': str, '_id': str, 'name': str, 'affiliation': str} |
| 67 | + |
| 68 | + creator = dataset.creator[0] |
| 69 | + |
| 70 | + for attribute, type_ in creator_types.items(): |
| 71 | + assert type(creator.get(attribute)) is type_ |
0 commit comments