예제 #1
0
 def test_where_binary_equals_returns_get_attribute_expression_on_lhs(self):
     col = []
     tree = From(col).where("some.other.property == 'Bernardo'")
     error = "Lhs should be GetAttributeExpression but was %s"
     class_name = tree.expressions[0].__class__.__name__
     assert isinstance(tree.expressions[0].lhs, GetAttributeExpression), \
                         error % class_name
예제 #2
0
    def test_select_many_returns_proper_results_for_sub_property(self):
        class SomeElement(object):
            def __init__(self, value):
                self.value = value

            def __str__(self):
                return str(self.value)

        col = [
            SomeElement(1),
            SomeElement(2),
            SomeElement(3),
            SomeElement(4),
            SomeElement(5)
        ]

        items = From(col).where(
            "item.value > 2 and item.value < 4").select_many()

        assert len(
            items
        ) == 1, "Only item 3 should be in the resulting collection, but it has length of %d" % len(
            items)
        assert items[
            0].value == 3, "Only item 3 should be in the resulting collection but it was %s." % items[
                0].value
예제 #3
0
    def test_desc_order_for_expression(self):
        result = From(
            self.col).order_by("-(item.first + item.second)").select_many()

        assert result[0].first == 7
        assert result[1].first == 4
        assert result[2].first == 1
예제 #4
0
 def test_select_many_returns_proper_results_for_numbers(self):
     items = From([1, 2, 3, 4,
                   5]).where("item > 2 and item < 4").select_many()
     assert items == [
         3
     ], "Only item 3 should be in the resulting collection but it was %s." % ",".join(
         items)
예제 #5
0
 def test_collection_provider_filters_using_binary_expression(self):
     col = ["a", "b"]
     query = From(col).where("item == 'a'")
     provider = query.provider
     result = provider.parse(query, Actions.SelectMany)
     assert result == [
         'a'
     ], "The collection was not filtered properly and now is: %s" % result
예제 #6
0
    def test_mixed_expression_order(self):
        result = From(
            self.col).order_by("item.second + item.third",
                               "item.first + item.second").select_many()

        assert result[0].first == 7
        assert result[1].first == 1
        assert result[2].first == 4
예제 #7
0
    def test_where_binary_equals_returns_binary_expression(self):
        col = []
        tree = From(col).where("some.other.property == 'Bernardo'")

        assert len(
            tree.expressions) == 1, "There should be one where expression"
        assert isinstance(tree.expressions[0], BinaryExpression), \
                "The first expression of the tree should be a BinaryExpression"
예제 #8
0
 def test_collection_provider_parses_query_using_lesser_than(self):
     col = range(5)
     query = From(col).where("item <= 3")
     provider = query.provider
     result = provider.parse(query, Actions.SelectMany)
     assert result == range(
         4
     ), "The collection was not filtered properly and now is: %s" % result
예제 #9
0
    def test_where_binary_equals_returns_tree(self):
        col = []
        tree = From(col).where("some.other.property == 'Bernardo'")

        assert tree is not None, "The From method needs to return something"
        assert isinstance(
            tree,
            Query), "The lambda should have resolved to a LambdaExpression"
예제 #10
0
def select_expression_fields():
    two_values_col = [TwoValues(item, item + 2) for item in range(ITERATIONS/2)]

    start_time = time.time()

    for i in range(2):
        total = From(two_values_col).select("item.value + item.value2", "item.value2 - item.value")

    print "Selecting Two Expression Fields %d ITEMS OPERATION - 2 iterations took %.2f" % (ITERATIONS/2, (time.time() - start_time))
예제 #11
0
def run_two_big_collections():
    dynamic_col = [OneValue(item) for item in range(ITERATIONS/2)]

    start_time = time.time()

    for i in range(2):
        total = From(dynamic_col).avg("item.value.value")

    print "AVG %d ITEMS OPERATION - 2 iterations took %.2f" % (ITERATIONS/2, (time.time() - start_time))
