Example #1
0
def test_bcp47_validator_validate():
    bcp = BCP47()
    with patch('bcp47.BCP47.validator', new_callable=PropertyMock) as m:
        m.return_value.validate = MagicMock(return_value=23)
        assert bcp.validate(7) == 23
        assert (
            list(m.return_value.validate.call_args)
            == [(7,), {}])
Example #2
0
def test_code_kwargs(m):
    bcp = BCP47()
    BCP47Code(bcp, foo="foo0", bar="bar0", baz="baz0")
    assert (list(m.call_args) == [(), {
        'foo': 'foo0',
        'bar': 'bar0',
        'baz': 'baz0'
    }])
Example #3
0
def test_code_string(m_lang, m):
    bcp = BCP47()
    m_lang.return_value = "LANG CODE"
    code = BCP47Code(bcp, "foo", "bar", "baz")
    assert str(code) == "LANG CODE"
    assert (repr(code) == (
        "<%s.%s '%s' />" %
        (code.__module__, code.__class__.__name__, code.lang_code)))
Example #4
0
def test_bcp47_code_kwargs():
    bcp = BCP47()
    with patch('bcp47.BCP47Code.construct') as m:
        BCP47Code(bcp, foo="foo0", bar="bar0", baz="baz0")
        assert (list(m.call_args) == [(), {
            'foo': 'foo0',
            'bar': 'bar0',
            'baz': 'baz0'
        }])
Example #5
0
def test_bcp47_code_valid():
    bcp = BCP47()
    with patch('bcp47.BCP47Code.construct'):
        errors = 'bcp47.BCP47Code.errors'
        with patch(errors, new_callable=PropertyMock) as error_m:
            code = BCP47Code(bcp)
            error_m.return_value = [23]
            assert not code.valid
            error_m.return_value = None
            assert code.valid
