Commit 00930d76 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Enhanced metainfo basic types tests.

parent edc65261
Pipeline #92291 passed with stages
in 28 minutes and 13 seconds
......@@ -349,7 +349,9 @@ from .metainfo import (
Reference,
QuantityReference,
Datetime,
Unit,
JSON,
Dimension,
MResource,
m_package,
Annotation,
......
......@@ -419,8 +419,10 @@ class _Datetime(DataType):
pass
try:
return aniso8601.parse_date(datetime_str)
except ValueError:
date = aniso8601.parse_date(datetime_str)
if isinstance(date, datetime):
return date
except ValueError as e:
pass
try:
......@@ -440,6 +442,11 @@ class _Datetime(DataType):
except ValueError:
pass
try:
return datetime.strptime(datetime_str, '%Y-%m-%d')
except ValueError:
pass
raise TypeError('Invalid date literal %s' % datetime_str)
def _convert(self, value):
......
......@@ -16,6 +16,9 @@
# limitations under the License.
#
# Contains more general test cases that are replaced continiously by more specialized
# in-depth tests in test_* files of the same module.
import pytest
import numpy as np
import pint.quantity
......@@ -684,45 +687,6 @@ class TestM1:
assert len(parent.many_sub_section) == 2
class TestDatatypes:
def test_datetime(self):
class TestSection(MSection):
datetime = Quantity(type=Datetime)
obj = TestSection()
assert obj.datetime is None
assert 'datetime' not in obj.m_to_dict()
obj.datetime = datetime.datetime.now()
assert obj.datetime is not None
assert isinstance(obj.m_to_dict()['datetime'], str)
obj.datetime = obj.datetime.isoformat()
assert obj.datetime is not None
assert isinstance(obj.m_to_dict()['datetime'], str)
obj.datetime = None
assert obj.datetime is None
assert 'datetime' not in obj.m_to_dict()
def test_json(self):
class TestSection(MSection):
json = Quantity(type=JSON)
obj = TestSection()
assert obj.json is None
assert 'json' not in obj.m_to_dict()
obj.json = dict(test_key='test_value')
assert obj.json is not None
assert isinstance(obj.m_to_dict()['json'], dict)
obj.json = None
assert obj.json is None
assert 'json' not in obj.m_to_dict()
class TestEnvironment:
@pytest.fixture
......
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
import json
import datetime
from nomad.metainfo import MSection, Quantity, Unit, units, JSON, Dimension, Datetime, Capitalized
@pytest.mark.parametrize('def_type, value', [
pytest.param(str, 'hello', id='str'),
pytest.param(int, 23, id='int'),
pytest.param(float, 3.14e23, id='float'),
pytest.param(bool, True, id='bool'),
pytest.param(JSON, dict(key='value'), id='JSON'),
pytest.param(Unit, units.parse_units('m*m/s'), id='Unit'),
pytest.param(Dimension, '*', id='Dimension-*'),
pytest.param(Dimension, 1, id='Dimension-1'),
pytest.param(Dimension, 'quantity', id='Dimension-quantity'),
pytest.param(Datetime, datetime.datetime.now(), id='Datetime'),
pytest.param(Capitalized, 'Hello', id='Capitalize')
])
def test_basic_types(def_type, value):
class TestSection(MSection):
quantity = Quantity(type=def_type)
section = TestSection()
assert section.quantity is None
section.quantity = value
assert section.quantity == value
section_serialized = section.m_to_dict()
json.dumps(section_serialized)
section = TestSection.m_from_dict(section_serialized)
assert section.quantity == value
class TestSection(MSection):
quantity = Quantity(type=def_type, default=value)
section = TestSection()
assert section.quantity == value
assert 'quantity' not in section.m_to_dict()
@pytest.mark.parametrize('def_type, orig_value, normalized_value', [
pytest.param(Unit, 'm*m/s', units.parse_units('m*m/s'), id='Unit'),
pytest.param(Datetime, '1970-01-01 01:00:00', None, id='Datetime-str'),
pytest.param(Datetime, '1970-01-01 01:00:00.0000', None, id='Datetime-str-ms'),
pytest.param(Datetime, 'Wed, 01 Jan 1970 00:00:00 -0100', None, id='Datetime-rfc822'),
pytest.param(Datetime, '1970-01-01T00:00:00Z', None, id='Datetime-aniso861-time'),
pytest.param(Datetime, '1970-01-01', None, id='Datetime-aniso861-date'),
pytest.param(Capitalized, 'hello', 'Hello', id='Capitalize')
])
def test_value_normalization(def_type, orig_value, normalized_value):
class TestSection(MSection):
quantity = Quantity(type=def_type)
section = TestSection()
assert section.quantity is None
section.quantity = orig_value
assert normalized_value is None or section.quantity == normalized_value
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment