Repository: ansible/awx
File: awx/main/tests/unit/utils/test_common.py (Python)

Code before:
    def custom_err_message(self):
        return "foobar"

    def test_hostame_regex_validator_constructor_with_args(self, regex_expr, re_flags, custom_err_message):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, message=custom_err_message)
        assert h.regex == _lazy_re_compile(regex_expr, re_flags)
        assert h.message == 'foobar'
        assert h.code == 'invalid'
        assert h.inverse_match == False
        assert h.flags == re_flags

    def test_hostame_regex_validator_default_constructor(self, regex_expr, re_flags):
        h = HostnameRegexValidator()
        assert h.regex == _lazy_re_compile(regex_expr, re_flags)
        assert h.message == 'Enter a valid value.'
        assert h.code == 'invalid'
        assert h.inverse_match == False
        assert h.flags == re_flags

    def test_good_call(self, regex_expr, re_flags):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags)
        h("192.168.56.101")  # Should not raise ValidationError

    def test_bad_call(self, regex_expr, re_flags):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags)
        with pytest.raises(ValidationError, match="illegal characters detected"):
            h("@#$%)$#(TUFAS_DG")

    def test_good_call_with_inverse(self, regex_expr, re_flags, inverse_match=True):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, inverse_match=inverse_match)
        with pytest.raises(ValidationError, match="Enter a valid value"):
            h("1.2.3.4")

    def test_bad_call_with_inverse(self, regex_expr, re_flags, inverse_match=True):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, inverse_match=inverse_match)
        h("@#$%)$#(TUFAS_DG")  # Should not raise ValidationError


Review comment (suggestion):
Whenever possible, I recommend using raw-strings and regex markers — without them, this arg often confuses people into thinking its a simple string inclusion check but it's actually a regex match.
```suggestion
        with pytest.raises(ValidationError, match=r"^illegal characters detected$"):
```

Code after:
    def custom_err_message(self):
        return "foobar"

    def test_hostame_regex_validator_constructor_with_args(self, regex_expr, re_flags, custom_err_message):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, message=custom_err_message)
        assert h.regex == _lazy_re_compile(regex_expr, re_flags)
        assert h.message == 'foobar'
        assert h.code == 'invalid'
        assert h.inverse_match == False
        assert h.flags == re_flags

    def test_hostame_regex_validator_default_constructor(self, regex_expr, re_flags):
        h = HostnameRegexValidator()
        assert h.regex == _lazy_re_compile(regex_expr, re_flags)
        assert h.message == 'Enter a valid value.'
        assert h.code == 'invalid'
        assert h.inverse_match == False
        assert h.flags == re_flags

    def test_good_call(self, regex_expr, re_flags):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags)
        assert h("192.168.56.101") is None

    def test_bad_call(self, regex_expr, re_flags):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags)
        with pytest.raises(ValidationError, match=r"^\['illegal characters detected in hostname=@#\$%\)\$#\(TUFAS_DG. Please verify.'\]$"):
            h("@#$%)$#(TUFAS_DG")

    def test_good_call_with_inverse(self, regex_expr, re_flags, inverse_match=True):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, inverse_match=inverse_match)
        with pytest.raises(ValidationError, match=r"^\['Enter a valid value.'\]$"):
            h("1.2.3.4")

    def test_bad_call_with_inverse(self, regex_expr, re_flags, inverse_match=True):
        h = HostnameRegexValidator(regex=regex_expr, flags=re_flags, inverse_match=inverse_match)
        assert h("@#$%)$#(TUFAS_DG") is None