Skip to content

Commit

Permalink
#73-Fixed group_join and refactoring WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Fenske committed Aug 10, 2022
1 parent 1c8ed74 commit 7bc5be2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 106 deletions.
26 changes: 6 additions & 20 deletions py_linq/py_linq.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,12 @@ def group_join(
raise TypeError(
u"inner enumerable parameter must be an instance of Enumerable"
)
group_joined = (
Enumerable(iter(self))
.join(
inner_enumerable=inner_enumerable,
outer_key=outer_key,
inner_key=inner_key,
result_func=lambda e: (e[0], e[1]),
)
.group_by(
key_names=["id"],
key=lambda t: outer_key(t[0]),
result_func=lambda g: (
g.first()[0],
g.where(lambda i: inner_key(i[1]) == g.key.id).select(
lambda i: i[1]
),
),
)
.select(lambda gj: result_func(gj))
)
group_joined = self.join(
inner_enumerable=inner_enumerable.group_by(key_names=["id"], key=inner_key),
outer_key=outer_key,
inner_key=lambda g: g.key.id,
result_func=lambda gj: (gj[0], Enumerable(gj[1]._iterable))
).select(result_func)
return group_joined

def any(self, predicate: Callable = None):
Expand Down
130 changes: 44 additions & 86 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
)


def reverse(self, result, element):
return element + " " + result

def sum(self, result, element):
return result + element


@pytest.mark.parametrize("enumerable", [
Enumerable(),
Enumerable([]),
Expand Down Expand Up @@ -77,7 +84,7 @@ def test_get_item(enumerable: Enumerable, index: int, expected: Any) -> None:

@pytest.mark.parametrize("enumerable, index, expected", [
(Enumerable(_simple).element_at, 1, 2),
(Enumerable(_complex).element_at, 1, {"value": 2})
(Enumerable(_complex).element_at, 1, {"value": 2}),
])
def test_element_at(enumerable: Callable, index: int, expected: Any) -> None:
assert enumerable(index) == expected
Expand Down Expand Up @@ -141,6 +148,7 @@ def test_element_at_error(enumerable: Callable, index: int, error: Exception) ->
(Enumerable().contains(1), False),
(Enumerable(_simple).contains(1), True),
(Enumerable(_complex).select(lambda x: x["value"]).contains(1), True),
(Enumerable(_complex).group_join(Enumerable([1, 2, 3, 3]), outer_key=lambda x: x["value"]).count(), 3),
])
def test_executors(value: Union[int, float], total: Union[int, float]) -> None:
assert value == total
Expand All @@ -160,6 +168,7 @@ def test_executors_error(enumerable: Callable, error: Exception) -> None:


