diff --git a/CHANGELOG.md b/CHANGELOG.md index 1592ac685731f0e132a2317559335f3fed924fa2..1d6e815c1a4c797414bc7275495aaf91c547d0b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,12 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ### ### Deprecated ### -- `connection.get_username`. Use `la.Info().user_info.name` instead. + +* `connection.get_username`. Use `la.Info().user_info.name` instead. ### Removed ### ### Fixed ### +* [#128](https://gitlab.com/linkahead/linkahead-pylib/-/issues/128) + Assign `datetime.date` or `datetime.datetime` values to `DATETIME` + properties. + ### Security ### ### Documentation ### diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 96851f34bd7558e03fb60d92ca001d8cd7c43171..a8144286fdacefacadf2b823160e0eb9bfe00c77 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -39,6 +39,7 @@ import re import sys from builtins import str from copy import deepcopy +from datetime import date, datetime from functools import cmp_to_key from hashlib import sha512 from os import listdir @@ -50,7 +51,6 @@ from typing import TYPE_CHECKING from typing import Any, Final, Literal, Optional, TextIO, Union if TYPE_CHECKING: - from datetime import datetime from .datatype import DATATYPE from tempfile import _TemporaryFileWrapper from io import BufferedWriter @@ -1687,6 +1687,9 @@ def _parse_value(datatype, value): if isinstance(value, str): return value + if datatype == DATETIME and (isinstance(value, date) or isinstance(value, datetime)): + return value + # deal with collections if isinstance(datatype, str): matcher = re.compile(r"^(?P<col>[^<]+)<(?P<dt>[^>]+)>$") diff --git a/tox.ini b/tox.ini index bbaaa1fc9eec2aba87c247d783818d215d8a7d5e..592c660c5bbbf5805a3ecbb3e60c41f597182a55 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ deps = . mypy jsonschema>=4.4.0 setuptools -commands=py.test --cov=caosdb -vv {posargs} +commands=py.test --cov=linkahead -vv {posargs} [flake8] max-line-length=100 diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 7472f710cea32c1d76f11e52fe7c3c3617804c3c..e24afbe8b7be8d9a87d85819eccd3a4bf0d453e8 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -24,6 +24,7 @@ import os import lxml import linkahead as db +from datetime import date, datetime from pytest import raises @@ -64,3 +65,28 @@ def test_issue_156(): # </ParentList> assert value is project assert parents[0].name == "RTName" + + +def test_issue_128(): + """Test assigning datetime.date(time) values to DATETIME + properties: + https://gitlab.com/linkahead/linkahead-pylib/-/issues/128. + + """ + # Test assignement correct assignment for both datatype=DATETIME + # and datatype=LIST<DATETIME>, just to be sure. + prop = db.Property(name="TestDatetime", datatype=db.DATETIME) + prop_list = db.Property(name="TestListDatetime", datatype=db.LIST(db.DATETIME)) + + today = date.today() + now = datetime.now() + + prop.value = today + assert prop.value == today + prop.value = now + assert prop.value == now + + prop_list.value = [today, today] + assert prop_list.value == [today, today] + prop_list.value = [now, now] + assert prop_list.value == [now, now]