Skip to content

Commit

Permalink
Merge pull request #45 from fintoc-com/master
Browse files Browse the repository at this point in the history
Release 2.2.1
  • Loading branch information
daleal authored Dec 16, 2021
2 parents 0b1e3f5 + f33d0fb commit 2e47b14
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
1 change: 1 addition & 0 deletions fintoc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
API_VERSION = "v1"

LINK_HEADER_PATTERN = r'<(?P<url>.*)>;\s*rel="(?P<rel>.*)"'
DATE_TIME_PATTERN = "%Y-%m-%dT%H:%M:%S.%fZ"
15 changes: 10 additions & 5 deletions fintoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from importlib import import_module

import httpx
from dateutil import parser

from fintoc.constants import DATE_TIME_PATTERN
from fintoc.errors import FintocError


Expand All @@ -25,7 +25,7 @@ def is_iso_datetime(string):
Otherwise, return False.
"""
try:
parser.isoparse(string)
datetime.datetime.strptime(string, DATE_TIME_PATTERN)
return True
except ValueError:
return False
Expand All @@ -43,7 +43,7 @@ def get_resource_class(snake_resource_name, value={}):
except AttributeError:
return getattr(module, "GenericFintocResource")
if isinstance(value, str) and is_iso_datetime(value):
return parser.isoparse
return objetize_datetime
return type(value)


Expand Down Expand Up @@ -78,15 +78,20 @@ def serialize(object_):
if callable(getattr(object_, "serialize", None)):
return object_.serialize()
if isinstance(object_, datetime.datetime):
return object_.isoformat()
return object_.strftime(DATE_TIME_PATTERN)
return object_


def objetize_datetime(string):
"""Objetizes a datetime string without checking for correctness."""
return datetime.datetime.strptime(string, DATE_TIME_PATTERN)


def objetize(klass, client, data, handlers={}, methods=[], path=None):
"""Transform the :data: object into an object with class :klass:."""
if data is None:
return None
if klass in [str, int, dict, bool, parser.isoparse]:
if klass in [str, int, dict, bool, objetize_datetime]:
return klass(data)
return klass(client, handlers, methods, path, **data)

Expand Down
2 changes: 1 addition & 1 deletion fintoc/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Module to hold the version utilities."""

version_info = (2, 2, 0)
version_info = (2, 2, 1)
__version__ = ".".join([str(x) for x in version_info])
29 changes: 1 addition & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fintoc"
version = "2.2.0"
version = "2.2.1"
description = "The official Python client for the Fintoc API."
authors = ["Daniel Leal <[email protected]>", "Nebil Kawas <[email protected]>"]
maintainers = ["Daniel Leal <[email protected]>"]
Expand All @@ -21,7 +21,6 @@ exclude = [
[tool.poetry.dependencies]
python = "^3.6"
httpx = ">=0.16, < 1.0"
python-dateutil = ">=2.8, < 3.0"

[tool.poetry.dev-dependencies]
black = "^20.8b1"
Expand Down
32 changes: 28 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import httpx
import pytest
from dateutil import parser

from fintoc.constants import DATE_TIME_PATTERN
from fintoc.errors import ApiError, FintocError
from fintoc.resources import GenericFintocResource, Link
from fintoc.utils import (
Expand All @@ -13,6 +13,7 @@
get_resource_class,
is_iso_datetime,
objetize,
objetize_datetime,
objetize_generator,
serialize,
singularize,
Expand Down Expand Up @@ -59,10 +60,14 @@ def test_valid_iso_format(self):
valid_iso_datetime_string = "2021-08-13T13:40:40.811Z"
assert is_iso_datetime(valid_iso_datetime_string)

def test_invalid_iso_format(self):
def test_invalid_iso_string_format(self):
invalid_iso_datetime_string = "This is not a date"
assert not is_iso_datetime(invalid_iso_datetime_string)

def test_invalid_iso_number_format(self):
invalid_iso_datetime_string = "1105122"
assert not is_iso_datetime(invalid_iso_datetime_string)


class TestGetResourceClass:
def test_default_valid_resource(self):
Expand All @@ -78,7 +83,7 @@ def test_default_invalid_resource(self):
def test_iso_datetime_resource(self):
resource = "any_resource"
klass = get_resource_class(resource, value="2021-08-13T13:40:40.811Z")
assert klass is parser.isoparse
assert klass is objetize_datetime

def test_string_resource(self):
resource = "any_resource"
Expand Down Expand Up @@ -178,7 +183,7 @@ def test_datetime_serialization(self):
now = datetime.datetime.now()
assert isinstance(now, datetime.datetime)
assert isinstance(serialize(now), str)
assert serialize(now) == now.isoformat()
assert serialize(now) == now.strftime(DATE_TIME_PATTERN)

def test_object_with_serialize_method_serialization(self):
data = {"a": "b", "c": "d"}
Expand Down Expand Up @@ -212,6 +217,25 @@ def test_complete_objetization(self):
assert object_.data["id"] == self.data["id"]


class TestObjetizeDateTime:
def setup_method(self):
self.valid_string = "2021-12-16T12:24:44.397000Z"
self.obviously_invalid_string = "This is not a date"
self.deceptively_invalid_string = "1105122"

def test_valid_string(self):
parsed = objetize_datetime(self.valid_string)
assert isinstance(parsed, datetime.datetime)

def test_obviously_invalid_string(self):
with pytest.raises(ValueError):
objetize_datetime(self.obviously_invalid_string)

def test_deceptively_invalid_string(self):
with pytest.raises(ValueError):
objetize_datetime(self.deceptively_invalid_string)


class TestObjetizeGenerator:
def setup_method(self):
self.client = "This is a client"
Expand Down

0 comments on commit 2e47b14

Please sign in to comment.