@pytest.mark.parametrize("enumerable, expected", [
(Enumerable.empty(), []),
(Enumerable().select(lambda x: x["value"]), []),
(Enumerable(_simple).select(lambda x: {"value": x}), _complex),
(Enumerable(_complex).select(lambda x: x["value"]), _simple),
Expand Down Expand Up @@ -253,28 +262,38 @@ def test_executors_error(enumerable: Callable, error: Exception) -> None:
(Enumerable(_complex).union(Enumerable([{"value": 1}, {"value": 4}, {"value": 5}]), lambda x: x["value"]), [{"value": 1}, {"value": 2}, {"value": 3}, {"value": 4}, {"value": 5}]),
(Enumerable().group_join(Enumerable()), []),
(Enumerable(_simple).group_join(Enumerable()), []),
(Enumerable(_complex).group_join(Enumerable([1, 2, 3, 3]), outer_key=lambda x: x["value"]), [({"value": 1}, [1]), ({"value": 2}, [2]), ({"value": 3}, [3, 3])]),
# def test_group_join(self):
# complex_simple_gj = Enumerable(
# [{"value": 1}, {"value": 2}, {"value": 3}]
# ).group_join(Enumerable([1, 2, 3, 3]), outer_key=lambda x: x["value"])
# self.assertListEqual(
# [({"value": 1}, [1]), ({"value": 2}, [2]), ({"value": 3}, [3, 3])],
# complex_simple_gj.select(lambda g: (g[0], g[1].to_list())).to_list(),
# )
# simple_gj = Enumerable([1, 2, 3]).group_join(
# Enumerable([2, 3]),
# result_func=lambda x: {"number": x[0], "collection": x[1].to_list()},
# )
# self.assertEqual(2, simple_gj.count())
# self.assertListEqual(
# [
# {"number": 2, "collection": [2]},
# {"number": 3, "collection": [3]},
# ],
# simple_gj.to_list(),
# )
(Enumerable(_complex).group_join(Enumerable([1, 2, 3, 3]), outer_key=lambda x: x["value"]).select(lambda t: (t[0], t[1].to_list())), [({"value": 1}, [1]), ({"value": 2}, [2]), ({"value": 3}, [3, 3])]),
(Enumerable(_simple).group_join(Enumerable([2, 3]), result_func=lambda x: {"number": x[0], "collection": x[1].to_list()}), [{"number": 2, "collection": [2]}, {"number": 3, "collection": [3]}]),
(Enumerable(_locations).order_by(lambda l: l[0]).then_by(lambda l: l[1]).select(lambda l: f"{l[1]}, {l[0]}"), [
"Liverpool, England",
"Liverpool, England",
"London, England",
"London, England",
"London, England",
"Manchester, England",
"Manchester, England",
"Edinburgh, Scotland",
"Glasgow, Scotland",
"Glasgow, Scotland",
"Bangor, Wales",
"Cardiff, Wales",
"Cardiff, Wales",
]),
(Enumerable(_locations).order_by(lambda l: l[0]).then_by_descending(lambda l: l[3]).select(lambda l: f"{l[1]}, {l[0]}: {l[3]}"), [
"London, England: 90000",
"London, England: 80000",
"London, England: 70000",
"Manchester, England: 50000",
"Manchester, England: 45600",
"Liverpool, England: 29700",
"Liverpool, England: 25000",
"Edinburgh, Scotland: 20000",
"Glasgow, Scotland: 12500",
"Glasgow, Scotland: 12000",
"Cardiff, Wales: 30000",
"Cardiff, Wales: 29700",
"Bangor, Wales: 12800",
]),
])
def test_non_executors(enumerable: Enumerable, expected: List) -> None:
assert enumerable.to_list() == expected
Expand All @@ -298,6 +317,7 @@ def test_non_executors(enumerable: Enumerable, expected: List) -> None:
(Enumerable().except_, None, TypeError),
(Enumerable().union, None, TypeError),
(Enumerable.empty().group_join, None, TypeError),
(Enumerable(_locations).order_by(lambda l: l[0]).then_by, None, NullArgumentError),
])
def test_non_executors_error(enumerable: Callable, func: Callable, error: Exception) -> None:
with pytest.raises(error):
Expand Down Expand Up @@ -332,70 +352,13 @@ def test_group_by_operations() -> None:
lambda c: c.key.city == "London" and c.key.country == "England"
)
assert london.sum(lambda c: c[3]) == 240000



# class TestFunctions(TestCase):



# def test_then_by(self):
# locations = Enumerable(_locations)
# self.assertRaises(
# NullArgumentError, locations.order_by(lambda l: l[0]).then_by, None
# )
# self.assertListEqual(
# [
# u"Liverpool, England",
# u"Liverpool, England",
# u"London, England",
# u"London, England",
# u"London, England",
# u"Manchester, England",
# u"Manchester, England",
# u"Edinburgh, Scotland",
# u"Glasgow, Scotland",
# u"Glasgow, Scotland",
# u"Bangor, Wales",
# u"Cardiff, Wales",
# u"Cardiff, Wales",
# ],
# locations.order_by(lambda l: l[0])
# .then_by(lambda l: l[1])
# .select(lambda l: u"{0}, {1}".format(l[1], l[0]))
# .to_list(),
# )

# def test_then_by_descending(self):
# locations = Enumerable(_locations)
# self.assertListEqual(
# [
# u"Liverpool, England: 29700",
# u"Liverpool, England: 25000",
# u"London, England: 90000",
# u"London, England: 80000",
# u"London, England: 70000",
# u"Manchester, England: 50000",
# u"Manchester, England: 45600",
# u"Edinburgh, Scotland: 20000",
# u"Glasgow, Scotland: 12500",
# u"Glasgow, Scotland: 12000",
# u"Bangor, Wales: 12800",
# u"Cardiff, Wales: 30000",
# u"Cardiff, Wales: 29700",
# ],
# locations.order_by(lambda l: l[0])
# .then_by(lambda l: l[1])
# .then_by_descending(lambda l: l[3])
# .select(lambda l: u"{0}, {1}: {2}".format(l[1], l[0], l[3]))
# .to_list(),
# )

# def reverse(self, result, element):
# return element + " " + result

# def sum(self, result, element):
# return result + element

# def test_aggregate(self):
# words = u"the quick brown fox jumps over the lazy dog".split(" ")
# self.assertListEqual(
Expand Down Expand Up @@ -430,11 +393,6 @@ def test_group_by_operations() -> None:
# self.assertEqual(test.count(), 4)
# self.assertEqual(test.element_at(0), 4)

# def test_empty(self):
# test = Enumerable.empty()
# self.assertIsInstance(test, Enumerable)
# self.assertEqual(test.count(), 0)

# def test_range(self):
# test = Enumerable.range(1, 3)
# self.assertEqual(test.count(), 3)
Expand Down

0 comments on commit 7bc5be2

Please sign in to comment.