Back to Repositories

Validating Dictionary Structure Implementations in Requests Library

This test suite validates the functionality of custom dictionary implementations in the Requests library, specifically CaseInsensitiveDict and LookupDict classes. The tests ensure proper handling of case-insensitive key operations and lookup behavior essential for HTTP header management.

Test Coverage Overview

The test suite provides comprehensive coverage of dictionary operations with focus on case-insensitive key handling and lookup functionality.

Key areas tested include:
  • Basic dictionary operations (get, set, delete)
  • Case-insensitive key matching
  • Dictionary copying and equality comparison
  • Custom lookup behavior with default values

Implementation Analysis

The testing approach utilizes pytest’s parametrized testing to validate multiple input variations efficiently. The implementation leverages fixtures for test setup and employs systematic validation of dictionary behaviors across different case variations and edge cases.

Notable patterns include:
  • Fixture-based test instance initialization
  • Parametrized test cases for multiple key variations
  • Systematic validation of dictionary contract compliance

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest.fixture for test setup management
  • pytest.mark.parametrize for input variation testing
  • Automated fixture setup with autouse=True
  • Custom assertion patterns for dictionary behavior validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including systematic test organization and thorough edge case coverage.

Notable practices include:
  • Consistent test method naming conventions
  • Efficient test setup using fixtures
  • Comprehensive input variation testing
  • Clear separation of test concerns
  • Proper encapsulation of test setup and teardown

psf/requests

tests/test_structures.py

            
import pytest

from requests.structures import CaseInsensitiveDict, LookupDict


class TestCaseInsensitiveDict:
    @pytest.fixture(autouse=True)
    def setup(self):
        """CaseInsensitiveDict instance with "Accept" header."""
        self.case_insensitive_dict = CaseInsensitiveDict()
        self.case_insensitive_dict["Accept"] = "application/json"

    def test_list(self):
        assert list(self.case_insensitive_dict) == ["Accept"]

    possible_keys = pytest.mark.parametrize(
        "key", ("accept", "ACCEPT", "aCcEpT", "Accept")
    )

    @possible_keys
    def test_getitem(self, key):
        assert self.case_insensitive_dict[key] == "application/json"

    @possible_keys
    def test_delitem(self, key):
        del self.case_insensitive_dict[key]
        assert key not in self.case_insensitive_dict

    def test_lower_items(self):
        assert list(self.case_insensitive_dict.lower_items()) == [
            ("accept", "application/json")
        ]

    def test_repr(self):
        assert repr(self.case_insensitive_dict) == "{'Accept': 'application/json'}"

    def test_copy(self):
        copy = self.case_insensitive_dict.copy()
        assert copy is not self.case_insensitive_dict
        assert copy == self.case_insensitive_dict

    @pytest.mark.parametrize(
        "other, result",
        (
            ({"AccePT": "application/json"}, True),
            ({}, False),
            (None, False),
        ),
    )
    def test_instance_equality(self, other, result):
        assert (self.case_insensitive_dict == other) is result


class TestLookupDict:
    @pytest.fixture(autouse=True)
    def setup(self):
        """LookupDict instance with "bad_gateway" attribute."""
        self.lookup_dict = LookupDict("test")
        self.lookup_dict.bad_gateway = 502

    def test_repr(self):
        assert repr(self.lookup_dict) == "<lookup 'test'>"

    get_item_parameters = pytest.mark.parametrize(
        "key, value",
        (
            ("bad_gateway", 502),
            ("not_a_key", None),
        ),
    )

    @get_item_parameters
    def test_getitem(self, key, value):
        assert self.lookup_dict[key] == value

    @get_item_parameters
    def test_get(self, key, value):
        assert self.lookup_dict.get(key) == value