コード例 #1
0
 def test_get(self):
     cid = CaseInsensitiveDict()
     cid["spam"] = "oneval"
     cid["SPAM"] = "blueval"
     assert cid.get("spam") == "blueval"
     assert cid.get("SPAM") == "blueval"
     assert cid.get("sPam") == "blueval"
     assert cid.get("notspam", "default") == "default"
コード例 #2
0
 def test_equality(self):
     cid = CaseInsensitiveDict({"SPAM": "blueval", "Eggs": "redval"})
     othercid = CaseInsensitiveDict({"spam": "blueval", "eggs": "redval"})
     assert cid == othercid
     del othercid["spam"]
     assert cid != othercid
     assert cid == {"spam": "blueval", "eggs": "redval"}
     assert cid != object()
コード例 #3
0
def summarize_all_fakers(faker) -> T.Sequence[FakerInfo]:
    """Summarize information about all fakers"""
    from snowfakery.utils.collections import CaseInsensitiveDict

    # get config info that can override samples etc.
    with (Path(__file__).parent / "docs_config.yml").open() as f:
        yaml_data = yaml.safe_load(f)
        common_fakes = yaml_data["common_fakes"]
        uncommon_fakes = yaml_data["uncommon_fakes"]

    faker_infos = CaseInsensitiveDict()
    for name, meth in faker.fake_names.items():
        if not isinstance(meth, types.MethodType):
            continue
        # python magic to introspect classnames, filenames, etc.
        friendly = _to_camel_case(name)
        func = meth.__func__
        doc = func.__doc__
        filename = func.__code__.co_filename
        cls = meth.__self__.__class__
        fullname = cls.__module__ + "." + cls.__name__ + "." + meth.__name__
        overrides = common_fakes.get(meth.__name__) or uncommon_fakes.get(
            meth.__name__)
        is_common = meth.__name__ in common_fakes

        # if it came from Faker
        if "/faker/" in filename:
            source = "faker"
            idx = filename.find("/faker/")
            url = "https://github.com/joke2k/faker/tree/master" + filename[idx:]
            parts = filename.split("/")
            while parts[-1] in ("__init__.py", "en_US"):
                del parts[-1]
            category = parts[-1]
        else:  # if it came from Snowfakery
            source = "snowfakery"
            idx = filename.find("/snowfakery/")
            url = ("https://github.com/SFDO-Tooling/Snowfakery/tree/main" +
                   filename[idx:])
            category = "Salesforce"

        faker_info = faker_infos.setdefault(
            friendly,
            FakerInfo(
                friendly,
                fullname,
                [],
                url,
                source,
                category,
                doc or "",
                is_common,
                overrides.get("example") if overrides else None,
            ),
        )
        faker_info.aliases.append(name)

    return faker_infos.values()
コード例 #4
0
 def test_lower_items(self):
     cid = CaseInsensitiveDict(
         {
             "Accept": "application/json",
             "user-Agent": "requests",
         }
     )
     keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
     lowerkeyset = frozenset(["accept", "user-agent"])
     assert keyset == lowerkeyset
コード例 #5
0
 def test_fixes_649(self):
     """__setitem__ should behave case-insensitively."""
     cid = CaseInsensitiveDict()
     cid["spam"] = "oneval"
     cid["Spam"] = "twoval"
     cid["sPAM"] = "redval"
     cid["SPAM"] = "blueval"
     assert cid["spam"] == "blueval"
     assert cid["SPAM"] == "blueval"
     assert list(cid.keys()) == ["SPAM"]
コード例 #6
0
 def test_preserve_key_case(self):
     cid = CaseInsensitiveDict(
         {
             "Accept": "application/json",
             "user-Agent": "requests",
         }
     )
     keyset = frozenset(["Accept", "user-Agent"])
     assert frozenset(i[0] for i in cid.items()) == keyset
     assert frozenset(cid.keys()) == keyset
     assert frozenset(cid) == keyset
コード例 #7
0
 def test_copy(self):
     cid = CaseInsensitiveDict(
         {
             "Accept": "application/json",
             "user-Agent": "requests",
         }
     )
     cid_copy = cid.copy()
     assert str(cid) == str(cid_copy)
     assert cid == cid_copy
     cid["changed"] = True
     assert cid != cid_copy
