Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PY-606][external] Added tests for CSV tag uploads #727

Merged
merged 9 commits into from
Dec 12, 2023
52 changes: 52 additions & 0 deletions tests/darwin/importer/formats/import_csv_tags_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import csv
from pathlib import Path
from typing import List, Optional

import pytest

from darwin.datatypes import AnnotationFile
from darwin.importer.formats.csv_tags import parse_path


class TestParsePath:
@pytest.fixture
def file_path(self, tmp_path: Path):
path = tmp_path / "annotation.csv"
yield path
path.unlink()

def test_it_returns_none_if_file_extension_is_not_csv(self):
path = Path("path/to/file.xml")
assert parse_path(path) is None

def test_it_parses_csv_file_correctly(self, file_path: Path):
with file_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["image1.jpg", "tag1", "tag2"])
writer.writerow(["image2.jpg", "tag3", "tag4"])

annotation_files: Optional[List[AnnotationFile]] = parse_path(file_path)
assert annotation_files is not None

assert len(annotation_files) == 2
assert annotation_files[0].filename == "image1.jpg"
assert len(annotation_files[0].annotations) == 2
assert annotation_files[1].filename == "image2.jpg"
assert len(annotation_files[1].annotations) == 2

def test_folders_paths_are_parsed_correctly(self, file_path: Path):
with file_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["folder1/folder2/image1.jpg", "tag1", "tag2"])
writer.writerow(["folder/image2.jpg", "tag3", "tag4"])

annotation_files: Optional[List[AnnotationFile]] = parse_path(file_path)
assert annotation_files is not None

assert len(annotation_files) == 2
assert annotation_files[0].filename == "image1.jpg"
assert annotation_files[0].remote_path == "/folder1/folder2"
assert len(annotation_files[0].annotations) == 2
assert annotation_files[1].filename == "image2.jpg"
assert annotation_files[1].remote_path == "/folder"
assert len(annotation_files[1].annotations) == 2
70 changes: 70 additions & 0 deletions tests/darwin/importer/formats/import_csv_tags_video_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import csv
from pathlib import Path
from typing import List, Optional

import pytest

import darwin.datatypes as dt
from darwin.importer.formats.csv_tags_video import parse_path


class TestParsePathVideo:
@pytest.fixture
def file_path(self, tmp_path: Path):
path = tmp_path / "annotation_video.csv"
yield path
path.unlink()

def test_it_returns_none_if_file_extension_is_not_csv(self):
path = Path("path/to/file.xml")
assert parse_path(path) is None

def test_it_parses_csv_file_correctly(self, file_path: Path):
with file_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["video1.mp4", "tag1", "1", "10"])
writer.writerow(["video2.mp4", "tag2", "5", "15"])

annotation_files: Optional[List[dt.AnnotationFile]] = parse_path(file_path)
assert annotation_files is not None

assert len(annotation_files) == 2
assert annotation_files[0].filename == "video1.mp4"
assert len(annotation_files[0].annotations) == 1
assert annotation_files[1].filename == "video2.mp4"
assert len(annotation_files[1].annotations) == 1

def test_folders_paths_are_parsed_correctly(self, file_path: Path):
with file_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["folder1/folder2/video1.mp4", "tag1", "1", "10"])
writer.writerow(["folder/video2.mp4", "tag2", "5", "15"])

annotation_files: Optional[List[dt.AnnotationFile]] = parse_path(file_path)
assert annotation_files is not None
assert len(annotation_files) == 2
assert annotation_files[0].filename == "video1.mp4"
if annotation_files[0].remote_path is not None:
assert Path(annotation_files[0].remote_path) == Path("/folder1/folder2")
assert len(annotation_files[0].annotations) == 1
assert annotation_files[1].filename == "video2.mp4"
if annotation_files[1].remote_path is not None:
assert Path(annotation_files[1].remote_path) == Path("/folder")

def test_keyframes_are_recorded_correctly(self, file_path: Path):
with file_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["video1.mp4", "tag1", "1", "10"])

annotation_files: Optional[List[dt.AnnotationFile]] = parse_path(file_path)
assert annotation_files is not None

assert len(annotation_files) == 1
assert annotation_files[0].filename == "video1.mp4"
assert len(annotation_files[0].annotations) == 1

video_annotation = annotation_files[0].annotations[0]
assert isinstance(video_annotation, dt.VideoAnnotation)

# Check that the keyframes are recorded correctly
assert video_annotation.keyframes == {i: i == 1 for i in range(1, 11)}