Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gireg.roussel committed Dec 24, 2024
1 parent a79f199 commit f1679a0
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 10 deletions.
18 changes: 13 additions & 5 deletions edge_orchestrator/tests/fixtures/binaries.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import os

from _pytest.fixtures import fixture

from tests.conftest import TEST_DATA_FOLDER_PATH


@fixture(scope="function")
def my_binaries_0():
with (TEST_DATA_FOLDER_PATH / "test_config" / "item_0" / "camera_id1.jpg").open("br") as f1, (
TEST_DATA_FOLDER_PATH / "test_config" / "item_0" / "camera_id2.jpg"
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name
with (TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_0" / "camera_id1.jpg").open("br") as f1, (
TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_0" / "camera_id2.jpg"
).open("br") as f2:
picture_1 = f1.read()
picture_2 = f2.read()
Expand All @@ -15,7 +19,9 @@ def my_binaries_0():

@fixture(scope="function")
def my_binaries_1():
with (TEST_DATA_FOLDER_PATH / "test_config" / "item_1" / "camera_id1.jpg").open("br") as f:
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name
with (TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_1" / "camera_id1.jpg").open("br") as f:
picture = f.read()
return {
"camera_id1": picture,
Expand All @@ -27,8 +33,10 @@ def my_binaries_1():

@fixture(scope="function")
def my_binaries_2():
with (TEST_DATA_FOLDER_PATH / "test_config" / "item_2" / "camera_id1.jpg").open("br") as f1, (
TEST_DATA_FOLDER_PATH / "test_config" / "item_2" / "camera_id2.jpg"
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name
with (TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_2" / "camera_id1.jpg").open("br") as f1, (
TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_2" / "camera_id2.jpg"
).open("br") as f2:
picture_2 = f1.read()
picture_3 = f2.read()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os

from fastapi.testclient import TestClient

Expand All @@ -10,9 +11,12 @@
class TestServer:
def test_upload_route__should_return_expected_logs_when_received_paylod_with_binary_image(self, caplog):
# Given
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name

client = TestClient(server())
test_file = "camera_id1.jpg"
test_file_path = TEST_DATA_FOLDER_PATH / "test_config" / "item_2" / test_file
test_file_path = TEST_DATA_FOLDER_PATH / edge_name / "test_config" / "item_2" / test_file
expected_logs = [
"Starting Save Binaries",
"Entering try Save Binaries",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest.mock import Mock, patch

from edge_orchestrator.domain.models.item import Item
Expand All @@ -11,9 +12,11 @@ class TestGCPBinaryStorage:
@patch("edge_orchestrator.infrastructure.binary_storage.gcp_binary_storage.storage")
def test_save_item_binaries_should_write_image_in_gcp(self, mock_storage):
# Given
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name
test_active_config_name = "test_config"
test_camera_id = "1"
test_file_path = TEST_DATA_FOLDER_PATH / test_active_config_name / "item_2" / "camera_id1.jpg"
test_file_path = TEST_DATA_FOLDER_PATH / edge_name / test_active_config_name / "item_2" / "camera_id1.jpg"
item = Item.from_nothing()
with open(test_file_path, "rb") as f:
item.binaries = {test_camera_id: f}
Expand All @@ -27,15 +30,19 @@ def test_save_item_binaries_should_write_image_in_gcp(self, mock_storage):

# Then
mock_storage.Client.assert_called_once()
mock_bucket.blob.assert_called_once_with(f"{test_active_config_name}/{item.id}/{test_camera_id}.jpg")
mock_bucket.blob.assert_called_once_with(
f"{edge_name}/{test_active_config_name}/{item.id}/{test_camera_id}.jpg"
)
mock_bucket.blob.return_value.upload_from_string.assert_called_once_with(f, content_type="image/jpg")

@patch("edge_orchestrator.infrastructure.binary_storage.gcp_binary_storage.storage")
def test_get_item_binary_should_return_image(self, mock_storage):
# Given
edge_name = "edge_test"
os.environ["EDGE_NAME"] = edge_name
test_active_config_name = "test_config"
test_camera_id = "1"
test_file_path = TEST_DATA_FOLDER_PATH / test_active_config_name / "item_2" / "camera_id1.jpg"
test_file_path = TEST_DATA_FOLDER_PATH / edge_name / test_active_config_name / "item_2" / "camera_id1.jpg"
item = Item.from_nothing()
with open(test_file_path, "rb") as f:
item.binaries = {test_camera_id: f}
Expand All @@ -50,4 +57,6 @@ def test_get_item_binary_should_return_image(self, mock_storage):

# Then
mock_storage.Client.assert_called_once()
mock_bucket.get_blob.assert_called_once_with(f"{test_active_config_name}/{item.id}/{test_camera_id}.jpg")
mock_bucket.get_blob.assert_called_once_with(
f"{edge_name}/{test_active_config_name}/{item.id}/{test_camera_id}.jpg"
)

0 comments on commit f1679a0

Please sign in to comment.