Skip to content

Commit

Permalink
Merge pull request #2 from simoneanam/main
Browse files Browse the repository at this point in the history
[ADD] DataReturn dataclass in BaseModels.py
  • Loading branch information
archetipo authored Oct 10, 2024
2 parents faa2963 + 7a432af commit cfab145
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
15 changes: 12 additions & 3 deletions ozonenv/core/BaseModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import logging
import operator
import re
from dataclasses import dataclass
from datetime import datetime
from functools import reduce
from typing import Any
from typing import List, Optional, Dict
from typing import TypeVar
from typing import Optional
from typing import TypeVar, Generic, List, Dict

import typing_extensions

# from datetime import datetime
from dateutil.parser import parse
from pydantic import BaseModel, Field, field_serializer, model_validator
from pydantic import BaseModel, Field, field_serializer
from typing_extensions import Literal

import ozonenv
Expand All @@ -31,6 +32,7 @@
logger = logging.getLogger("asyncio")

T = TypeVar("T", bound=BaseModel)
D = TypeVar("D")
ModelType = TypeVar("ModelType", bound=BaseModel)

default_fields = [
Expand Down Expand Up @@ -1610,3 +1612,10 @@ def components_ext_data_src(cls):
@classmethod
def get_data_model(cls):
return ""


@dataclass
class DataReturn(Generic[D]):
data: D | None = None
fail: bool = False
msg: str = ""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ozon-env"
version = "2.2.16"
version = "2.2.17"
homepage = "https://github.com/archetipo/ozon-env"
description = "Ozon Env API"
readme = "README.md"
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_unit_base_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from ozonenv.core.BaseModels import DataReturn


class TestDataReturn:
def test_default_initialization(self):
"""Test default initialization of DataReturn"""
result = DataReturn()

assert result.data is None # data should default to None
assert result.fail is False # fail should default to False
assert result.msg == "" # msg should default to an empty string

def test_custom_initialization(self):
"""Test custom initialization of DataReturn with various types"""
result = DataReturn(data=42, fail=True, msg="Custom message")

assert result.data == 42
assert result.fail is True
assert result.msg == "Custom message"

def test_list_data(self):
"""Test using a list as the data"""
data = [1, 2, 3]
result = DataReturn(data=data)

assert result.data == data
assert result.data[0] == 1

def test_dict_data(self):
"""Test using a dictionary as the data"""
data = {"key": "value"}
result = DataReturn(data=data)

assert result.data == data
assert result.data["key"] == "value"

def test_none_data(self):
"""Test explicitly passing None to data"""
result = DataReturn(data=None, fail=False, msg="No data")

assert result.data is None
assert result.fail is False
assert result.msg == "No data"

0 comments on commit cfab145

Please sign in to comment.