Skip to content

Commit

Permalink
Add utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatorelaiso authored Jul 26, 2023
1 parent 941a425 commit bf936a7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
84 changes: 84 additions & 0 deletions pyeudiw/tests/tools/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

import datetime
import sys
import freezegun

import pytest

from pyeudiw.tools.utils import exp_from_now, iat_now, random_token


def test_make_timezone_aware():
# TODO: test the function after it is implemented
pass


def frozen_time(fake_now, function, *args):
with freezegun.freeze_time(fake_now):
return function(*args)


@pytest.mark.parametrize("fake_now, timestamp", [
("2020-12-31 12:00:00", 1609416000),
("2000-10-02 12:23:14", 970489394),
("1992-09-03 22:00:00", 715557600),
])
def test_iat_now(fake_now, timestamp):
iat = frozen_time(fake_now=fake_now, function=iat_now)
assert iat == timestamp


@pytest.mark.parametrize("fake_now, delta_mins, timestamp", [
("2020-12-31 12:00:00", 0, 1609416000),
("2000-10-02 12:23:14", 1, 970489454),
("1992-09-03 22:00:00", 2, 715557720),
])
def test_exp_from_now(fake_now, delta_mins, timestamp):
exp = frozen_time(fake_now, exp_from_now, delta_mins)
assert exp == timestamp


def test_datetime_from_timestamp():
# TODO: test the function after it is implemented
pass


def test_get_http_url():
# TODO: test the function after it is implemented
pass



@pytest.mark.parametrize("n", [
-1, 0, 1, 2, 3, 10, 999, 10**1000, 2.,
sys.maxsize, sys.maxsize - 1,
#sys.maxsize // 2 -1,
"1"])
def test_random_token(n):
if type(n) != int:
with pytest.raises(TypeError):
random_token(n)
return

if n < 0:
with pytest.raises(ValueError):
random_token(n)
return

if n >= sys.maxsize - 32:
with pytest.raises(OverflowError):
random_token(n)
return

rand = random_token(n)

if (n == 0):
assert rand == ''
return

assert rand
assert len(rand) == n * 2
hex = int(rand, 16)
assert hex
assert type(hex) == int

5 changes: 2 additions & 3 deletions pyeudiw/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import timezone
# from django.utils.timezone import make_aware
from secrets import token_hex

Expand All @@ -20,8 +19,8 @@ def iat_now() -> int:


def exp_from_now(minutes: int = 33) -> int:
_now = timezone.localtime()
return int((_now + datetime.timedelta(minutes=minutes)).timestamp())
now = datetime.datetime.now()
return int((now + datetime.timedelta(minutes=minutes)).timestamp())


def datetime_from_timestamp(value) -> datetime.datetime:
Expand Down

0 comments on commit bf936a7

Please sign in to comment.