def test_top_level_string_multiple_matches(): d = collections.OrderedDict( title="my dataset", last_updated="recently", update_frequency="quite often", ) losser.query("update", d) == ["recently", "quite often"]
def test_unicode_string(): string_transformations = [lambda x: x[:3]] result = losser.query([], u" fübar ", strip=True, string_transformations=string_transformations) assert result == u"füb"
def test_dict_of_lists(): d = collections.OrderedDict(( ("foo", [1, 2, 3]), ("bar", [4, 5, 6]), ("foobar", ["a", "b", "c"]), )) assert losser.query("foo", d) == [1, 2, 3, "a", "b", "c"]
def test_top_level_string_no_matches(): d = collections.OrderedDict( title="my dataset", last_updated="recently", update_frequency="quite often", ) assert losser.query("maintainer", d) is None
def test_terminal_value_is_list_of_scalars(): """If the pattern path terminates at a list that list is returned as is.""" d = collections.OrderedDict(title="my dataset", last_updated="recently", update_frequency="quite often", resources=[1, 2, 3]) assert losser.query("^resources$", d) == [1, 2, 3]
def test_string_with_strip_and_transform(): string_transformations = [lambda x: x[:3]] result = losser.query([], " foobar ", strip=True, string_transformations=string_transformations) assert result == "foo"
def test_sub_dict_with_one_match(): d = collections.OrderedDict(title="my dataset", extras=dict( foo="bar", something_else="some other thing", )) pattern = ["^extras$", "foo"] assert losser.query(pattern, d) == "bar"
def test_sub_dict_with_multiple_matches(): d = collections.OrderedDict(title="my dataset", extras=dict( foo="bar", foo_again="bah", )) pattern = ["^extras$", "foo"] assert losser.query(pattern, d) == ["bar", "bah"]
def test_list_of_dicts(): d = collections.OrderedDict( title="my dataset", resources=[dict(format="CSV"), dict(format="JSON"), dict(foo="bar")], ) pattern = ["^resources$", "format"] result = losser.query(pattern, d) assert result == ["CSV", "JSON"]
def test_terminal_value_is_list_of_scalars(): """If the pattern path terminates at a list that list is returned as is.""" d = collections.OrderedDict( title="my dataset", last_updated="recently", update_frequency="quite often", resources=[1, 2, 3] ) assert losser.query("^resources$", d) == [1, 2, 3]
def test_case_sensitive_matching(): d = collections.OrderedDict(title="my dataset", extras=dict( Title="the right title", title="the wrong title", something_else="some other thing", )) pattern = ["^extras$", "Title"] assert losser.query(pattern, d, case_sensitive=True) == "the right title"
def test_sub_dict_with_one_match(): d = collections.OrderedDict( title="my dataset", extras=dict( foo="bar", something_else="some other thing", ) ) pattern = ["^extras$", "foo"] assert losser.query(pattern, d) == "bar"
def test_sub_dict_with_multiple_matches(): d = collections.OrderedDict( title="my dataset", extras=dict( foo="bar", foo_again="bah", ) ) pattern = ["^extras$", "foo"] assert losser.query(pattern, d) == ["bar", "bah"]
def test_deduplicate(): """Test the deduplicate option.""" d = collections.OrderedDict( title="my dataset", resources=[dict(format="CSV"), dict(format="CSV"), dict(format="JSON")], ) pattern = ["^resources$", "format"] result = losser.query(pattern, d, deduplicate=True) assert result == ["CSV", "JSON"]
def test_sub_dict_multiple_matches_returns_multiple_results(): d = collections.OrderedDict(title="my dataset", extras=dict( foo="bar", foo_again="bah", )) pattern = ["^extras$", "foo"] nose.tools.assert_equals( losser.query(pattern, d, return_multiple_columns=True), collections.OrderedDict([('extras_foo', ['bar']), ('extras_foo_again', ['bah'])]))
def test_case_sensitive_matching(): d = collections.OrderedDict( title="my dataset", extras=dict( Title="the right title", title="the wrong title", something_else="some other thing", ) ) pattern = ["^extras$", "Title"] assert losser.query(pattern, d, case_sensitive=True) == "the right title"
def test_deduplicate(): """Test the deduplicate option.""" d = collections.OrderedDict( title="my dataset", resources=[ dict(format="CSV"), dict(format="CSV"), dict(format="JSON") ], ) pattern = ["^resources$", "format"] result = losser.query(pattern, d, deduplicate=True) assert result == ["CSV", "JSON"]
def test_sub_dict_multiple_matches_returns_multiple_results(): d = collections.OrderedDict( title="my dataset", extras=dict( foo="bar", foo_again="bah", ) ) pattern = ["^extras$", "foo"] nose.tools.assert_equals( losser.query(pattern, d, return_multiple_columns=True), collections.OrderedDict( [('extras_foo', ['bar']), ('extras_foo_again', ['bah'])] ) )
def test_list_of_dicts_multiple_matches(): """Test processing a list of dicts when some of the individual dicts have multiple matches.""" d = collections.OrderedDict( title="my dataset", resources=[ collections.OrderedDict( (("format", "CSV"), ("formatting", "commas"))), collections.OrderedDict( (("format", "JSON"), ("formatting", "pretty printed"))), dict(foo="bar") ], ) pattern = ["^resources$", "format"] result = losser.query(pattern, d) assert result == ["CSV", "commas", "JSON", "pretty printed"]
def test_list_of_dicts_multiple_matches(): """Test processing a list of dicts when some of the individual dicts have multiple matches.""" d = collections.OrderedDict( title="my dataset", resources=[ collections.OrderedDict(( ("format", "CSV"), ("formatting", "commas"))), collections.OrderedDict(( ("format", "JSON"), ("formatting", "pretty printed"))), dict(foo="bar") ], ) pattern = ["^resources$", "format"] result = losser.query(pattern, d) assert result == ["CSV", "commas", "JSON", "pretty printed"]
def test_top_level_string_single_match(): assert losser.query("^title$", {"title": "my title"}) == "my title"
def test_boolean(): assert losser.query([], False) == False
def test_none(): assert losser.query([], None) == None
def test_tuples(): """Test that tuples are treated the same as lists.""" d = {"foo": ((1, 2, 3), (4, 5, 6), ("a", "b", "c"))} result = losser.query("foo", d) assert result == [1, 2, 3, 4, 5, 6, "a", "b", "c"]
def test_string(): assert losser.query([], "foo") == "foo"
def test_list_of_lists(): d = {"foo": [[1, 2, 3], [4, 5, 6], ["a", "b", "c"]]} result = losser.query("foo", d) assert result == [1, 2, 3, 4, 5, 6, "a", "b", "c"]
def test_string_with_strip(): assert losser.query([], " foo ", strip=True) == "foo"
def test_number(): assert losser.query([], 42) == 42