Skip to content

Commit

Permalink
adding tests and typing to utils_py (#180)
Browse files Browse the repository at this point in the history
* adding tests and typing to utils_py

* added urljoin strip test

* added test for is_project_path
  • Loading branch information
Fl4m3Ph03n1x authored Aug 5, 2021
1 parent d0857a3 commit 10b9b3e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
17 changes: 10 additions & 7 deletions darwin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@
from darwin.config import Config
from darwin.exceptions import OutdatedDarwinJSONFormat, UnsupportedFileType

if TYPE_CHECKING:
from darwin.client import Client


SUPPORTED_IMAGE_EXTENSIONS = [".png", ".jpeg", ".jpg", ".jfif", ".tif", ".tiff", ".bmp", ".svs"]
SUPPORTED_VIDEO_EXTENSIONS = [".avi", ".bpm", ".dcm", ".mov", ".mp4"]
SUPPORTED_EXTENSIONS = SUPPORTED_IMAGE_EXTENSIONS + SUPPORTED_VIDEO_EXTENSIONS


def is_extension_allowed(extension):
def is_extension_allowed(extension: str) -> bool:
"""Returns whether or not the given video or image extension is allowed."""
return extension.lower() in SUPPORTED_EXTENSIONS


def is_image_extension_allowed(extension):
def is_image_extension_allowed(extension: str) -> bool:
"""Returns whether or not the given image extension is allowed."""
return extension.lower() in SUPPORTED_IMAGE_EXTENSIONS


def is_video_extension_allowed(extension):
def is_video_extension_allowed(extension: str) -> bool:
"""Returns whether or not the given video extension is allowed."""
return extension.lower() in SUPPORTED_VIDEO_EXTENSIONS


if TYPE_CHECKING:
from darwin.client import Client


def urljoin(*parts: str) -> str:
"""Take as input an unpacked list of strings and joins them to form an URL"""
return "/".join(part.strip("/") for part in parts)
Expand Down
58 changes: 57 additions & 1 deletion tests/darwin/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
from unittest.mock import MagicMock, patch

from darwin.utils import is_unix_like_os
from darwin.utils import (
is_extension_allowed,
is_image_extension_allowed,
is_project_dir,
is_unix_like_os,
is_video_extension_allowed,
urljoin,
)


def describe_is_extension_allowed():
def it_returns_true_for_allowed_extensions():
assert is_extension_allowed(".png")

def it_returns_false_for_unknown_extensions():
assert not is_extension_allowed(".mkv")


def describe_is_image_extension_allowed():
def it_returns_true_for_allowed_extensions():
assert is_image_extension_allowed(".png")

def it_returns_false_for_unknown_extensions():
assert not is_image_extension_allowed(".not_an_image")


def describe_is_video_extension_allowed():
def it_returns_true_for_allowed_extensions():
assert is_video_extension_allowed(".mp4")

def it_returns_false_for_unknown_extensions():
assert not is_video_extension_allowed(".not_video")


def describe_urljoin():
def it_returns_an_url():
assert urljoin("api", "teams") == "api/teams"

def it_strips_correctly():
assert (
urljoin("http://www.darwin.v7labs.com/", "/users/token_info")
== "http://www.darwin.v7labs.com/users/token_info"
)


def describe_is_project_dir():
def it_returns_true_if_path_is_project_dir(tmp_path):
releases_path = tmp_path / "releases"
releases_path.mkdir()

images_path = tmp_path / "images"
images_path.mkdir()

assert is_project_dir(tmp_path)

def it_returns_false_if_path_is_not_project_dir(tmp_path):
assert not is_project_dir(tmp_path)


def describe_is_unix_like_os():
Expand Down

0 comments on commit 10b9b3e

Please sign in to comment.