コード例 #8
0
 def test_contains(self):
     cid = CaseInsensitiveDict()
     cid["Spam"] = "someval"
     assert "Spam" in cid
     assert "spam" in cid
     assert "SPAM" in cid
     assert "sPam" in cid
     assert "notspam" not in cid
コード例 #9
0
 def test_update(self):
     cid = CaseInsensitiveDict()
     cid["spam"] = "blueval"
     cid.update({"sPam": "notblueval"})
     assert cid["spam"] == "notblueval"
     cid = CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"})
     cid.update({"fOO": "anotherfoo", "bAR": "anotherbar"})
     assert len(cid) == 2
     assert cid["foo"] == "anotherfoo"
     assert cid["bar"] == "anotherbar"
コード例 #10
0
 def test_preserve_last_key_case(self):
     cid = CaseInsensitiveDict(
         {
             "Accept": "application/json",
             "user-Agent": "requests",
         }
     )
     cid.update({"ACCEPT": "application/json"})
     cid["USER-AGENT"] = "requests"
     keyset = frozenset(["ACCEPT", "USER-AGENT"])
     assert frozenset(i[0] for i in cid.items()) == keyset
     assert frozenset(cid.keys()) == keyset
     assert frozenset(cid) == keyset
コード例 #11
0
 def test_iter(self):
     cid = CaseInsensitiveDict({"Spam": "spam", "Eggs": "eggs"})
     keys = frozenset(["Spam", "Eggs"])
     assert frozenset(iter(cid)) == keys
コード例 #12
0
 def test_update_retains_unchanged(self):
     cid = CaseInsensitiveDict({"foo": "foo", "bar": "bar"})
     cid.update({"foo": "newfoo"})
     assert cid["bar"] == "bar"
