def test_color_equals(self): clr_hex = Color(0) self.assertEqual(0, clr_hex) self.assertEqual(Color(0), clr_hex) self.assertEqual(ColorFactory.BLACK, clr_hex) self.assertEqual("#000000", clr_hex) self.assertEqual("000000", clr_hex) self.assertNotEquals(Color(1), clr_hex)
def test_color(self): clr_hex = Color(0) self.assertEqual("#000000", clr_hex.color_hex) self.assertEqual(0, clr_hex.color_int) self.assertEqual(0, clr_hex.r) self.assertEqual(0, clr_hex.g) self.assertEqual(0, clr_hex.b) with self.assertRaises(ValueError): Color(16777216) with self.assertRaises(ValueError): Color(-1)
def setUpTestCase(self) -> None: self.default_dict = { "m": { "i2": 7 }, "i": 7, "ma": [{ "i2": 9 }, { "i2": 11 }], "d": { "a": 10 }, "f": 8.5, "b": True, "u": "https://google.com", "t": "ABCDE", "o": ObjectId("5ebc47c97041fb2e814b59bb"), "g": 100, "dt": datetime(2020, 5, 7).replace(tzinfo=timezone.utc), "c": Color(5000000), "a": [5, 11, 13], "ac": AutoReplyContentType.TEXT }
def _check_value_valid_not_none_(self, value): if isinstance(value, int): if Color.color_num_valid(value): return else: raise FieldValueInvalid(self.key, value) elif isinstance(value, str): try: ColorFactory.from_hex(value) except ValueError: raise FieldValueInvalid(self.key, value) elif isinstance(value, Color): pass else: raise FieldTypeMismatch(self.key, type(value), value, self.expected_types)
def get_value_validity_test(self) -> Tuple[Tuple[Any, bool], ...]: return ((None, True), (ColorFactory.DEFAULT, True), (5723991, True), ("#575757", True), ("575757", True), (Color(5723991), True), (True, False), (-8000, False), (20000000, False), ("GGGGGG", False))
def get_valid_value_to_set(self) -> Tuple[Tuple[Any, Any], ...]: return ((ColorFactory.DEFAULT, ColorFactory.DEFAULT), (5723991, Color(5723991)), ("#575757", Color(5723991)), ("575757", Color(5723991)), (Color(5723991), Color(5723991)))
def get_values_to_cast(self) -> Tuple[Tuple[Any, Any], ...]: return ((None, None), (ColorFactory.DEFAULT, ColorFactory.DEFAULT), (5723991, Color(5723991)), ("#575757", Color(5723991)), ("575757", Color(5723991)), (Color(5723991), Color(5723991)))
def _cast_to_desired_type_(self, value): # Data store in the database is int if isinstance(value, int): return Color(value) else: return ColorFactory.from_hex(value)
def test_color_num_valid(self): self.assertTrue(Color.color_num_valid(0)) self.assertTrue(Color.color_num_valid(16777215)) self.assertFalse(Color.color_num_valid(-1)) self.assertFalse(Color.color_num_valid(16777216))