Skip to content

Commit

Permalink
add feature init caregory in add new video
Browse files Browse the repository at this point in the history
  • Loading branch information
joseevilasio committed Sep 20, 2023
1 parent 44dbcbb commit a67600c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Makefile
.PHONY: install update env set-app ipython clean test pflake8 fmt lint watch docs docs-serve build publish code-coverage
.PHONY: install install-without-dev update env set-app ipython clean test pflake8 fmt lint watch docs docs-serve build publish code-coverage

install:
@poetry install

install-without-dev:
@poetry install without dev

update:
@poetry update

Expand Down
21 changes: 20 additions & 1 deletion api/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
# CONTROLLER VIDEOS


def init_category():
"""Create free category if it doesn't exist"""

query = mongo.db.category.find_one({"id": 1}, projection={"_id": False})

if query is None:
id = get_next_sequence_value("category")
mongo.db.category.insert_one(
{
"id": id, # insert auto increment
"title": "livre",
"color": "branco",
}
)


def get_all_videos() -> dict:
"""Get all videos from database and list information"""
query = mongo.db.videos.find(projection={"_id": False})
Expand Down Expand Up @@ -37,6 +53,9 @@ def get_video_by_id(video_id: int) -> dict:

def add_new_video(data: dict) -> int:
"""Add new video on database"""

init_category()

id = get_next_sequence_value("videos")

if (
Expand All @@ -56,7 +75,7 @@ def add_new_video(data: dict) -> int:
raise ValueError("Url is invalid")

if data.get("categoryId") is None:
data["categoryId"] = None
data["categoryId"] = 1

mongo.db.videos.insert_one(
{
Expand Down
12 changes: 11 additions & 1 deletion tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
get_all_videos_by_category,
get_category_by_id,
get_video_by_id,
init_category,
search_video,
update_category,
update_video,
Expand All @@ -24,6 +25,15 @@
)


@pytest.mark.unit
def test_init_category():
"""Test create free category if it doesn't exist"""

init_category()

assert get_category_by_id(1)["title"] == "livre"


@pytest.mark.unit
def test_get_all_videos_positive():
"""Test get all videos from database and list information"""
Expand Down Expand Up @@ -171,7 +181,7 @@ def test_add_new_video_positve_without_category():
}

assert add_new_video(data) == 1
assert get_video_by_id(1).get("categoryId") is None
assert get_video_by_id(1).get("categoryId") is 1


@pytest.mark.unit
Expand Down

0 comments on commit a67600c

Please sign in to comment.