def test_households_initialization():
    sp.logger.info("Test households initialization methods.")

    pop = sc.prettyobj()
    pop.households = []

    # test no households made
    sp.initialize_empty_households(pop)
    assert pop.n_households == 0, 'Check failed. households.n_households is not 0.'
    print(
        'Check passed. Initially without any households information, households.n_households is 0.'
    )

    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }

    pop.households = homes_by_uids
    pop.n_households = 2

    assert pop.n_households != len(
        pop.households
    ), 'Check failed. pop.n_households and len(pop.households_array) are not aligned.'
    print(
        'Check passed. Initially households.n_households do not match len(households.households_array).'
    )

    pop.households = []
    sp.initialize_empty_households(pop, n_households=5)
    for i in range(pop.n_households):
        assert isinstance(
            pop.households[i], sp.Household
        ) and pop.households[i][
            'hhid'] is None, 'Check failed. households[i] is not a household object.'
    print(f'Check passed. Initialized {pop.n_households} empty households.')

    # test that if there are not enough households when populating, we reinitialize that cover with the correct number
    pop.households = []
    sp.populate_households(pop, homes_by_uids, age_by_uid)
    assert len(pop.households) == len(homes_by_uids), 'Check failed.'
    print('Check passed.')
def test_reset_household_values():
    sp.logger.info(
        "Test resetting household values. Warning these features should only be available when synthpops is set to use vital dynamics."
    )
    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }
    pop = sc.prettyobj()
    pop.households = []
    sp.populate_households(pop, homes_by_uids, age_by_uid)
def test_households_basic():
    sp.logger.info("Test creating generic households.")
    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }

    pop = sp.Pop(n=settings.pop_sizes.small)
    assert pop.get_household(1)['reference_uid'] == pop.homes_by_uids[1][
        0], 'Check failed on pop object.'
    print('Check passed on pop.get_household() method.')

    pop_1 = sc.dcp(pop)
    pop_1.initialize_empty_households(n_households=5)
    assert len(
        pop_1.households
    ) == 5, 'Check failed. Could not re-initialize to 5 households after generation.'
    print('Check passed on pop.initialize_empty_households().')

    pop_1.add_household(sc.dcp(pop.get_household(1)))
    assert len(pop_1.households
               ) == 6, 'Check failed. Could not add a new household to pop.'
    print('Check passed. Could add a new household with pop.add_household().')

    pop_2 = sc.prettyobj()
    pop_2.households = []
    sp.populate_households(pop_2, homes_by_uids, age_by_uid)

    assert pop_2.n_households == len(
        homes_by_uids), "number of households should match."
    for i in range(0, len(homes_by_uids)):
        assert pop_2.households[i]['reference_uid'] == homes_by_uids[i][0]
        assert sp.get_household(
            pop_2, i)['reference_age'] == age_by_uid[homes_by_uids[i][0]]
        assert len(pop_2.households[i]) == len(homes_by_uids[i])
    print(
        'Check passed. Generic households can be populated during class initialization.'
    )

    not_a_household = ''
    with pytest.raises(ValueError):
        sp.add_household(pop_2, not_a_household)
    print(
        'Check passed. Cannot add an object that is not a sp.Household using sp.add_household().'
    )

    with pytest.raises(TypeError):
        sp.get_household(pop_2, 0.5)
    print('Check passed. Cannot get a household with a non-int hhid')

    with pytest.raises(IndexError):
        sp.get_household(pop_2, len(pop_2.households) + 1)
    print(
        'Check passed. Cannot get a household with hhid larger than the number of households'
    )