def test_meta_account_loader_filters_with_multi_word_attr_names(): d = [ { "id": "100200300400", "acct status": "active account" }, { "id": "200300400100", "acct status": "account suspended" }, { "id": "300400100200", "acct status": "active account" }, ] mal = acctload.MetaAccountLoader(d) acct = mal.accounts(exclude={"acct_status": ["active account"]})[0] assert acct.id == "200300400100"
def test_meta_account_loader_accounts_missing_ids(): d = [ { "id": "100200300400", "status": "active" }, { "id": "200300400100" }, { "id": "300400100200", "env": "dev", "status": "active" }, ] mal = acctload.MetaAccountLoader(d) acct = mal.accounts(acct_ids=["200300400100"])[0] assert acct.id == "200300400100" # These should not raise exceptions as the MetaAccountLoader adds missing # keys so all account objects have all attributes. assert acct.env is None assert acct.status is None
def test_load_with_exclude_attrs(exclude_attrs, one_acct_list, expected): mal = acctload.MetaAccountLoader(one_acct_list, exclude_attrs=exclude_attrs) assert mal.accts == expected, "dicts are not equal" assert set(mal.attributes()) == set(expected[0].keys())
def test_load_with_include_attrs_that_doesnt_have_the_id_attr( id_attr, include_attrs, one_acct_list): with pytest.raises(ValueError): acctload.MetaAccountLoader(one_acct_list, id_attr=id_attr, include_attrs=include_attrs)
def test_load_with_bad_custom_str_template(str_template, one_acct_list): with pytest.raises(acctload.InvalidFormatTemplateError): acctload.MetaAccountLoader(one_acct_list, str_template=str_template)
def test_load_with_custom_str_template(str_template, one_acct_list, expected): mal = acctload.MetaAccountLoader(one_acct_list, str_template=str_template) assert str( mal.accounts()[0]) == expected, "str() output does not match expected"
def test_load_with_invalid_acct_id(test_input): with pytest.raises(ValueError): acctload.MetaAccountLoader(test_input)
def test_load_with_nonmatching_acct_key(test_input): with pytest.raises(ValueError): acctload.MetaAccountLoader(test_input)
def test_load_with_custom_id_attribute(test_input): d = {} d[test_input] = "10" mal = acctload.MetaAccountLoader([d], id_attr=test_input) assert mal.acct_id( mal.accounts()[0]) == "10", "custom id did not return acct #"
def test_load_without_default_id_attribute(test_input): with pytest.raises(ValueError): acctload.MetaAccountLoader(test_input)
def test_load_with_key_rewriting(test_input, expected): mal = acctload.MetaAccountLoader(test_input) assert mal.accts == expected, "dict key names not rewritten"
def test_load_basic_with_dict_of_accts(test_input, expected): mal = acctload.MetaAccountLoader(test_input) assert mal.accts == expected, "dicts are not equal"
def test_meta_account_loader_acct_id(many_acct_list, acct_id): mal = acctload.MetaAccountLoader(many_acct_list) accts = mal.accounts(acct_ids=(acct_id, )) assert len(accts) == 1 assert mal.acct_id(accts[0]) == acct_id
def test_meta_account_loader_accounts_by_invalid_id(many_acct_list): mal = acctload.MetaAccountLoader(many_acct_list) with pytest.raises(acctload.AccountsNotFoundError) as e: mal.accounts(acct_ids=["100200300400", "no_such_id", "200300400100"]) assert e.value.missing_acct_ids == ["no_such_id"]
def test_load_with_path(path, test_input, expected): mal = acctload.MetaAccountLoader(test_input, path=path) assert mal.accts == expected, "dicts are not equal"
def test_load_with_bad_custom_id_attribute(test_input, one_acct_list): with pytest.raises(ValueError): acctload.MetaAccountLoader(one_acct_list, id_attr=test_input)
def test_load_with_invalid_path(path, test_input, expected): with pytest.raises(expected): acctload.MetaAccountLoader(test_input, path=path)
def test_meta_account_loader_accounts(many_acct_list, test_input, include, exclude, expected): mal = acctload.MetaAccountLoader(many_acct_list) accts = mal.accounts(acct_ids=test_input, include=include, exclude=exclude) acct_ids = set([a.id for a in accts]) assert acct_ids == expected