Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: datetime -> Aware for created/last_modified #1155

Merged
merged 14 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/aind_data_schema/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
import json
import logging
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
from typing import Dict, List, Literal, Optional, get_args
from uuid import UUID, uuid4
Expand All @@ -20,7 +20,7 @@
model_validator,
)

from aind_data_schema.base import AindCoreModel, is_dict_corrupt
from aind_data_schema.base import AindCoreModel, is_dict_corrupt, AwareDatetimeWithDefault
from aind_data_schema.core.acquisition import Acquisition
from aind_data_schema.core.data_description import DataDescription
from aind_data_schema.core.instrument import Instrument
Expand Down Expand Up @@ -83,15 +83,13 @@ class Metadata(AindCoreModel):
description="Name of the data asset.",
title="Data Asset Name",
)
# We'll set created and last_modified defaults using the root_validator
# to ensure they're synced on creation
created: datetime = Field(
default_factory=datetime.utcnow,
created: AwareDatetimeWithDefault = Field(
default_factory=lambda: datetime.now(tz=timezone.utc),
title="Created",
description="The utc date and time the data asset created.",
)
last_modified: datetime = Field(
default_factory=datetime.utcnow,
last_modified: AwareDatetimeWithDefault = Field(
default_factory=lambda: datetime.now(tz=timezone.utc),
title="Last Modified",
description="The utc date and time that the data asset was last modified.",
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
from datetime import datetime, time, timezone
from unittest.mock import MagicMock, call, patch
import uuid

from aind_data_schema_models.modalities import Modality
from aind_data_schema_models.organizations import Organization
Expand Down Expand Up @@ -536,6 +537,31 @@ def test_create_from_core_jsons_corrupt(self, mock_is_dict_corrupt: MagicMock, m
any_order=True,
)

def test_last_modified(self):
"""Test that the last_modified field enforces timezones"""
m = Metadata.model_construct(
name="name",
location="location",
id=uuid.uuid4(),
)
m_dict = m.model_dump()
m_dict["_id"] = m_dict.pop("id")

# Test that naive datetime is coerced to timezone-aware datetime
date = "2022-11-22T08:43:00"
date_with_timezone = datetime.fromisoformat(date).astimezone()
m_dict["last_modified"] = "2022-11-22T08:43:00"
m2 = Metadata(**m_dict)
self.assertIsNotNone(m2)
self.assertEqual(m2.last_modified, date_with_timezone)

# Test that timezone-aware datetime is not coerced
date_minus = "2022-11-22T08:43:00-07:00"
m_dict["last_modified"] = date_minus
m3 = Metadata(**m_dict)
self.assertIsNotNone(m3)
self.assertEqual(m3.last_modified, datetime.fromisoformat(date_minus))


if __name__ == "__main__":
unittest.main()
Loading