Skip to content
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
22 changes: 20 additions & 2 deletions template/{{app_name}}/tests/src/util/test_datetime_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import datetime, timezone
from datetime import date, datetime, timezone

import pytest
import pytz

from src.util.datetime_util import adjust_timezone
from src.util.datetime_util import adjust_timezone, datetime_str_to_date, utcnow


@pytest.mark.parametrize(
Expand Down Expand Up @@ -50,3 +50,21 @@ def test_adjust_timezone_from_non_utc(timezone_name, expected_output):
# in a few places that don't observe DST (the US timezones are all 1 hour closer to UTC)

assert adjust_timezone(input_datetime, timezone_name).isoformat() == expected_output


@pytest.mark.parametrize(
"datetime_str, expected_output",
[
("2022-05-31T23:00:00-06:00", date(2022, 5, 31)),
("2022-06-01T14:00:00+09:00", date(2022, 6, 1)),
("", None),
(None, None),
],
)
def test_datetime_str_to_date(datetime_str, expected_output):
assert datetime_str_to_date(datetime_str) == expected_output


def test_utcnow():
assert utcnow().tzinfo == timezone.utc
assert utcnow().date() == datetime.now().date()
83 changes: 83 additions & 0 deletions template/{{app_name}}/tests/src/util/test_file_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os

import pytest
from smart_open import open as smart_open

import src.util.file_util as file_util


def create_file(root_path, file_path):
full_path = os.path.join(root_path, file_path)

if not file_util.is_s3_path(str(full_path)):
os.makedirs(os.path.dirname(full_path), exist_ok=True)

with smart_open(full_path, mode="w") as outfile:
outfile.write("hello")

return full_path


@pytest.mark.parametrize(
"path,is_s3",
[
("s3://bucket/folder/test.txt", True),
("./relative/folder/test.txt", False),
("http://example.com/test.txt", False),
],
)
def test_is_s3_path(path, is_s3):
assert file_util.is_s3_path(path) is is_s3


@pytest.mark.parametrize(
"path,bucket,prefix",
[
("s3://my_bucket/my_key", "my_bucket", "my_key"),
("s3://my_bucket/path/to/directory/", "my_bucket", "path/to/directory/"),
("s3://my_bucket/path/to/file.txt", "my_bucket", "path/to/file.txt"),
],
)
def test_split_s3_url(path, bucket, prefix):
assert file_util.split_s3_url(path) == (bucket, prefix)


@pytest.mark.parametrize(
"path,bucket",
[
("s3://bucket/folder/test.txt", "bucket"),
("s3://bucket_x/folder", "bucket_x"),
("s3://bucket-y/folder/", "bucket-y"),
("s3://bucketz", "bucketz"),
],
)
def test_get_s3_bucket(path, bucket):
assert file_util.get_s3_bucket(path) == bucket


@pytest.mark.parametrize(
"path,file_key",
[
("s3://bucket/folder/test.txt", "folder/test.txt"),
("s3://bucket_x/file.csv", "file.csv"),
("s3://bucket-y/folder/path/to/abc.zip", "folder/path/to/abc.zip"),
("./folder/path", "/folder/path"),
("sftp://folder/filename", "filename"),
],
)
def test_get_s3_file_key(path, file_key):
assert file_util.get_s3_file_key(path) == file_key


@pytest.mark.parametrize(
"path,file_name",
[
("s3://bucket/folder/test.txt", "test.txt"),
("s3://bucket_x/file.csv", "file.csv"),
("s3://bucket-y/folder/path/to/abc.zip", "abc.zip"),
("./folder/path", "path"),
("sftp://filename", "filename"),
],
)
def test_get_s3_file_name(path, file_name):
assert file_util.get_file_name(path) == file_name
14 changes: 14 additions & 0 deletions template/{{app_name}}/tests/src/util/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from src.util.string_utils import join_list


def test_join_list():
assert join_list(None) == ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope of this PR but why do we even have this function

assert join_list(None, ",") == ""
assert join_list(None, "|") == ""
assert join_list([]) == ""
assert join_list([], ",") == ""
assert join_list([], "|") == ""

assert join_list(["a", "b", "c"]) == "a\nb\nc"
assert join_list(["a", "b", "c"], ",") == "a,b,c"
assert join_list(["a", "b", "c"], "|") == "a|b|c"