コード例 #13
0
class TestCaseInsensitiveDict:
    @pytest.mark.parametrize(
        "cid",
        (
            CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"}),
            CaseInsensitiveDict([("Foo", "foo"), ("BAr", "bar")]),
            CaseInsensitiveDict(FOO="foo", BAr="bar"),
        ),
    )
    def test_init(self, cid):
        assert len(cid) == 2
        assert "foo" in cid
        assert "bar" in cid

    def test_docstring_example(self):
        cid = CaseInsensitiveDict()
        cid["Accept"] = "application/json"
        assert cid["aCCEPT"] == "application/json"
        assert list(cid) == ["Accept"]

    def test_len(self):
        cid = CaseInsensitiveDict({"a": "a", "b": "b"})
        cid["A"] = "a"
        assert len(cid) == 2

    def test_getitem(self):
        cid = CaseInsensitiveDict({"Spam": "blueval"})
        assert cid["spam"] == "blueval"
        assert cid["SPAM"] == "blueval"

    def test_fixes_649(self):
        """__setitem__ should behave case-insensitively."""
        cid = CaseInsensitiveDict()
        cid["spam"] = "oneval"
        cid["Spam"] = "twoval"
        cid["sPAM"] = "redval"
        cid["SPAM"] = "blueval"
        assert cid["spam"] == "blueval"
        assert cid["SPAM"] == "blueval"
        assert list(cid.keys()) == ["SPAM"]

    def test_delitem(self):
        cid = CaseInsensitiveDict()
        cid["Spam"] = "someval"
        del cid["sPam"]
        assert "spam" not in cid
        assert len(cid) == 0

    def test_contains(self):
        cid = CaseInsensitiveDict()
        cid["Spam"] = "someval"
        assert "Spam" in cid
        assert "spam" in cid
        assert "SPAM" in cid
        assert "sPam" in cid
        assert "notspam" not in cid

    def test_get(self):
        cid = CaseInsensitiveDict()
        cid["spam"] = "oneval"
        cid["SPAM"] = "blueval"
        assert cid.get("spam") == "blueval"
        assert cid.get("SPAM") == "blueval"
        assert cid.get("sPam") == "blueval"
        assert cid.get("notspam", "default") == "default"

    def test_update(self):
        cid = CaseInsensitiveDict()
        cid["spam"] = "blueval"
        cid.update({"sPam": "notblueval"})
        assert cid["spam"] == "notblueval"
        cid = CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"})
        cid.update({"fOO": "anotherfoo", "bAR": "anotherbar"})
        assert len(cid) == 2
        assert cid["foo"] == "anotherfoo"
        assert cid["bar"] == "anotherbar"

    def test_update_retains_unchanged(self):
        cid = CaseInsensitiveDict({"foo": "foo", "bar": "bar"})
        cid.update({"foo": "newfoo"})
        assert cid["bar"] == "bar"

    def test_iter(self):
        cid = CaseInsensitiveDict({"Spam": "spam", "Eggs": "eggs"})
        keys = frozenset(["Spam", "Eggs"])
        assert frozenset(iter(cid)) == keys

    def test_equality(self):
        cid = CaseInsensitiveDict({"SPAM": "blueval", "Eggs": "redval"})
        othercid = CaseInsensitiveDict({"spam": "blueval", "eggs": "redval"})
        assert cid == othercid
        del othercid["spam"]
        assert cid != othercid
        assert cid == {"spam": "blueval", "eggs": "redval"}
        assert cid != object()

    def test_setdefault(self):
        cid = CaseInsensitiveDict({"Spam": "blueval"})
        assert cid.setdefault("spam", "notblueval") == "blueval"
        assert cid.setdefault("notspam", "notblueval") == "notblueval"

    def test_lower_items(self):
        cid = CaseInsensitiveDict(
            {
                "Accept": "application/json",
                "user-Agent": "requests",
            }
        )
        keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
        lowerkeyset = frozenset(["accept", "user-agent"])
        assert keyset == lowerkeyset

    def test_preserve_key_case(self):
        cid = CaseInsensitiveDict(
            {
                "Accept": "application/json",
                "user-Agent": "requests",
            }
        )
        keyset = frozenset(["Accept", "user-Agent"])
        assert frozenset(i[0] for i in cid.items()) == keyset
        assert frozenset(cid.keys()) == keyset
        assert frozenset(cid) == keyset

    def test_preserve_last_key_case(self):
        cid = CaseInsensitiveDict(
            {
                "Accept": "application/json",
                "user-Agent": "requests",
            }
        )
        cid.update({"ACCEPT": "application/json"})
        cid["USER-AGENT"] = "requests"
        keyset = frozenset(["ACCEPT", "USER-AGENT"])
        assert frozenset(i[0] for i in cid.items()) == keyset
        assert frozenset(cid.keys()) == keyset
        assert frozenset(cid) == keyset

    def test_copy(self):
        cid = CaseInsensitiveDict(
            {
                "Accept": "application/json",
                "user-Agent": "requests",
            }
        )
        cid_copy = cid.copy()
        assert str(cid) == str(cid_copy)
        assert cid == cid_copy
        cid["changed"] = True
        assert cid != cid_copy
コード例 #14
0
 def test_delitem(self):
     cid = CaseInsensitiveDict()
     cid["Spam"] = "someval"
     del cid["sPam"]
     assert "spam" not in cid
     assert len(cid) == 0
コード例 #15
0
 def test_setdefault(self):
     cid = CaseInsensitiveDict({"Spam": "blueval"})
     assert cid.setdefault("spam", "notblueval") == "blueval"
     assert cid.setdefault("notspam", "notblueval") == "notblueval"
コード例 #16
0
 def test_getitem(self):
     cid = CaseInsensitiveDict({"Spam": "blueval"})
     assert cid["spam"] == "blueval"
     assert cid["SPAM"] == "blueval"
コード例 #17
0
 def test_len(self):
     cid = CaseInsensitiveDict({"a": "a", "b": "b"})
     cid["A"] = "a"
     assert len(cid) == 2
コード例 #18
0
 def test_docstring_example(self):
     cid = CaseInsensitiveDict()
     cid["Accept"] = "application/json"
     assert cid["aCCEPT"] == "application/json"
     assert list(cid) == ["Accept"]