mmengine/tests/test_fileio/test_backends/test_memcached_backend.py
Zaida Zhou ed84dfd34d
[Refactor] Refactor fileio without breaking back compatibility (#533)
* [Refactor] Refactor fileio but without breaking bc

* handle compatibility

* fix format

* modify io functions

* fix ut

* fix ut

* rename method names

* refine

* refine docstring

* fix ut in windows

* update ut

* minor fix

* ensure client is not None when closing it

* add more examples for list_dir_or_file interface

* refine docstring

* refine deprecated info

* fix ut

* add a description for lmdb docstring
2022-09-26 14:30:40 +08:00

60 lines
1.8 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import sys
from pathlib import Path
from unittest import TestCase
from unittest.mock import MagicMock, patch
import cv2
import numpy as np
from parameterized import parameterized
from mmengine.fileio.backends import MemcachedBackend
def imfrombytes(content):
img_np = np.frombuffer(content, np.uint8)
img = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
return img
sys.modules['mc'] = MagicMock()
class MockMemcachedClient:
def __init__(self, server_list_cfg, client_cfg):
pass
def Get(self, filepath, buffer):
with open(filepath, 'rb') as f:
buffer.content = f.read()
class TestMemcachedBackend(TestCase):
@classmethod
def setUpClass(cls):
cls.mc_cfg = dict(server_list_cfg='', client_cfg='', sys_path=None)
cls.test_data_dir = Path(__file__).parent.parent.parent / 'data'
cls.img_path = cls.test_data_dir / 'color.jpg'
cls.img_shape = (300, 400, 3)
@parameterized.expand([[Path], [str]])
@patch('mc.MemcachedClient.GetInstance', MockMemcachedClient)
@patch('mc.pyvector', MagicMock)
@patch('mc.ConvertBuffer', lambda x: x.content)
def test_get(self, path_type):
backend = MemcachedBackend(**self.mc_cfg)
img_bytes = backend.get(path_type(self.img_path))
self.assertEqual(self.img_path.open('rb').read(), img_bytes)
img = imfrombytes(img_bytes)
self.assertEqual(img.shape, self.img_shape)
@patch('mc.MemcachedClient.GetInstance', MockMemcachedClient)
@patch('mc.pyvector', MagicMock)
@patch('mc.ConvertBuffer', lambda x: x.content)
def test_get_text(self):
backend = MemcachedBackend(**self.mc_cfg)
with self.assertRaises(NotImplementedError):
backend.get_text('filepath')