Ejemplo n.º 1
0
    def test_subset_format(self):
        """
        Test subset_format parameter
        """

        if sys.version_info < (2, 6):
            # subset format not supported in py25
            self.assertRaises(TankError, StringKey, "field_name", subset="(.{3}).*", subset_format="{0} FOO")
            return

        # test properties
        template_field = StringKey("field_name", subset="(.)().*", subset_format="{0} FOO")
        self.assertEqual("{0} FOO", template_field.subset_format)

        # cannot specify subset_format without subset
        self.assertRaises(TankError, StringKey, "field_name", subset_format="{0} FOO")

        tests = []

        # basic test
        tests.append( {
            "short": "\u3042foo ",
            "full": "foobar",
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="\u3042{0} ")
            }
        )

        # unicode
        tests.append( {
            "short": u'\u3042\u308a\u304c ',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="{0} ")
            }
        )

        # multi token
        tests.append( {
            "short": 'S J',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*', subset_format="{1} {0}")
            }
        )

        for test in tests:

            print(test)

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])
Ejemplo n.º 2
0
    def test_subset_format(self):
        """
        Test subset_format parameter
        """

        if sys.version_info < (2, 6):
            # subset format not supported in py25
            self.assertRaises(TankError, StringKey, "field_name", subset="(.{3}).*", subset_format="{0} FOO")
            return

        # test properties
        template_field = StringKey("field_name", subset="(.)().*", subset_format="{0} FOO")
        self.assertEquals("{0} FOO", template_field.subset_format)

        # cannot specify subset_format without subset
        self.assertRaises(TankError, StringKey, "field_name", subset_format="{0} FOO")

        tests = []

        # basic test
        tests.append( {
            "short": "\u3042foo ",
            "full": "foobar",
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="\u3042{0} ")
            }
        )

        # unicode
        tests.append( {
            "short": u'\u3042\u308a\u304c ',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="{0} ")
            }
        )

        # multi token
        tests.append( {
            "short": 'S J',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*', subset_format="{1} {0}")
            }
        )

        for test in tests:

            print test

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEquals(short, template_field.value_from_str(short))
            self.assertEquals(full, template_field.value_from_str(full))

            self.assertEquals(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])