예제 #12
0
def run_many_small_collections():
    start_time = time.time()
    
    fixed_col = [OneValue(1), OneValue(2), OneValue(3)]
    
    for i in range(ITERATIONS):
        total = From(fixed_col).avg("item.value.value")

    print "AVG FIXED COL OPERATION - %d iterations took %.2f" % (ITERATIONS, (time.time() - start_time))
예제 #13
0
 def test_collection_provider_filters_using_binary_expression_for_numbers(
         self):
     col = [1, 2, 10, 11, 12]
     query = From(col).where("item > 10")
     provider = query.provider
     result = provider.parse(query, Actions.SelectMany)
     assert result == [
         11, 12
     ], "The collection was not filtered properly and now is: %s" % result
예제 #14
0
 def test_querying_with_invalid_action_raises(self):
     error = "Invalid action exception. invalid_action is unknown."
     q = From([1, 2, 3])
     provider = q.provider
     self.assertRaisesEx(ValueError,
                         provider.parse,
                         q,
                         "invalid_action",
                         exc_pattern=re.compile(error))
예제 #15
0
 def test_max_returns_right_amount_for_a_given_sub_property(self):
     class OtherValue(object):
         def __init__(self, value):
             self.value = value
             
     class OneValue(object):
         def __init__(self, value):
             self.value = OtherValue(value)
             
     value = From([OneValue(1), OneValue(2), OneValue(3)]).max("item.value.value")
     assert value == 3, "value should be 3 but was %s" % value
예제 #16
0
 def test_max_raises_for_an_invalid_property(self):
     error_message = "The attribute '%s' was not found in the specified collection's items. If you meant to use the raw value of each item in the collection just use the word 'item' as a parameter to .max or use .max()"
     
     class OneValue(object):
         def __init__(self, value):
             self.value = value
     fr = From([OneValue(1), OneValue(2), OneValue(3)])
     self.assertRaisesEx(ValueError, fr.max, "value", exc_pattern=re.compile(error_message % "value"))
     self.assertRaisesEx(ValueError, fr.max, "item.dumb", exc_pattern=re.compile(error_message % "item.dumb"))
     self.assertRaisesEx(ValueError, fr.max, "", exc_pattern=re.compile(error_message % ""))
     self.assertRaisesEx(ValueError, fr.max, None, exc_pattern=re.compile(error_message % "None"))
예제 #17
0
 def test_grouping_returns_the_right_items_on_select_many(self):
     items = From(self.col).order_by("first").group_by("second").select(
         "first", "second")
     assert items[2][0].first == 1
     assert items[2][0].second == 2
     assert items[5][0].first == 4
     assert items[5][0].second == 5
     assert items[5][1].first == 7
     assert items[5][1].second == 5
     assert not hasattr(items[2][0], "third")
     assert not hasattr(items[5][0], "third")
     assert not hasattr(items[5][1], "third")
예제 #18
0
 def test_grouping_with_strings_returns_the_right_items_on_select_many(
         self):
     new_col = [
         self.TestGroupByClass("a", "z", "a"),
         self.TestGroupByClass("b", "w", "b"),
         self.TestGroupByClass("c", "z", "c")
     ]
     items = From(new_col).order_by("first").group_by(
         "second").select_many()
     assert items["z"][0].first == "a"
     assert items["z"][1].first == "c"
     assert items["w"][0].first == "b"
예제 #19
0
 def test_where_binary_equals_returns_tree_name_expressions_as_attributes_on_lhs(
         self):
     col = []
     tree = From(col).where("some.other.property == 'Bernardo'")
     error_message = "There should be three attributes "\
                     "('some','other','property') in the GetAttributeExpression, "\
                     "but there was %d"
     assert len(tree.expressions[0].lhs.attributes) == 3, \
             error_message % len(tree.expressions[0].lhs.attributes)
     for i in range(3):
         error = "The %d parameter should be a NameExpression but was %s"
         class_name = tree.expressions[0].lhs.attributes[
             i].__class__.__name__
         assert isinstance(tree.expressions[0].lhs.attributes[i], NameExpression), \
                             error % (i, class_name)
