Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
bb96fca
1
Parent(s):
a865ba6
test: mock of FileUploader and tiny test validating it
Browse files
tests/test_input_observation.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Protocol, runtime_checkable
|
| 2 |
+
import pytest
|
| 3 |
+
from unittest.mock import MagicMock, patch
|
| 4 |
+
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
#from PIL import Image
|
| 7 |
+
import datetime
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
#from streamlit.runtime.uploaded_file_manager import UploadedFile # for type hinting
|
| 11 |
+
#from typing import List, Union
|
| 12 |
+
|
| 13 |
+
from input.input_observation import InputObservation
|
| 14 |
+
|
| 15 |
+
@runtime_checkable
|
| 16 |
+
class UploadedFile(Protocol):
|
| 17 |
+
name: str
|
| 18 |
+
size: int
|
| 19 |
+
type: str
|
| 20 |
+
_file_urls: list
|
| 21 |
+
|
| 22 |
+
def getvalue(self) -> bytes: ...
|
| 23 |
+
def read(self) -> bytes: ...
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class MockUploadedFile(BytesIO):
|
| 27 |
+
def __init__(self,
|
| 28 |
+
initial_bytes: bytes,
|
| 29 |
+
*, # enforce keyword-only arguments after now
|
| 30 |
+
name:str,
|
| 31 |
+
size:int,
|
| 32 |
+
type:str):
|
| 33 |
+
#super().__init__(*args, **kwargs)
|
| 34 |
+
super().__init__(initial_bytes)
|
| 35 |
+
self.name = name
|
| 36 |
+
self.size = size
|
| 37 |
+
self.type = type
|
| 38 |
+
|
| 39 |
+
self._file_urls = [None,]
|
| 40 |
+
|
| 41 |
+
@pytest.fixture
|
| 42 |
+
def mock_uploadedFile():
|
| 43 |
+
class MockGUIClass(MagicMock):
|
| 44 |
+
def __init__(self, *args, **kwargs):
|
| 45 |
+
super().__init__(*args, **kwargs)
|
| 46 |
+
name = kwargs.get('name', 'image2.jpg')
|
| 47 |
+
size = kwargs.get('size', 123456)
|
| 48 |
+
type = kwargs.get('type', 'image/jpeg')
|
| 49 |
+
self.bytes_io = MockUploadedFile(
|
| 50 |
+
b"test data", name=name, size=size, type=type)
|
| 51 |
+
self.get_data = MagicMock(return_value=self.bytes_io)
|
| 52 |
+
return MockGUIClass
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# let's first generate a test for the mock_uploaded_file and MockUploadedFile class
|
| 56 |
+
# - test with valid input
|
| 57 |
+
def test_mock_uploaded_file(mock_uploadedFile):
|
| 58 |
+
# setup values for the test (all valid)
|
| 59 |
+
image_name = "test_image.jpg"
|
| 60 |
+
mock_file = mock_uploadedFile(name=image_name).get_data()
|
| 61 |
+
|
| 62 |
+
#print(dir(mock_file))
|
| 63 |
+
assert isinstance(mock_file, BytesIO)
|
| 64 |
+
|
| 65 |
+
assert mock_file.name == image_name
|
| 66 |
+
assert mock_file.size == 123456
|
| 67 |
+
assert mock_file.type == "image/jpeg"
|