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

FIX: Convert fluid contact to lowercase #679

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 14 additions & 0 deletions src/fmu/dataio/datastructure/meta/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from typing import Any, Dict, List, Literal, Optional, Union

from pydantic import (
Expand All @@ -9,6 +10,7 @@
GetJsonSchemaHandler,
NaiveDatetime,
RootModel,
field_validator,
model_validator,
)
from pydantic_core import CoreSchema
Expand Down Expand Up @@ -78,6 +80,18 @@ class FluidContact(BaseModel):
)
truncated: bool = Field(default=False)

@field_validator("contact", mode="before")
def contact_to_lowercase(cls, v: str) -> str:
if any(c.isupper() for c in v):
warnings.warn(
f"You've defined the fluid contact as '{v}' which contains uppercase "
"characters. In a future version we may require that fluid contacts "
"should be all lowercase. To ensure future compatibility you should "
f"change this value to '{v.lower()}'.",
UserWarning,
)
return v.lower()


class FieldOutline(BaseModel):
"""
Expand Down
1 change: 0 additions & 1 deletion src/fmu/dataio/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class PolygonsProxy(Polygons): ...
"Collection of 'inferrable' objects with metadata deduction capabilities",
]


Parameters: TypeAlias = Annotated[
MutableMapping[str, Union[str, float, int, None, "Parameters"]],
"Nested or flat configurations for dynamically structured parameters.",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_units/test_contents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Explicitly test all allowed contents."""

import pytest
from fmu.dataio.dataio import ExportData
from pydantic import ValidationError

# generic testing of functionality related to content is done elsewhere,
# mainly in test_dataio.py.
Expand Down Expand Up @@ -66,6 +68,28 @@ def test_content_fluid_contact(regsurf, globalconfig2):
assert meta["data"]["content"] == "fluid_contact"


def test_content_fluid_contact_case_insensitive(regsurf, globalconfig2):
"""Test export of the fluid_contact content."""
with pytest.warns(UserWarning, match=r"contains uppercase.+value to 'owc'"):
meta = ExportData(
config=globalconfig2,
name="MyName",
content={"fluid_contact": {"contact": "OWC"}},
).generate_metadata(regsurf)

assert meta["data"]["fluid_contact"]["contact"] == "owc"


def test_content_fluid_contact_raises_on_invalid_contact(regsurf, globalconfig2):
"""Test export of the fluid_contact content."""
with pytest.raises(ValidationError, match="fluid_contact"):
ExportData(
config=globalconfig2,
name="MyName",
content={"fluid_contact": {"contact": "OEC"}},
).generate_metadata(regsurf)


def test_content_kh_product(regsurf, globalconfig2):
"""Test export of the khproduct content."""
meta = ExportData(
Expand Down