diff --git a/template/{{app_name}}/tests/src/util/test_datetime_util.py b/template/{{app_name}}/tests/src/util/test_datetime_util.py index d6bf02d9..e38ee0d5 100644 --- a/template/{{app_name}}/tests/src/util/test_datetime_util.py +++ b/template/{{app_name}}/tests/src/util/test_datetime_util.py @@ -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( @@ -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() diff --git a/template/{{app_name}}/tests/src/util/test_file_util.py b/template/{{app_name}}/tests/src/util/test_file_util.py new file mode 100644 index 00000000..26369fc5 --- /dev/null +++ b/template/{{app_name}}/tests/src/util/test_file_util.py @@ -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 diff --git a/template/{{app_name}}/tests/src/util/test_string_utils.py b/template/{{app_name}}/tests/src/util/test_string_utils.py new file mode 100644 index 00000000..3ef3f170 --- /dev/null +++ b/template/{{app_name}}/tests/src/util/test_string_utils.py @@ -0,0 +1,14 @@ +from src.util.string_utils import join_list + + +def test_join_list(): + assert join_list(None) == "" + 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"