new unit tests
This commit is contained in:
@@ -129,7 +129,7 @@ def load_text(current_text: int, raw_text: dict, text: Optional[Text] = None) ->
|
|||||||
if "position" in raw_text:
|
if "position" in raw_text:
|
||||||
if raw_text["position"] not in [p.name for p in Pos]:
|
if raw_text["position"] not in [p.name for p in Pos]:
|
||||||
raise TypeError(f"'position' is not a valid position (ex: NW, E, SE, ...)")
|
raise TypeError(f"'position' is not a valid position (ex: NW, E, SE, ...)")
|
||||||
text.position = [p for p in Pos if p.name == raw_text["position"]][0]
|
text.position = getattr(Pos, raw_text["position"])
|
||||||
if "align" in raw_text:
|
if "align" in raw_text:
|
||||||
if raw_text["align"] not in ["left", "center", "right"]:
|
if raw_text["align"] not in ["left", "center", "right"]:
|
||||||
raise TypeError(f"'align' is not 'left', 'center' or 'right'")
|
raise TypeError(f"'align' is not 'left', 'center' or 'right'")
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
from meme_otron import meme_db
|
||||||
|
from meme_otron.types import Pos, Text
|
||||||
|
from random import randint, randrange, choice
|
||||||
|
|
||||||
|
|
||||||
|
def get_rand_raw_text():
|
||||||
|
return {
|
||||||
|
"font": str(randint(0, pow(10, 10))),
|
||||||
|
"x_range": [randrange(-10, 10), randrange(-10, 10)],
|
||||||
|
"y_range": [randrange(-10, 10), randrange(-10, 10)],
|
||||||
|
"text_ref": randint(-10, 10),
|
||||||
|
"style_ref": randint(-10, 10),
|
||||||
|
"angle": randrange(-10, 10),
|
||||||
|
"font_size": randrange(-10, 10),
|
||||||
|
"fill": [randint(-10, 10), randint(-10, 10), randint(-10, 10)],
|
||||||
|
"stroke_width": randrange(-10, 10),
|
||||||
|
"stroke_fill": [randint(-10, 10), randint(-10, 10), randint(-10, 10)],
|
||||||
|
"align": choice(["left", "center", "right"]),
|
||||||
|
"position": choice([k.name for k in Pos])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TestMemeDbLoadText(TestCase):
|
||||||
|
object_keys = ["text_ref", "style_ref", "angle", "font_size", "fill",
|
||||||
|
"stroke_width", "stroke_fill", "position", "align"]
|
||||||
|
|
||||||
|
def test_load_text_minimal(self):
|
||||||
|
try:
|
||||||
|
text = meme_db.load_text(0, {})
|
||||||
|
self.assertEqual(f"text 0", text.text)
|
||||||
|
for key in self.object_keys:
|
||||||
|
self.assertIsNone(getattr(text, key))
|
||||||
|
except TypeError as e:
|
||||||
|
self.fail(e)
|
||||||
|
|
||||||
|
def test_load_text_normal(self):
|
||||||
|
try:
|
||||||
|
raw_text = get_rand_raw_text()
|
||||||
|
i = randint(-10, 10)
|
||||||
|
text = meme_db.load_text(i, raw_text)
|
||||||
|
self.assertEqual(f"text {i}", text.text)
|
||||||
|
for key in self.object_keys:
|
||||||
|
if key == "position":
|
||||||
|
self.assertEqual(getattr(Pos, raw_text[key]), text.position)
|
||||||
|
else:
|
||||||
|
self.assertEqual(raw_text[key], getattr(text, key))
|
||||||
|
except TypeError as e:
|
||||||
|
self.fail(e)
|
||||||
|
|
||||||
|
def test_load_text_base(self):
|
||||||
|
try:
|
||||||
|
base_text = meme_db.load_text(0, get_rand_raw_text())
|
||||||
|
raw_text = {
|
||||||
|
"font": str(randint(0, pow(10, 10)))
|
||||||
|
}
|
||||||
|
text = meme_db.load_text(0, raw_text, base_text)
|
||||||
|
self.assertEqual(f"text 0", text.text)
|
||||||
|
for key in self.object_keys:
|
||||||
|
if key in ["font_size", "fill", "stroke_width", "stroke_fill", "position", "align"]:
|
||||||
|
self.assertEqual(getattr(base_text, key), getattr(text, key))
|
||||||
|
elif key == "font":
|
||||||
|
self.assertEqual(raw_text["font"], getattr(text, key))
|
||||||
|
else:
|
||||||
|
self.assertIsNone(getattr(text, key))
|
||||||
|
except TypeError as e:
|
||||||
|
self.fail(e)
|
||||||
Reference in New Issue
Block a user