Ejemplo n.º 3
0
    def test_subset(self):
        """
        Test subset_format parameter
        """

        # test properties
        template_field = StringKey("field_name", subset="(.{3}).*")
        self.assertEqual("(.{3}).*", template_field.subset)
        # test bad regex
        self.assertRaises(TankError, StringKey, "field_name", subset="({4}.).*BROKENREGEX")

        # test basic regex
        template_field = StringKey("field_name", subset="(.{3}).*")

        tests = []

        # basic test
        tests.append({"short": "foo", "full": "foobar", "template": StringKey("field_name", subset="(.{3}).*")})
        tests.append({"short": u"foo", "full": u"foobar", "template": StringKey("field_name", subset="(.{3}).*")})

        # unicode
        tests.append({
            "short": u'\u3042\u308a\u304c',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*")}
        )

        # multi token
        tests.append({
            "short": 'JS',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*')}
        )

        for test in tests:

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])
Ejemplo n.º 4
0
    def test_subset(self):
        """
        Test subset_format parameter
        """

        # test properties
        template_field = StringKey("field_name", subset="(.{3}).*")
        self.assertEquals("(.{3}).*", template_field.subset)
        # test bad regex
        self.assertRaises(TankError, StringKey, "field_name", subset="({4}.).*BROKENREGEX")

        # test basic regex
        template_field = StringKey("field_name", subset="(.{3}).*")

        tests = []

        # basic test
        tests.append({"short": "foo", "full": "foobar", "template": StringKey("field_name", subset="(.{3}).*")})
        tests.append({"short": u"foo", "full": u"foobar", "template": StringKey("field_name", subset="(.{3}).*")})

        # unicode
        tests.append({
            "short": u'\u3042\u308a\u304c',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*")}
        )

        # multi token
        tests.append({
            "short": 'JS',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*')}
        )

        for test in tests:

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEquals(short, template_field.value_from_str(short))
            self.assertEquals(full, template_field.value_from_str(full))

            self.assertEquals(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])
Ejemplo n.º 5
0
class TestStringKey(TankTestBase):
    def setUp(self):
        super(TestStringKey, self).setUp()
        self.str_field = StringKey("field_name")
        self.alpha_field = StringKey("field_name", filter_by="alphanumeric")
        self.choice_field = StringKey("field_name", choices=["a", "b"])
        self.default_field = StringKey("field_name", default="b")

    def test_default(self):
        default_value = "default_value"
        template_field = StringKey("field_name", default=default_value)
        self.assertEquals(default_value, template_field.default)

    def test_no_default(self):
        template_field = StringKey("field_name")
        self.assertIsNone(template_field.default)

    def test_choices(self):
        choices_value = ["a", "b"]
        template_field = StringKey("field_name", choices=choices_value)
        self.assertEquals(choices_value, template_field.choices)

    def test_exclusions(self):
        exclusions = ["a", "b"]
        template_field = StringKey("field_name", exclusions=exclusions)
        self.assertEquals(exclusions, template_field.exclusions)
        self.assertFalse(template_field.validate("a"))
        self.assertFalse(template_field.validate("b"))

    def test_illegal_choice_alphanumic(self):
        choices_value = ["@", "b"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices_value,
                          filter_by="alphanumeric")

    def test_default_choices_mismatch(self):
        """Case that default value is not part of enumerated choices."""
        default_value = "c"
        choices_value = ["a", "b"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices_value,
                          default=default_value)

    def test_choices_exclusions_conflict(self):
        """Case that same value is put as valid and invalid choice."""
        choices = ["a", "b"]
        exclusions = ["c", "a"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices,
                          exclusions=exclusions)

    def test_name_set(self):
        name = "field_name"
        template_field = StringKey("field_name")
        self.assertEquals(name, template_field.name)

    def test_validate_string_good(self):
        value = "some string"
        self.assertTrue(self.str_field.validate(value))

    def test_validate_alphanum_good(self):
        value = "some0alphanumeric0string"
        self.assertTrue(self.alpha_field.validate(value))

    def test_validate_alphanum_bad(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alpha_field.validate(bad_value))

    def test_str_from_value_good(self):
        value = "a string"
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_empty(self):
        value = ""
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_good_choice(self):
        value = "b"
        expected = value
        result = self.choice_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_bad_choice(self):
        value = "c"
        expected = "%s Illegal value: 'c' not in choices: ['a', 'b']" % str(
            self.choice_field)
        self.check_error_message(TankError, expected,
                                 self.choice_field.str_from_value, value)

    def test_str_from_value_use_default(self):
        expected = "b"
        result = self.default_field.str_from_value()
        self.assertEquals(expected, result)

    def test_str_from_value_no_default(self):
        expected = "No value provided and no default available for %s" % self.str_field
        self.check_error_message(TankError, expected,
                                 self.str_field.str_from_value)

    def test_str_from_value_alphanum_good(self):
        value = "a9b9C"
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_alphanum_empty(self):
        value = ""
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_alphanum_bad(self):
        base_expected = "%s Illegal value '%%s' does not fit filter" % self.alpha_field
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected,
                                     self.alpha_field.str_from_value,
                                     bad_value)

    def test_str_from_value_ignore_type_alphanum(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.alpha_field.str_from_value(bad_value,
                                                     ignore_type=True)
            self.assertEquals(expected, result)

    def test_value_from_str(self):
        str_value = "something"
        self.assertEquals(str_value, self.str_field.value_from_str(str_value))

    def test_shotgun_entity_type_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot")
        self.assertEquals("Shot", str_field.shotgun_entity_type)

    def test_shotgun_field_name_without_entity_type(self):
        """
        Test that setting shotgun_field name is not possible if not setting shotgun_entity_type.
        """
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          shotgun_field_name="code")

    def test_shotgun_field_name_set(self):
        str_field = StringKey("field_name",
                              shotgun_entity_type="Shot",
                              shotgun_field_name="code")
        self.assertEquals("Shot", str_field.shotgun_entity_type)
        self.assertEquals("code", str_field.shotgun_field_name)

    def test_repr(self):
        expected = "<Sgtk StringKey field_name>"
        self.assertEquals(expected, str(self.str_field))