예제 #20
0
    def test_select_field_operator_add(self):
        class Item:
            def __init__(self, name, value, value2):
                self.name = name
                self.value = value
                self.value2 = value2

        col = [Item("A", 10, 10), Item("B", 20, 20), Item("C", 30, 30)]
        mod = From(col).select("name", "item.value + item.value2")

        assert mod[0].name == "A"
        assert mod[0].dynamic_1 == 20

        assert mod[1].name == "B"
        assert mod[1].dynamic_1 == 40

        assert mod[2].name == "C"
        assert mod[2].dynamic_1 == 60
예제 #21
0
    def test_where_not_returns_proper_results(self):
        col = [
            self.TwoValues(1, 2),
            self.TwoValues(2, 3),
            self.TwoValues(3, 4),
            self.TwoValues(4, 5),
            self.TwoValues(5, 6)
        ]

        items = From(col).where("not (item.value < 4)").select_many()

        assert len(
            items
        ) == 2, "Only items 4 and 5 should be in the resulting collection, but it has length of %d" % len(
            items)
        assert items[
            0].value == 4, "Item 4 should be in the resulting collection but it was %s." % items[
                0].value
        assert items[
            1].value == 5, "Item 5 should be in the resulting collection but it was %s." % items[
                1].value
예제 #22
0
    def test_select_many_expressions_at_once(self):
        class Item:
            def __init__(self, name, value, value2):
                self.name = name
                self.value = value
                self.value2 = value2

        col = [Item("A", 10, 20), Item("B", 20, 30), Item("C", 30, 40)]
        mod = From(col).select("name", "item.value2 - item.value",
                               "item.value * item.value2")

        assert mod[0].name == "A"
        assert mod[0].dynamic_1 == 10
        assert mod[0].dynamic_2 == 200

        assert mod[1].name == "B"
        assert mod[1].dynamic_1 == 10
        assert mod[1].dynamic_2 == 600

        assert mod[2].name == "C"
        assert mod[2].dynamic_1 == 10
        assert mod[2].dynamic_2 == 1200
예제 #23
0
 def test_min_returns_right_amount_for_full_collection(self):
     value = From([1,2,3]).min("item")
     assert value == 1, "value should be 1 but was %s" % value
예제 #24
0
 def test_min_returns_right_amount_for_filtered_collection_with_no_keyword(self):
     value = From([1,2,3,4]).where("item >= 2").min()
     assert value == 2, "value should be 2 but was %s" % value
예제 #25
0
 def test_min_returns_right_amount_for_full_collection_with_no_keyword(self):
     value = From([1,2,3]).min()
     assert value == 1, "value should be 1 but was %s" % value
예제 #26
0
 def test_max_returns_right_amount_for_filtered_collection(self):
     value = From([1,2,3,4]).where("item <= 2").max("item")
     assert value == 2, "value should be 2 but was %s" % value
예제 #27
0
 def test_grouping_returns_the_right_items_on_select_many(self):
     items = From(
         self.col).order_by("first").group_by("second").select_many()
     assert items[2][0].first == 1
     assert items[5][0].first == 4
     assert items[5][1].first == 7
예제 #28
0
 def test_grouping_returns_two_keys_on_select_many(self):
     items = From(self.col).group_by("second").select_many()
     assert len(items.keys()) == 2
예제 #29
0
 def test_grouping_returns_the_two_right_keys_on_select_many(self):
     items = From(self.col).group_by("second").select_many()
     assert items.has_key(2)
     assert items.has_key(5)
예제 #30
0
 def test_min_returns_right_amount_for_filtered_collection(self):
     value = From([1,2,3,4]).where("item > 2").min("item")
     assert value == 3, "value should be 3 but was %s" % value
예제 #31
0
 def test_min_returns_right_amount_for_a_given_property(self):
     class OneValue(object):
         def __init__(self, value):
             self.value = value
     value = From([OneValue(1), OneValue(2), OneValue(3)]).min("item.value")
     assert value == 1, "value should be 1 but was %s" % value
예제 #32
0
 def test_max_returns_right_amount_for_filtered_collection_with_no_keyword(self):
     value = From([1,2,3,4]).where("item <= 3").max()
     assert value == 3, "value should be 3 but was %s" % value