Skip to content

Commit

Permalink
Merge pull request #93 from Nautilus-Cyberneering/issue-92
Browse files Browse the repository at this point in the history
New Base image purpose code (52)
  • Loading branch information
josecelano authored Mar 1, 2022
2 parents a636117 + 5b75e64 commit 7871bce
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/nautilus_librarian/mods/dvc/domain/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def get_files_to_commit(self, base_img_relative_path) -> List[str]:
For example:
For the Base image "data/000001/42/000001-42.600.2.tif", these are
For the Base image "data/000001/52/000001-52.600.2.tif", these are
the files tracked on the git repo:
- data/000001/42/.gitignore
- data/000001/42/000001-42.600.2.tif.dvc
- data/000001/52/.gitignore
- data/000001/52/000001-52.600.2.tif.dvc
"""
base_img_dir = os.path.dirname(base_img_relative_path)

Expand Down
2 changes: 1 addition & 1 deletion src/nautilus_librarian/mods/dvc/domain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def extract_added_files_from_dvc_diff(dvc_diff_json):
{
"added": [
{"path": "data/000001/32/000001-32.600.2.tif"},
{"path": "data/000001/42/000001-42.600.2.tif"},
{"path": "data/000001/52/000001-52.600.2.tif"},
],
"deleted": [],
"modified": [],
Expand Down
6 changes: 3 additions & 3 deletions src/nautilus_librarian/mods/namecodes/domain/filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class PurposeCode(Enum):
GOLD_INDEX = 30
GOLD_METADATA = 31
GOLD_IMAGE = 32
BASE_INDEX = 40
BASE_METADATA = 41
BASE_IMAGE = 42
BASE_INDEX = 50
BASE_METADATA = 51
BASE_IMAGE = 52

def __str__(self):
return "%s" % self.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def validate_artwork_id(artwork_id):
def validate_purpose_code(purpose_code):
if purpose_code == "":
raise FilenameException(
"Missing purpose code. Purpose code should be: 32 or 42"
"Missing purpose code. Purpose code should be: 32 (Gold) or 52 (Base)"
)
if int(purpose_code) not in [32, 52]:
raise FilenameException(
"Wrong purpose code. Purpose code should be: 32 (Gold) or 52 (Base)"
)
if int(purpose_code) not in [32, 42]:
raise FilenameException("Wrong purpose code. Purpose code should be: 32 or 42")


def validate_transformation_code(transformation_code):
Expand Down
2 changes: 1 addition & 1 deletion src/nautilus_librarian/mods/namecodes/typer/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def validate_filename_command(filename: str):
More information about the specification on: https://github.com/da2ce7/nautilus-namecodes.
Filename examples: 000000-32.600.2.tif, 000001-42.600.2.tif.
Filename examples: 000000-32.600.2.tif, 000001-52.600.2.tif.
EXAMPLES:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ def test_gold_image_localization():


def test_base_image_localization():
gold_image_filename = Filename("000001-42.600.2.tif")
gold_image_filename = Filename("000001-52.600.2.tif")

folder = file_locator(gold_image_filename)

assert folder == "data/000001/42"
assert folder == "data/000001/52"


def it_should_return_the_corresponding_base_image_filename_from_the_gold_image():
gold_image_filename = Filename("000001-32.600.2.tif")

base_image_filename = get_base_image_filename_from_gold_image(gold_image_filename)

assert base_image_filename == Filename("000001-42.600.2.tif")
assert base_image_filename == Filename("000001-52.600.2.tif")
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def test_valid_folder_for_gold_image():


def test_valid_folder_for_base_image():
validate_filepath("data/000001/42/000001-42.600.2.tif")
validate_filepath("data/000001/52/000001-52.600.2.tif")


def test_invalid_folder_for_gold_image():
with pytest.raises(InvalidImageFolderException):
validate_filepath("data/000001/42/000001-32.600.2.tif")
validate_filepath("data/000001/52/000001-32.600.2.tif")


def test_invalid_folder_for_base_image():
with pytest.raises(InvalidImageFolderException):
validate_filepath("data/000001/32/000001-42.600.2.tif")
validate_filepath("data/000001/32/000001-52.600.2.tif")
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def test_files_to_commit(temp_dvc_dir_with_test_content):

api = DvcApiWrapper(temp_dvc_dir_with_test_content)

filepaths = api.get_files_to_commit("data/000001/42/000001-42.600.2.tif")
filepaths = api.get_files_to_commit("data/000001/52/000001-52.600.2.tif")

assert filepaths == [
"data/000001/42/.gitignore",
"data/000001/42/000001-42.600.2.tif.dvc",
"data/000001/52/.gitignore",
"data/000001/52/000001-52.600.2.tif.dvc",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def test_files_to_commit(temp_dvc_dir_with_librarian_test_content):

api = DvcServicesApi(temp_dvc_dir_with_librarian_test_content)

filepaths = api.get_files_to_commit("data/000001/42/000001-42.600.2.tif")
filepaths = api.get_files_to_commit("data/000001/52/000001-52.600.2.tif")

assert filepaths == [
"data/000001/42/.gitignore",
"data/000001/42/000001-42.600.2.tif.dvc",
"data/000001/52/.gitignore",
"data/000001/52/000001-52.600.2.tif.dvc",
]
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_extract_added_files_from_dvc_diff():
dvc_diff = {
"added": [
{"path": "data/000001/32/000001-32.600.2.tif"},
{"path": "data/000001/42/000001-42.600.2.tif"},
{"path": "data/000001/52/000001-52.600.2.tif"},
],
"deleted": [],
"modified": [],
Expand All @@ -43,7 +43,7 @@ def test_extract_added_files_from_dvc_diff():

assert result == [
"data/000001/32/000001-32.600.2.tif",
"data/000001/42/000001-42.600.2.tif",
"data/000001/52/000001-52.600.2.tif",
]


Expand All @@ -54,7 +54,7 @@ def test_modified_files_from_dvc_diff():
"deleted": [],
"modified": [
{"path": "data/000001/32/000001-32.600.2.tif"},
{"path": "data/000001/42/000001-42.600.2.tif"},
{"path": "data/000001/52/000001-52.600.2.tif"},
],
"renamed": [],
}
Expand All @@ -63,7 +63,7 @@ def test_modified_files_from_dvc_diff():

assert result == [
"000001-32.600.2.tif",
"000001-42.600.2.tif",
"000001-52.600.2.tif",
]


Expand All @@ -72,7 +72,7 @@ def test_extract_deleted_files_from_dvc_diff():
dvc_diff = {
"deleted": [
{"path": "data/000001/32/000001-32.600.2.tif"},
{"path": "data/000001/42/000001-42.600.2.tif"},
{"path": "data/000001/52/000001-52.600.2.tif"},
],
"added": [],
"modified": [],
Expand All @@ -85,7 +85,7 @@ def test_extract_deleted_files_from_dvc_diff():

assert result == [
"data/000001/32/000001-32.600.2.tif",
"data/000001/42/000001-42.600.2.tif",
"data/000001/52/000001-52.600.2.tif",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_base_image_instantiation_from_gold_image():

base_image = gold_image_filename.generate_base_image_filename()

assert str(base_image) == str(Filename("000001-42.600.2.tif"))
assert str(base_image) == str(Filename("000001-52.600.2.tif"))


def test_invalid_filename():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_filter_gold_images_from_file_list():

original_file_list = [
"data/000001/32/000001-32.600.2.tif",
"data/000001/42/000001-42.600.2.tif",
"data/000001/52/000001-52.600.2.tif",
]

filtered_file_list = filter_gold_images(original_file_list)
Expand All @@ -76,11 +76,11 @@ def test_filter_base_images_from_file_list():

original_file_list = [
"data/000001/32/000001-32.600.2.tif",
"data/000001/42/000001-42.600.2.tif",
"data/000001/52/000001-52.600.2.tif",
]

filtered_file_list = filter_base_images(original_file_list)

expected_file_list = ["data/000001/42/000001-42.600.2.tif"]
expected_file_list = ["data/000001/52/000001-52.600.2.tif"]

assert filtered_file_list == expected_file_list
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_invalid_artwork_id(filename):
validate_filename(filename)


@pytest.mark.parametrize("filename", [("000001-32.600.2.tif"), ("000001-42.600.2.tif")])
@pytest.mark.parametrize("filename", [("000001-32.600.2.tif"), ("000001-52.600.2.tif")])
def test_valid_purpose_code(filename):
validate_filename(filename)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def sample_gold_image_relative_path():

@pytest.fixture(scope="session")
def sample_base_image_absolute_path(workflows_fixtures_dir):
base_image_path = f"{workflows_fixtures_dir}/images/000001-42.600.2.tif"
base_image_path = f"{workflows_fixtures_dir}/images/000001-52.600.2.tif"
return base_image_path


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_get_new_gold_images_from_dvc_diff():
dvc_diff = {
"added": [
{"path": "data/000001/32/000001-32.600.2.tif"},
{"path": "data/000001/42/000001-42.600.2.tif"},
{"path": "data/000001/52/000001-52.600.2.tif"},
],
"deleted": [],
"modified": [],
Expand All @@ -44,28 +44,28 @@ def test_calculate_the_corresponding_base_image_from_gold_image():
)

assert base_image_path == (
"data/000001/42/000001-42.600.2.tif", # relative path
"/home/repo/data/000001/42/000001-42.600.2.tif", # absolute path
"data/000001/52/000001-52.600.2.tif", # relative path
"/home/repo/data/000001/52/000001-52.600.2.tif", # absolute path
)


def remove_base_image_dvc_files(temp_git_dir):
os.remove(f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif.dvc")
os.remove(f"{temp_git_dir}/data/000001/42/.gitignore")
os.remove(f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif.dvc")
os.remove(f"{temp_git_dir}/data/000001/52/.gitignore")


def overwrite_base_image(fixtures_dir, temp_git_dir):
shutil.copyfile(
f"{fixtures_dir}/images/000001-42.600.2-modified.tif",
f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif",
f"{fixtures_dir}/images/000001-52.600.2-modified.tif",
f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif",
)


def rename_base_image(temp_git_dir):
execute_shell_command(
"""
mkdir -p data/000002/42
dvc move data/000001/42/000001-42.600.2.tif data/000002/42/000002-42.600.2.tif
mkdir -p data/000002/52
dvc move data/000001/52/000001-52.600.2.tif data/000002/52/000002-52.600.2.tif
""",
cwd=temp_git_dir,
print_output=True,
Expand All @@ -75,7 +75,7 @@ def rename_base_image(temp_git_dir):
def add_base_image_to_dvc(temp_git_dir):
execute_shell_command(
"""
dvc add data/000001/42/000001-42.600.2.tif
dvc add data/000001/52/000001-52.600.2.tif
""",
cwd=temp_git_dir,
print_output=True,
Expand Down Expand Up @@ -145,12 +145,12 @@ def given_a_dvc_diff_object_with_a_new_gold_image_it_should_commit_the_added_bas
repo = Repo(temp_git_dir)
commit = repo.commit(repo.heads[0].commit) # latest commit on the branch

assert_commit_summary(commit, "feat: new base image: 000001-42.600.2.tif")
assert_commit_summary(commit, "feat: new base image: 000001-52.600.2.tif")

# Assert the commit contains the right files
expected_commit_stats_files = {
"data/000001/42/.gitignore": {"insertions": 1, "deletions": 0, "lines": 1},
"data/000001/42/000001-42.600.2.tif.dvc": {
"data/000001/52/.gitignore": {"insertions": 1, "deletions": 0, "lines": 1},
"data/000001/52/000001-52.600.2.tif.dvc": {
"insertions": 4,
"deletions": 0,
"lines": 4,
Expand Down Expand Up @@ -201,11 +201,11 @@ def given_a_dvc_diff_object_with_a_gold_image_deleton_it_should_commit_the_base_
repo = Repo(temp_git_dir)
commit = repo.commit(repo.heads[0].commit) # latest commit on the branch

assert_commit_summary(commit, "feat: deleted base image: 000001-42.600.2.tif")
assert_commit_summary(commit, "feat: deleted base image: 000001-52.600.2.tif")

expected_commit_stats_files = {
"data/000001/42/.gitignore": {"insertions": 0, "deletions": 1, "lines": 1},
"data/000001/42/000001-42.600.2.tif.dvc": {
"data/000001/52/.gitignore": {"insertions": 0, "deletions": 1, "lines": 1},
"data/000001/52/000001-52.600.2.tif.dvc": {
"insertions": 0,
"deletions": 4,
"lines": 4,
Expand Down Expand Up @@ -257,16 +257,16 @@ def given_a_dvc_diff_object_with_a_gold_image_rename_it_should_commit_the_base_i
commit = repo.commit(repo.heads[0].commit)

expected_commit_stats_files = {
"data/000001/42/.gitignore": {"insertions": 0, "deletions": 1, "lines": 1},
"data/000002/42/.gitignore": {"insertions": 3, "deletions": 0, "lines": 3},
"data/{000001/42/000001-42.600.2.tif.dvc => 000002/42/000002-42.600.2.tif.dvc}": {
"data/000001/52/.gitignore": {"insertions": 0, "deletions": 1, "lines": 1},
"data/000002/52/.gitignore": {"insertions": 3, "deletions": 0, "lines": 3},
"data/{000001/52/000001-52.600.2.tif.dvc => 000002/52/000002-52.600.2.tif.dvc}": {
"insertions": 1,
"deletions": 1,
"lines": 2,
},
}
assert_commit_summary(
commit, "feat: renamed base image: 000001-42.600.2.tif -> 000002-42.600.2.tif"
commit, "feat: renamed base image: 000001-52.600.2.tif -> 000002-52.600.2.tif"
)
assert_commit_content(commit, expected_commit_stats_files, git_user)
assert_commit_signingkey(commit, temp_git_dir, git_user)
Expand Down Expand Up @@ -309,10 +309,10 @@ def given_a_dvc_diff_object_with_a_gold_image_modification_it_should_commit_the_

commit = repo.commit(repo.heads[0].commit)

assert_commit_summary(commit, "feat: modified base image: 000001-42.600.2.tif")
assert_commit_summary(commit, "feat: modified base image: 000001-52.600.2.tif")

expected_commit_stats_files = {
"data/000001/42/000001-42.600.2.tif.dvc": {
"data/000001/52/000001-52.600.2.tif.dvc": {
"insertions": 2,
"deletions": 2,
"lines": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def given_a_diff_structure_with_deleted_gold_image_it_should_delete_base_images(
execute_shell_command(
"""
ls -la
dvc add data/000001/42/000001-42.600.2.tif
dvc add data/000001/52/000001-52.600.2.tif
""",
cwd=temp_git_dir,
)

assert path.exists(f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif.dvc")
assert path.exists(f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif.dvc")

result = delete_base_images(
compact_json(dvc_diff_with_added_gold_image), temp_git_dir
)

assert result.code == ResultCode.CONTINUE
assert not path.exists(f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif.dvc")
assert not path.exists(f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif.dvc")
assert result.contains_text("successfully deleted")
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ def given_a_diff_structure_with_added_gold_image_it_should_generate_base_image(
)

# Assert Base image was created
assert os.path.isfile(f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif")
assert os.path.isfile(f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif")

# DVC Asserts

# Assert dvc files were created
assert os.path.isfile(f"{temp_git_dir}/data/000001/42/000001-42.600.2.tif.dvc")
assert os.path.isfile(f"{temp_git_dir}/data/000001/42/.gitignore")
assert os.path.isfile(f"{temp_git_dir}/data/000001/52/000001-52.600.2.tif.dvc")
assert os.path.isfile(f"{temp_git_dir}/data/000001/52/.gitignore")

# Assert Base image was pushed to local "remote" storage
dvc_status_output_json = dvc(temp_git_dir).status_remote("localremote")
expected_status_new = {"data/000001/42/000001-42.600.2.tif": "new"}
expected_status_new = {"data/000001/52/000001-52.600.2.tif": "new"}
expected_status_empty = {}
assert (
expected_status_new == dvc_status_output_json
Expand Down
Loading

0 comments on commit 7871bce

Please sign in to comment.