Ejemplo n.º 6
0
class TestStringKey(ShotgunTestBase):
    def setUp(self):
        super(TestStringKey, self).setUp()
        self.str_field = StringKey("field_name")
        self.alphanum_field = StringKey("field_name", filter_by="alphanumeric")
        self.alpha_field = StringKey("field_name", filter_by="alpha")

        self.regex_field = StringKey(
            "field_name", filter_by="^[0-9]{3}@[a-z]+")  # e.g 123@foo

        self.choice_field = StringKey("field_name", choices=["a", "b"])
        self.default_field = StringKey("field_name", default="b")

    def test_invalid(self):
        self.assertRaises(TankError, StringKey, "S!hot")

    def test_default(self):
        default_value = "default_value"
        template_field = StringKey("field_name", default=default_value)
        self.assertEqual(default_value, template_field.default)

    def test_no_default(self):
        template_field = StringKey("field_name")
        self.assertIsNone(template_field.default)

    def test_choices(self):
        choices_value = ["a", "b"]
        template_field = StringKey("field_name", choices=choices_value)
        self.assertEqual(choices_value, template_field.choices)

    def test_exclusions(self):
        exclusions = ["a", "b"]
        template_field = StringKey("field_name", exclusions=exclusions)
        self.assertEqual(exclusions, template_field.exclusions)
        self.assertFalse(template_field.validate("a"))
        self.assertFalse(template_field.validate("b"))

    def test_illegal_choice_alphanumic(self):
        choices_value = ["@", "b"]
        self.assertRaises(
            TankError,
            StringKey,
            "field_name",
            choices=choices_value,
            filter_by="alphanumeric",
        )

    def test_default_choices_mismatch(self):
        """Case that default value is not part of enumerated choices."""
        default_value = "c"
        choices_value = ["a", "b"]
        self.assertRaises(
            TankError,
            StringKey,
            "field_name",
            choices=choices_value,
            default=default_value,
        )

    def test_choices_exclusions_conflict(self):
        """Case that same value is put as valid and invalid choice."""
        choices = ["a", "b"]
        exclusions = ["c", "a"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices,
                          exclusions=exclusions)

    def test_name_set(self):
        name = "field_name"
        template_field = StringKey("field_name")
        self.assertEqual(name, template_field.name)

    def test_validate_string_good(self):
        value = "some string"
        self.assertTrue(self.str_field.validate(value))

    def test_validate_alphanum_good(self):
        value = "some0alphanumeric0string"
        self.assertTrue(self.alphanum_field.validate(value))

    def test_validate_alphanum_bad(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alphanum_field.validate(bad_value))

    def test_validate_regex_good(self):
        value = "123@foobar"
        self.assertTrue(self.regex_field.validate(value))

    def test_validate_regex_bad(self):
        bad_values = ["basd", "1234@asd", " 123@foo", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.regex_field.validate(bad_value))

    def test_validate_alpha_good(self):
        value = "somealphastring"
        self.assertTrue(self.alpha_field.validate(value))

    def test_validate_alpha_bad(self):
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alpha_field.validate(bad_value))

    def test_str_from_value_good(self):
        value = "a string"
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_empty(self):
        value = ""
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_good_choice(self):
        value = "b"
        expected = value
        result = self.choice_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_bad_choice(self):
        value = "c"
        expected = "%s Illegal value: 'c' not in choices: ['a', 'b']" % str(
            self.choice_field)
        self.check_error_message(TankError, expected,
                                 self.choice_field.str_from_value, value)

    def test_str_from_value_use_default(self):
        expected = "b"
        result = self.default_field.str_from_value()
        self.assertEqual(expected, result)

    def test_str_from_value_no_default(self):
        expected = "No value provided and no default available for %s" % self.str_field
        self.check_error_message(TankError, expected,
                                 self.str_field.str_from_value)

    def test_str_from_value_alphanum_good(self):
        value = "a9b9C"
        expected = value
        result = self.alphanum_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alphanum_empty(self):
        value = ""
        expected = value
        result = self.alphanum_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alphanum_bad(self):
        base_expected = (
            "%s Illegal value '%%s' does not fit filter_by 'alphanumeric'" %
            self.alphanum_field)
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected,
                                     self.alphanum_field.str_from_value,
                                     bad_value)

    def test_str_from_value_ignore_type_alphanum(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.alphanum_field.str_from_value(bad_value,
                                                        ignore_type=True)
            self.assertEqual(expected, result)

    def test_str_from_value_alpha_good(self):
        value = "abC"
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alpha_empty(self):
        value = ""
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alpha_bad(self):
        base_expected = (
            "%s Illegal value '%%s' does not fit filter_by 'alpha'" %
            self.alpha_field)
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected,
                                     self.alpha_field.str_from_value,
                                     bad_value)

    def test_str_from_value_ignore_type_alpha(self):
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.regex_field.str_from_value(bad_value,
                                                     ignore_type=True)
            self.assertEqual(expected, result)

    def test_str_from_value_regex_good(self):
        value = "444@jlasdlkjasd"
        expected = value
        result = self.regex_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_regex_bad(self):
        base_expected = (
            "%s Illegal value '%%s' does not fit filter_by '^[0-9]{3}@[a-z]+'"
            % self.regex_field)
        bad_values = ["", " 121@fff", "asdasd", "123@", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected,
                                     self.regex_field.str_from_value,
                                     bad_value)

    def test_str_from_value_ignore_type_regex(self):
        bad_values = ["", " 121@fff", "asdasd", "123@", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.regex_field.str_from_value(bad_value,
                                                     ignore_type=True)
            self.assertEqual(expected, result)

    def test_value_from_str(self):
        str_value = "something"
        self.assertEqual(str_value, self.str_field.value_from_str(str_value))

    def test_shotgun_entity_type_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot")
        self.assertEqual("Shot", str_field.shotgun_entity_type)

    def test_shotgun_field_name_without_entity_type(self):
        """
        Test that setting shotgun_field name is not possible if not setting shotgun_entity_type.
        """
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          shotgun_field_name="code")

    def test_shotgun_field_name_set(self):
        str_field = StringKey("field_name",
                              shotgun_entity_type="Shot",
                              shotgun_field_name="code")
        self.assertEqual("Shot", str_field.shotgun_entity_type)
        self.assertEqual("code", str_field.shotgun_field_name)

    def test_repr(self):
        expected = "<Sgtk StringKey field_name>"
        self.assertEqual(expected, str(self.str_field))

    def test_subset(self):
        """
        Test subset_format parameter
        """

        # test properties
        template_field = StringKey("field_name", subset="(.{3}).*")
        self.assertEqual("(.{3}).*", template_field.subset)
        # test bad regex
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          subset="({4}.).*BROKENREGEX")

        # test basic regex
        template_field = StringKey("field_name", subset="(.{3}).*")

        tests = []

        # basic test
        tests.append({
            "short": "foo",
            "full": "foobar",
            "template": StringKey("field_name", subset="(.{3}).*"),
        })
        tests.append({
            "short": u"foo",
            "full": u"foobar",
            "template": StringKey("field_name", subset="(.{3}).*"),
        })

        # unicode
        tests.append({
            "short": u"\u3042\u308a\u304c",
            "full": u"\u3042\u308a\u304c\u3068",
            "template": StringKey("field_name", subset="(.{3}).*"),
        })

        # multi token
        tests.append({
            "short":
            "JS",
            "full":
            "John Smith",
            "template":
            StringKey("field_name", subset="([A-Z])[a-z]* ([A-Z])[a-z]*"),
        })

        for test in tests:

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value,
                              short[0])

    def test_subset_format(self):
        """
        Test subset_format parameter
        """

        if sys.version_info < (2, 6):
            # subset format not supported in py25
            self.assertRaises(
                TankError,
                StringKey,
                "field_name",
                subset="(.{3}).*",
                subset_format="{0} FOO",
            )
            return

        # test properties
        template_field = StringKey("field_name",
                                   subset="(.)().*",
                                   subset_format="{0} FOO")
        self.assertEqual("{0} FOO", template_field.subset_format)

        # cannot specify subset_format without subset
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          subset_format="{0} FOO")

        tests = []

        # basic test
        tests.append({
            "short":
            "\u3042foo ",
            "full":
            "foobar",
            "template":
            StringKey("field_name",
                      subset="(.{3}).*",
                      subset_format="\u3042{0} "),
        })

        # unicode
        tests.append({
            "short":
            u"\u3042\u308a\u304c ",
            "full":
            u"\u3042\u308a\u304c\u3068",
            "template":
            StringKey("field_name", subset="(.{3}).*", subset_format="{0} "),
        })

        # multi token
        tests.append({
            "short":
            "S J",
            "full":
            "John Smith",
            "template":
            StringKey(
                "field_name",
                subset="([A-Z])[a-z]* ([A-Z])[a-z]*",
                subset_format="{1} {0}",
            ),
        })

        for test in tests:

            print(test)

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value,
                              short[0])
