2019-12-08 22:43:49 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
2021-05-31 20:09:40 +02:00
|
|
|
import os
|
2021-05-02 05:50:19 +02:00
|
|
|
import sys
|
2021-05-02 05:48:07 +02:00
|
|
|
import unittest
|
2021-08-19 21:20:13 +02:00
|
|
|
from typing import Iterable, List, Dict, Any
|
2021-05-02 05:48:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Supress custom handler tracebacks inside handler frames
|
|
|
|
__unittest = True
|
|
|
|
|
|
|
|
|
|
|
|
class ExtendedTestCase(unittest.TestCase):
|
2021-05-02 05:50:19 +02:00
|
|
|
@property
|
|
|
|
def verbose(self) -> bool:
|
|
|
|
return ("-v" in sys.argv) or ("--verbose" in sys.argv)
|
|
|
|
|
2021-08-19 21:20:13 +02:00
|
|
|
def assertItemsEqual(self, a: Iterable[Any], b: Iterable[Any]) -> None:
|
2021-05-02 05:48:07 +02:00
|
|
|
a_items = {x for x in a}
|
|
|
|
b_items = {x for x in b}
|
|
|
|
self.assertEqual(a_items, b_items)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
class FakeCursor:
|
2019-12-08 22:43:49 +01:00
|
|
|
def __init__(self, rows: List[Dict[str, Any]]) -> None:
|
|
|
|
self.__rows = rows
|
|
|
|
self.rowcount = len(rows)
|
2023-03-19 06:23:35 +01:00
|
|
|
self.pos = -1
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def fetchone(self) -> Dict[str, Any]:
|
|
|
|
if len(self.__rows) != 1:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception(
|
|
|
|
f"Tried to fetch one row and there are {len(self.__rows)} rows!"
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
return self.__rows[0]
|
|
|
|
|
2023-03-19 06:23:35 +01:00
|
|
|
def __iter__(self) -> "FakeCursor":
|
|
|
|
self.pos = -1
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self) -> Dict[str, Any]:
|
|
|
|
self.pos += 1
|
|
|
|
if self.pos < self.rowcount:
|
|
|
|
return self.__rows[self.pos]
|
|
|
|
else:
|
|
|
|
raise StopIteration
|
2021-05-31 20:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_fixture(name: str) -> bytes:
|
|
|
|
location = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
with open(os.path.join(location, name), "rb") as fp:
|
|
|
|
return fp.read()
|