iMitwe
iMitwe

Reputation: 1257

In pytest set up databases mirroring and test

I have a Django app that reads a read_only replica from a model in the DB. So in the pytest conftest fixtures, I have this settings.DATABASES["read_only"]["TEST"] = {"MIRROR": "default"} but when I instantiate fixtures, the read_only database doesn't have the data that I created with factoryboy.

@pytest.fixture()
def populate_cache() -> Callable[[CountryFactory], Household]:
    """
    Fixture to populate the dashboard cache for a specific business area,
    verify creation in the default DB, and ensure readability in the read_only DB.
    """

    def _populate_cache(goodcountry: CountryFactory) -> Household:
        # Create household and related records
        household, individuals = create_household("business_area": afghanistan)
        PaymentFactory.create_batch(5, household=household)
        PaymentRecordFactory.create_batch(3, household=household)
        # Verify data exists in the default DB
        payment_count_default = Payment.objects.using("default").filter(household=household).count()
        print(f"Payments in default DB: {payment_count_default}")
        # Verify data accessibility in the read_only DB
        payment_count_read_only = Payment.objects.using("read_only").filter(household=household).count()
        print(f"Payments in read_only DB: {payment_count_read_only}")
        # Assert that the data is accessible in the read_only DB
        assert payment_count_read_only == payment_count_default, "Mismatch in Payment count between default and read_only DBs."

        return household

    return _populate_dashboard_cache

and I get an error:

Payments in default DB: 5 Payments in read_only DB: 0

Upvotes: 0

Views: 25

Answers (0)

Related Questions