Ejemplo n.º 7
0
class TestStringKey(TankTestBase):
    def setUp(self):
        super(TestStringKey, self).setUp()
        self.str_field = StringKey("field_name")
        self.alpha_field = StringKey("field_name", filter_by="alphanumeric")
        self.choice_field = StringKey("field_name", choices=["a", "b"])
        self.default_field = StringKey("field_name", default="b")

    def test_default(self):
        default_value = "default_value"
        template_field = StringKey("field_name", default=default_value)
        self.assertEquals(default_value, template_field.default)

    def test_no_default(self):
        template_field = StringKey("field_name")
        self.assertIsNone(template_field.default)

    def test_choices(self):
        choices_value = ["a", "b"]
        template_field = StringKey("field_name", choices=choices_value)
        self.assertEquals(choices_value, template_field.choices)

    def test_exclusions(self):
        exclusions = ["a", "b"]
        template_field = StringKey("field_name", exclusions=exclusions)
        self.assertEquals(exclusions, template_field.exclusions)
        self.assertFalse(template_field.validate("a"))
        self.assertFalse(template_field.validate("b"))

    def test_illegal_choice_alphanumic(self):
        choices_value = ["@", "b"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices_value,
                          filter_by="alphanumeric")

    def test_default_choices_mismatch(self):
        """Case that default value is not part of enumerated choices."""
        default_value = "c"
        choices_value = ["a", "b"]
        self.assertRaises(TankError, StringKey, "field_name", choices=choices_value, default=default_value)

    def test_choices_exclusions_conflict(self):
        """Case that same value is put as valid and invalid choice."""
        choices = ["a", "b"]
        exclusions = ["c", "a"]
        self.assertRaises(TankError, StringKey, "field_name", choices=choices, exclusions=exclusions)

    def test_name_set(self):
        name = "field_name"
        template_field = StringKey("field_name")
        self.assertEquals(name, template_field.name)

    def test_validate_string_good(self):
        value = "some string"
        self.assertTrue(self.str_field.validate(value))

    def test_validate_alphanum_good(self):
        value = "some0alphanumeric0string"
        self.assertTrue(self.alpha_field.validate(value))

    def test_validate_alphanum_bad(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alpha_field.validate(bad_value))

    def test_str_from_value_good(self):
        value = "a string"
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_empty(self):
        value = ""
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_good_choice(self):
        value = "b"
        expected = value
        result = self.choice_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_bad_choice(self):
        value = "c"
        expected = "%s Illegal value: 'c' not in choices: ['a', 'b']" % str(self.choice_field)
        self.check_error_message(TankError, expected, self.choice_field.str_from_value, value)

    def test_str_from_value_use_default(self):
        expected = "b"
        result = self.default_field.str_from_value()
        self.assertEquals(expected, result)

    def test_str_from_value_no_default(self):
        expected = "No value provided and no default available for %s" % self.str_field
        self.check_error_message(TankError, expected, self.str_field.str_from_value)

    def test_str_from_value_alphanum_good(self):
        value = "a9b9C"
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_alphanum_empty(self):
        value = ""
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEquals(expected, result)

    def test_str_from_value_alphanum_bad(self):
        base_expected = "%s Illegal value '%%s' does not fit filter" % self.alpha_field
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected, self.alpha_field.str_from_value, bad_value)

    def test_str_from_value_ignore_type_alphanum(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.alpha_field.str_from_value(bad_value, ignore_type=True)
            self.assertEquals(expected, result)

    def test_value_from_str(self):
        str_value = "something"
        self.assertEquals(str_value, self.str_field.value_from_str(str_value))

    def test_shotgun_entity_type_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot")
        self.assertEquals("Shot", str_field.shotgun_entity_type)

    def test_shotgun_field_name_without_entity_type(self):
        """
        Test that setting shotgun_field name is not possible if not setting shotgun_entity_type.
        """
        self.assertRaises(TankError, StringKey, "field_name", shotgun_field_name="code")

    def test_shotgun_field_name_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot", shotgun_field_name="code")
        self.assertEquals("Shot", str_field.shotgun_entity_type)
        self.assertEquals("code", str_field.shotgun_field_name)


    def test_repr(self):
        expected = "<Sgtk StringKey field_name>"
        self.assertEquals(expected, str(self.str_field))
Ejemplo n.º 8
0
class TestStringKey(ShotgunTestBase):
    def setUp(self):
        super(TestStringKey, self).setUp()
        self.str_field = StringKey("field_name")
        self.alphanum_field = StringKey("field_name", filter_by="alphanumeric")
        self.alpha_field = StringKey("field_name", filter_by="alpha")

        self.regex_field = StringKey("field_name", filter_by="^[0-9]{3}@[a-z]+") # e.g 123@foo

        self.choice_field = StringKey("field_name", choices=["a", "b"])
        self.default_field = StringKey("field_name", default="b")

    def test_invalid(self):
        self.assertRaises(TankError, StringKey, "S!hot")

    def test_default(self):
        default_value = "default_value"
        template_field = StringKey("field_name", default=default_value)
        self.assertEqual(default_value, template_field.default)

    def test_no_default(self):
        template_field = StringKey("field_name")
        self.assertIsNone(template_field.default)

    def test_choices(self):
        choices_value = ["a", "b"]
        template_field = StringKey("field_name", choices=choices_value)
        self.assertEqual(choices_value, template_field.choices)

    def test_exclusions(self):
        exclusions = ["a", "b"]
        template_field = StringKey("field_name", exclusions=exclusions)
        self.assertEqual(exclusions, template_field.exclusions)
        self.assertFalse(template_field.validate("a"))
        self.assertFalse(template_field.validate("b"))

    def test_illegal_choice_alphanumic(self):
        choices_value = ["@", "b"]
        self.assertRaises(TankError,
                          StringKey,
                          "field_name",
                          choices=choices_value,
                          filter_by="alphanumeric")

    def test_default_choices_mismatch(self):
        """Case that default value is not part of enumerated choices."""
        default_value = "c"
        choices_value = ["a", "b"]
        self.assertRaises(TankError, StringKey, "field_name", choices=choices_value, default=default_value)

    def test_choices_exclusions_conflict(self):
        """Case that same value is put as valid and invalid choice."""
        choices = ["a", "b"]
        exclusions = ["c", "a"]
        self.assertRaises(TankError, StringKey, "field_name", choices=choices, exclusions=exclusions)

    def test_name_set(self):
        name = "field_name"
        template_field = StringKey("field_name")
        self.assertEqual(name, template_field.name)

    def test_validate_string_good(self):
        value = "some string"
        self.assertTrue(self.str_field.validate(value))

    def test_validate_alphanum_good(self):
        value = "some0alphanumeric0string"
        self.assertTrue(self.alphanum_field.validate(value))

    def test_validate_alphanum_bad(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alphanum_field.validate(bad_value))

    def test_validate_regex_good(self):
        value = "123@foobar"
        self.assertTrue(self.regex_field.validate(value))

    def test_validate_regex_bad(self):
        bad_values = ["basd", "1234@asd", " 123@foo", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.regex_field.validate(bad_value))

    def test_validate_alpha_good(self):
        value = "somealphastring"
        self.assertTrue(self.alpha_field.validate(value))

    def test_validate_alpha_bad(self):
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            self.assertFalse(self.alpha_field.validate(bad_value))

    def test_str_from_value_good(self):
        value = "a string"
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_empty(self):
        value = ""
        expected = value
        result = self.str_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_good_choice(self):
        value = "b"
        expected = value
        result = self.choice_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_bad_choice(self):
        value = "c"
        expected = "%s Illegal value: 'c' not in choices: ['a', 'b']" % str(self.choice_field)
        self.check_error_message(TankError, expected, self.choice_field.str_from_value, value)

    def test_str_from_value_use_default(self):
        expected = "b"
        result = self.default_field.str_from_value()
        self.assertEqual(expected, result)

    def test_str_from_value_no_default(self):
        expected = "No value provided and no default available for %s" % self.str_field
        self.check_error_message(TankError, expected, self.str_field.str_from_value)

    def test_str_from_value_alphanum_good(self):
        value = "a9b9C"
        expected = value
        result = self.alphanum_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alphanum_empty(self):
        value = ""
        expected = value
        result = self.alphanum_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alphanum_bad(self):
        base_expected = "%s Illegal value '%%s' does not fit filter_by 'alphanumeric'" % self.alphanum_field
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected, self.alphanum_field.str_from_value, bad_value)

    def test_str_from_value_ignore_type_alphanum(self):
        bad_values = ["a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.alphanum_field.str_from_value(bad_value, ignore_type=True)
            self.assertEqual(expected, result)

    def test_str_from_value_alpha_good(self):
        value = "abC"
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alpha_empty(self):
        value = ""
        expected = value
        result = self.alpha_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_alpha_bad(self):
        base_expected = "%s Illegal value '%%s' does not fit filter_by 'alpha'" % self.alpha_field
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected, self.alpha_field.str_from_value, bad_value)

    def test_str_from_value_ignore_type_alpha(self):
        bad_values = ["a2b", "a_b", "a b", "a-b", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.regex_field.str_from_value(bad_value, ignore_type=True)
            self.assertEqual(expected, result)

    def test_str_from_value_regex_good(self):
        value = "444@jlasdlkjasd"
        expected = value
        result = self.regex_field.str_from_value(value)
        self.assertEqual(expected, result)

    def test_str_from_value_regex_bad(self):
        base_expected = "%s Illegal value '%%s' does not fit filter_by '^[0-9]{3}@[a-z]+'" % self.regex_field
        bad_values = ["", " 121@fff", "asdasd", "123@", "*"]
        for bad_value in bad_values:
            expected = base_expected % bad_value
            self.check_error_message(TankError, expected, self.regex_field.str_from_value, bad_value)

    def test_str_from_value_ignore_type_regex(self):
        bad_values = ["", " 121@fff", "asdasd", "123@", "*"]
        for bad_value in bad_values:
            expected = bad_value
            result = self.regex_field.str_from_value(bad_value, ignore_type=True)
            self.assertEqual(expected, result)

    def test_value_from_str(self):
        str_value = "something"
        self.assertEqual(str_value, self.str_field.value_from_str(str_value))

    def test_shotgun_entity_type_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot")
        self.assertEqual("Shot", str_field.shotgun_entity_type)

    def test_shotgun_field_name_without_entity_type(self):
        """
        Test that setting shotgun_field name is not possible if not setting shotgun_entity_type.
        """
        self.assertRaises(TankError, StringKey, "field_name", shotgun_field_name="code")

    def test_shotgun_field_name_set(self):
        str_field = StringKey("field_name", shotgun_entity_type="Shot", shotgun_field_name="code")
        self.assertEqual("Shot", str_field.shotgun_entity_type)
        self.assertEqual("code", str_field.shotgun_field_name)

    def test_repr(self):
        expected = "<Sgtk StringKey field_name>"
        self.assertEqual(expected, str(self.str_field))

    def test_subset(self):
        """
        Test subset_format parameter
        """

        # test properties
        template_field = StringKey("field_name", subset="(.{3}).*")
        self.assertEqual("(.{3}).*", template_field.subset)
        # test bad regex
        self.assertRaises(TankError, StringKey, "field_name", subset="({4}.).*BROKENREGEX")

        # test basic regex
        template_field = StringKey("field_name", subset="(.{3}).*")

        tests = []

        # basic test
        tests.append({"short": "foo", "full": "foobar", "template": StringKey("field_name", subset="(.{3}).*")})
        tests.append({"short": u"foo", "full": u"foobar", "template": StringKey("field_name", subset="(.{3}).*")})

        # unicode
        tests.append({
            "short": u'\u3042\u308a\u304c',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*")}
        )

        # multi token
        tests.append({
            "short": 'JS',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*')}
        )

        for test in tests:

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])



    def test_subset_format(self):
        """
        Test subset_format parameter
        """

        if sys.version_info < (2, 6):
            # subset format not supported in py25
            self.assertRaises(TankError, StringKey, "field_name", subset="(.{3}).*", subset_format="{0} FOO")
            return

        # test properties
        template_field = StringKey("field_name", subset="(.)().*", subset_format="{0} FOO")
        self.assertEqual("{0} FOO", template_field.subset_format)

        # cannot specify subset_format without subset
        self.assertRaises(TankError, StringKey, "field_name", subset_format="{0} FOO")

        tests = []

        # basic test
        tests.append( {
            "short": "\u3042foo ",
            "full": "foobar",
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="\u3042{0} ")
            }
        )

        # unicode
        tests.append( {
            "short": u'\u3042\u308a\u304c ',
            "full": u'\u3042\u308a\u304c\u3068',
            "template": StringKey("field_name", subset="(.{3}).*", subset_format="{0} ")
            }
        )

        # multi token
        tests.append( {
            "short": 'S J',
            "full": 'John Smith',
            "template": StringKey("field_name", subset='([A-Z])[a-z]* ([A-Z])[a-z]*', subset_format="{1} {0}")
            }
        )

        for test in tests:

            print(test)

            short = test["short"]
            full = test["full"]
            template_field = test["template"]

            self.assertEqual(short, template_field.value_from_str(short))
            self.assertEqual(full, template_field.value_from_str(full))

            self.assertEqual(short, template_field.str_from_value(full))

            self.assertTrue(template_field.validate(full))

            self.assertFalse(template_field.validate(short[0]))
            self.assertRaises(TankError, template_field.str_from_value, short[0])