def test_bad_value(self): """ Test with values that are not supported. """ key = TimestampKey("test") with self.assertRaisesRegex(TankError, "Invalid type"): key.str_from_value(1)
def test_string_default_value(self): """ Makes sure that a default value is proprely generated when a string default value is provided. """ # Create a template using our key. key = TimestampKey("datetime", default=self._datetime_string) # Convert to a string and compare the result. self.assertEqual(key.str_from_value(None), self._datetime_string)
def test_value_from_str(self): """ Makes sure that a string can be converted to a datetime. """ key = TimestampKey("test") self.assertEqual(key.value_from_str(self._datetime_string), self._datetime) self.assertEqual(key.value_from_str(unicode(self._datetime_string)), self._datetime)
def test_str_from_value(self): """ Convert all supported value types into a string and validates that we are getting the right result """ key = TimestampKey("test") # Try and convert each and every date format to string self.assertEqual(key.str_from_value(self._datetime), self._datetime_string)
def test_utc_now_default_value(self, _get_utc_time_mock): """ Makes sure that a default value is proprely generated when the utc_now default value is requested. """ # Mock it to the expected date. _get_utc_time_mock.return_value = self._datetime # Create the key key = TimestampKey("datetime", default="utc_now") # Convert to a string and compare the result. self.assertEqual(key.str_from_value(None), self._datetime_string)
def test_str_from_value(self): """ Convert all supported value types into a string and validates that we are getting the right result """ key = TimestampKey("test") # Try and convert each and every date format to string self.assertEqual( key.str_from_value(self._datetime), self._datetime_string )
def test_value_from_str(self): """ Makes sure that a string can be converted to a datetime. """ key = TimestampKey("test") self.assertEqual( key.value_from_str(self._datetime_string), self._datetime ) self.assertEqual( key.value_from_str(unicode(self._datetime_string)), self._datetime )
def setUp(self): super(TestTemplate, self).setUp() # Make various types of keys(fields) self.keys = { "Sequence": StringKey("Sequence"), "Shot": StringKey("Shot", default="s1", choices=["s1", "s2", "shot_1"]), "Step": StringKey("Step"), "branch": StringKey("branch", filter_by="alphanumeric"), "name": StringKey("name"), "version": IntegerKey("version", format_spec="03"), "snapshot": IntegerKey("snapshot", format_spec="03"), "ext": StringKey("ext"), "seq_num": SequenceKey("seq_num"), "frame": SequenceKey("frame", format_spec="04"), "day_month_year": TimestampKey("day_month_year", format_spec="%d_%m_%Y") } # Make a template self.definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}.{branch}.v{version}.{snapshot}.{day_month_year}.ma" self.template = Template(self.definition, self.keys)
def test_default_values(self): """ Makes sure default values are as expected. """ key = TimestampKey("name") self.assertEqual(key.format_spec, "%Y-%m-%d-%H-%M-%S") self.assertIsNone(key.default)
def test_bad_str(self): """ Test with strings that don't match the specified format. """ key = TimestampKey("test") # bad format with self.assertRaisesRegex(TankError, "Invalid string"): key.value_from_str("1 2 3") # out of bound values with self.assertRaisesRegex(TankError, "Invalid string"): key.value_from_str("2015-06-33-21-20-30") # Too much data with self.assertRaisesRegex(TankError, "Invalid string"): key.value_from_str(self._datetime_string + "bad date")
def test_init(self): """ Tests the __init__ of TimestampKey. """ # No args should be time, it's just a timestamp with default formatting options. TimestampKey("name") # While unlikely, hardcoding a timestamp matching the format spec should be fine. TimestampKey("name", default="2015-07-03-09-09-00") # Hardcoding a default value with a custom format spec should be fine. TimestampKey("name", default="03-07-2015", format_spec="%d-%m-%Y") # utc and now are special cases that end up returning the current time as the default # value. key = TimestampKey("name", default="utc_now") # Make sure UTC time will be generated. self.assertEqual(key._default, key._TimestampKey__get_current_utc_time) key = TimestampKey("name", default="now") # Make sure localtime will be generated. self.assertEqual(key._default, key._TimestampKey__get_current_time) # One can override the format_spec without providing a default. TimestampKey("name", format_spec="%Y-%m-%d") # format_spec has to be a string. with self.assertRaisesRegex(TankError, "is not of type string or None"): TimestampKey("name", default=1) # format_spec has to be a string. with self.assertRaisesRegex(TankError, "is not of type string"): TimestampKey("name", format_spec=1) # Date that to be a valid time. with self.assertRaisesRegex(TankError, "Invalid string"): TimestampKey("name", default="00-07-2015", format_spec="%d-%m-%Y") # Date that to be a valid time. with self.assertRaisesRegex(TankError, "Invalid string"): TimestampKey("name", default="not_a_date")