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

Code before:
        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):
```suggestion
        with pytest.raises(ValidationError, match=r"^Enter a valid value$"):
```

Code after:
        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