Skip to content

Commit 0847754

Browse files
committed
tests for better logic handling
1 parent 45ab34d commit 0847754

File tree

1 file changed

+143
-1
lines changed

1 file changed

+143
-1
lines changed

_delphi_utils_python/tests/test_archive.py

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
from boto3 import Session
77
from git import Repo, exc
8+
import mock
89
from moto import mock_s3
910
import numpy as np
1011
import pandas as pd
1112
from pandas.testing import assert_frame_equal
1213
import pytest
1314

14-
from delphi_utils import ArchiveDiffer, GitArchiveDiffer, S3ArchiveDiffer
15+
from delphi_utils.archive import ArchiveDiffer, GitArchiveDiffer, S3ArchiveDiffer,\
16+
archiver_from_params
1517

1618
CSV_DTYPES = {"geo_id": str, "val": float, "se": float, "sample_size": float}
1719

@@ -467,3 +469,143 @@ def test_run(self, tmp_path):
467469
assert_frame_equal(
468470
pd.read_csv(join(export_dir, "csv1.csv"), dtype=CSV_DTYPES),
469471
csv1_diff)
472+
473+
474+
class TestFromParams:
475+
"""Tests for creating archive differs from params."""
476+
477+
def test_null_creation(self):
478+
"""Test that a None object is created with no "archive" params."""
479+
assert archiver_from_params({"common": {}}) is None
480+
481+
@mock.patch("delphi_utils.archive.GitArchiveDiffer")
482+
def test_get_git_archiver(self, mock_archiver):
483+
"""Test that GitArchiveDiffer is created successfully."""
484+
params = {
485+
"common": {
486+
"export_dir": "dir"
487+
},
488+
"archive": {
489+
"cache_dir": "cache",
490+
"branch_name": "branch",
491+
"override_dirty": True,
492+
"commit_partial_success": True,
493+
"commit_message": "msg"
494+
}
495+
}
496+
497+
archiver_from_params(params)
498+
mock_archiver.assert_called_once_with(
499+
export_dir="dir",
500+
cache_dir="cache",
501+
branch_name="branch",
502+
override_dirty=True,
503+
commit_partial_success=True,
504+
commit_message="msg"
505+
)
506+
507+
@mock.patch("delphi_utils.archive.GitArchiveDiffer")
508+
def test_get_git_archiver_with_defaults(self, mock_archiver):
509+
"""Test that GitArchiveDiffer is created successfully without optional arguments."""
510+
params = {
511+
"common": {
512+
"export_dir": "dir"
513+
},
514+
"archive": {
515+
"cache_dir": "cache",
516+
"branch_name": "branch",
517+
"commit_message": "msg"
518+
}
519+
}
520+
521+
archiver_from_params(params)
522+
mock_archiver.assert_called_once_with(
523+
export_dir="dir",
524+
cache_dir="cache",
525+
branch_name="branch",
526+
commit_message="msg"
527+
)
528+
@mock.patch("delphi_utils.archive.S3ArchiveDiffer")
529+
def test_get_s3_archiver(self, mock_archiver):
530+
"""Test that S3ArchiveDiffer is created successfully."""
531+
params = {
532+
"common": {
533+
"export_dir": "dir"
534+
},
535+
"archive": {
536+
"cache_dir": "cache",
537+
"bucket_name": "bucket",
538+
"indicator_prefix": "ind",
539+
"aws_credentials": {"pass": "word"}
540+
}
541+
}
542+
543+
archiver_from_params(params)
544+
mock_archiver.assert_called_once_with(
545+
export_dir="dir",
546+
cache_dir="cache",
547+
bucket_name="bucket",
548+
indicator_prefix="ind",
549+
aws_credentials={"pass": "word"}
550+
)
551+
552+
def test_get_s3_archiver_without_required(self):
553+
"""Test that S3ArchiveDiffer is not created without required arguments."""
554+
params = {
555+
"common": {
556+
"export_dir": "dir"
557+
},
558+
"archive": {
559+
"cache_dir": "cache",
560+
"bucket_name": "bucket"
561+
}
562+
}
563+
564+
with pytest.raises(AssertionError,
565+
match="Missing indicator_prefix in params"):
566+
archiver_from_params(params)
567+
568+
params["archive"]["indicator_prefix"] = "prefix"
569+
with pytest.raises(AssertionError,
570+
match="Missing aws_credentials in params"):
571+
archiver_from_params(params)
572+
573+
@mock.patch("delphi_utils.archive.FilesystemArchiveDiffer")
574+
def test_get_filesystem_archiver(self, mock_archiver):
575+
"""Test that FilesystemArchiveDiffer is created successfully."""
576+
params = {
577+
"common": {
578+
"export_dir": "dir"
579+
},
580+
"archive": {
581+
"cache_dir": "cache"
582+
}
583+
}
584+
585+
archiver_from_params(params)
586+
mock_archiver.assert_called_once_with(
587+
export_dir="dir",
588+
cache_dir="cache"
589+
)
590+
591+
def test_get_filesystem_archiver_with_extra_params(self):
592+
"""Test that FilesystemArchiveDiffer is not created with extra parameters."""
593+
params = {
594+
"common": {
595+
"export_dir": "dir"
596+
},
597+
"archive": {
598+
"cache_dir": "cache",
599+
"indicator_prefix": "prefix"
600+
}
601+
}
602+
603+
with pytest.raises(AssertionError,
604+
match="If you intended to run"):
605+
archiver_from_params(params)
606+
607+
del params["archive"]["cache_dir"]
608+
del params["archive"]["indicator_prefix"]
609+
with pytest.raises(AssertionError,
610+
match="If you intended to run"):
611+
archiver_from_params(params)

0 commit comments

Comments
 (0)