Example #6
0
def test_scripts(m):
    m.return_value = dict(script=[
        dict(Subtag="foo", other="foo0"),
        dict(Subtag="bar", other="bar0"),
        dict(Subtag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    scripts = bcp47["scripts"]
    assert isinstance(scripts, OrderedDict)
    assert list(scripts.keys()) == ["foo", "bar", "baz"]
    assert list(scripts.values()) == m.return_value["script"]
Example #7
0
def test_extlangs(m):
    m.return_value = dict(extlang=[
        dict(Subtag="foo", other="foo0"),
        dict(Subtag="bar", other="bar0"),
        dict(Subtag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    extlangs = bcp47["extlangs"]
    assert isinstance(extlangs, OrderedDict)
    assert list(extlangs.keys()) == ["foo", "bar", "baz"]
    assert list(extlangs.values()) == m.return_value["extlang"]
Example #8
0
def test_languages(m):
    m.return_value = dict(language=[
        dict(Subtag="foo", other="foo0"),
        dict(Subtag="bar", other="bar0"),
        dict(Subtag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    languages = bcp47["languages"]
    assert isinstance(languages, OrderedDict)
    assert list(languages.keys()) == ["foo", "bar", "baz"]
    assert list(languages.values()) == m.return_value["language"]
Example #9
0
def test_redundants(m):
    m.return_value = dict(redundant=[
        dict(Tag="foo", other="foo0"),
        dict(Tag="bar", other="bar0"),
        dict(Tag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    redundants = bcp47["redundants"]
    assert isinstance(redundants, OrderedDict)
    assert list(redundants.keys()) == ["foo", "bar", "baz"]
    assert list(redundants.values()) == m.return_value["redundant"]
Example #10
0
def test_grandfathereds(m):
    m.return_value = dict(grandfathered=[
        dict(Tag="foo", other="foo0"),
        dict(Tag="bar", other="bar0"),
        dict(Tag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    grandfathereds = bcp47["grandfathereds"]
    assert isinstance(grandfathereds, OrderedDict)
    assert list(grandfathereds.keys()) == ["foo", "bar", "baz"]
    assert list(grandfathereds.values()) == m.return_value["grandfathered"]
Example #11
0
def test_regions(m):
    m.return_value = dict(region=[
        dict(Subtag="foo", other="foo0"),
        dict(Subtag="bar", other="bar0"),
        dict(Subtag="baz", other="baz0")
    ])
    bcp47 = BCP47()
    regions = bcp47["regions"]
    assert isinstance(regions, OrderedDict)
    assert list(regions.keys()) == ["foo", "bar", "baz"]
    assert list(regions.values()) == m.return_value["region"]
Example #12
0
def test_creates_parser():
    bcp = BCP47()
    assert bcp._parser is None
    assert bcp.parser_class is BCP47Parser
    # mangle the parser class
    bcp.parser_class = DummyParser
    assert bcp.parsed == 23
    parser = bcp._parser
    assert isinstance(parser, DummyParser)
    # dont parse again
    assert bcp.parsed == 23
    assert bcp._parser is parser
Example #13
0
def test_bcp47_validator():
    bcp = BCP47()
    assert bcp._validator is None
    assert bcp.validator_class is BCP47Validator
    # mangle the validator class
    bcp.validator_class = DummyValidator
    validator = bcp.validator
    assert isinstance(validator, DummyValidator)
    assert validator.bcp47 is bcp
    new_validator = validator
    assert new_validator is validator
    assert BCP47Validator(13).bcp47 == 13
Example #14
0
def test_bcp47_redundants():

    with patch('bcp47.BCP47.parsed', new_callable=PropertyMock) as m:
        m.return_value = dict(
            redundant=[
                dict(Tag="foo", other="foo0"),
                dict(Tag="bar", other="bar0"),
                dict(Tag="baz", other="baz0")])
        bcp47 = BCP47()
        redundants = bcp47["redundants"]
        assert isinstance(redundants, OrderedDict)
        assert list(redundants.keys()) == ["foo", "bar", "baz"]
        assert list(redundants.values()) == m.return_value["redundant"]
Example #15
0
def test_bcp47_languages():

    with patch('bcp47.BCP47.parsed', new_callable=PropertyMock) as m:
        m.return_value = dict(
            language=[
                dict(Subtag="foo", other="foo0"),
                dict(Subtag="bar", other="bar0"),
                dict(Subtag="baz", other="baz0")])
        bcp47 = BCP47()
        languages = bcp47["languages"]
        assert isinstance(languages, OrderedDict)
        assert list(languages.keys()) == ["foo", "bar", "baz"]
        assert list(languages.values()) == m.return_value["language"]
Example #16
0
def test_bcp47_grandfathereds():

    with patch('bcp47.BCP47.parsed', new_callable=PropertyMock) as m:
        m.return_value = dict(
            grandfathered=[
                dict(Tag="foo", other="foo0"),
                dict(Tag="bar", other="bar0"),
                dict(Tag="baz", other="baz0")])
        bcp47 = BCP47()
        grandfathereds = bcp47["grandfathereds"]
        assert isinstance(grandfathereds, OrderedDict)
        assert list(grandfathereds.keys()) == ["foo", "bar", "baz"]
        assert list(grandfathereds.values()) == m.return_value["grandfathered"]
Example #17
0
def test_bcp47_code_errors():
    bcp = BCP47()
    with patch('bcp47.BCP47Code.construct'):
        with patch('bcp47.BCP47Code.validate') as validate_m:
            validate_m.return_value = [23]
            code = BCP47Code(bcp)
            assert code._errors is None
            errors = code.errors
            assert errors == [23]
            assert code._errors == [23]
            assert code.errors == [23]
            # validate only called once
            assert (list(list(c) for c in validate_m.call_args_list) == [[(),
                                                                          {}]])
Example #18
0
def test_constructor():
    bcp = BCP47()
    assert bcp.code_class is BCP47Code
    bcp.code_class = DummyCode
    code = bcp()
    assert code.bcp47 is bcp
    # test args, kwargs passthrough
    assert code.args == ()
    assert code.kwargs == {}
    code = bcp("foo", "bar", "baz")
    assert code.args == ('foo', 'bar', 'baz')
    assert code.kwargs == {}
    code = bcp(foo="foo0", bar="bar0", baz="baz0")
    assert (code.kwargs == {'foo': 'foo0', 'bar': 'bar0', 'baz': 'baz0'})
    code = bcp("foo", "bar", "baz", foo="foo0", bar="bar0", baz="baz0")
    assert code.args == ('foo', 'bar', 'baz')
    assert (code.kwargs == {'foo': 'foo0', 'bar': 'bar0', 'baz': 'baz0'})
Example #19
0
def test_code_props(m):
    bcp = BCP47()
    code = BCP47Code(bcp)
    code._lang_code = "LANG CODE"
    code.kwargs = {
        "grandfathered": "GRANDFATHERED",
        "language": "LANG",
        "extlang": "EXTLANG",
        "script": "SCRIPT",
        "region": "REGION",
        "variant": "VARIANT"
    }
    assert code.language == "LANG"
    assert code.extlang == "EXTLANG"
    assert code.script == "SCRIPT"
    assert code.region == "REGION"
    assert code.variant == "VARIANT"
    assert code.lang_code == "LANG CODE"
    assert code.grandfathered == "GRANDFATHERED"
Example #20
0
def test_bcp47_code_args_and_kwargs():
    bcp = BCP47()
    with pytest.raises(Exception) as e:
        BCP47Code(bcp, "foo", "bar", "baz", foo="foo0")
    assert e.value.args[0].startswith("Mixture of args and kwargs")
Example #21
0
def test_bcp47_code_string():
    bcp = BCP47()
    with patch('bcp47.BCP47Code.lang_code', new_callable=PropertyMock) as m:
        m.return_value = "LANG CODE"
        code = BCP47Code(bcp, "foo", "bar", "baz")
        assert str(code) == "LANG CODE"
Example #22
0
def test_code_args(m):
    bcp = BCP47()
    BCP47Code(bcp, "foo", "bar", "baz")
    assert (list(m.call_args) == [('foo', 'bar', 'baz'), {}])
Example #23
0
def test_bcp47_code_args():
    bcp = BCP47()
    with patch('bcp47.BCP47Code.construct') as m:
        BCP47Code(bcp, "foo", "bar", "baz")
        assert (list(m.call_args) == [('foo', 'bar', 'baz'), {}])
Example #24
0
def test_bcp47_code_no_args_or_kwargs():
    bcp = BCP47()
    with pytest.raises(Exception) as e:
        BCP47Code(bcp)
    assert e.value.args[0].startswith("No arguments provided")