Skip to content

Commit

Permalink
Refactored tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuschka committed Nov 3, 2023
1 parent 3ba2bd7 commit a79d8dd
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 146 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/github/cbuschka/objset/BiSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ default void forEach(BiConsumer<Element1, Element2> consumer) {
stream().forEach(consumer);
}

default List<Tuple<Element1, Element2>> toList() {
default List<Pair<Element1, Element2>> toList() {
return tupleStream().collect(Collectors.toList());
}

Stream<Tuple<Element1, Element2>> tupleStream();
Stream<Pair<Element1, Element2>> tupleStream();
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class Tuple<Element1, Element2> {
public class Pair<Element1, Element2> {
private final Element1 element1;
private final Element2 element2;

public static <Element1, Element2> Tuple<Element1, Element2> of(Element1 element1, Element2 element2) {
return new Tuple<>(element1, element2);
public static <Element1, Element2> Pair<Element1, Element2> of(Element1 element1, Element2 element2) {
return new Pair<>(element1, element2);
}

public Element1 element1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

public class BiSelectImpl<Element1, Element2> implements FilterableBiSelect<Element1, Element2> {
private final ObjectSetImpl objectSet;
private final Iterable<Tuple<Element1, Element2>> tupleStream;
private final Iterable<Pair<Element1, Element2>> tupleStream;

public BiSelectImpl(ObjectSetImpl objectSet, Iterable<Tuple<Element1, Element2>> tupleStream) {
public BiSelectImpl(ObjectSetImpl objectSet, Iterable<Pair<Element1, Element2>> tupleStream) {
this.objectSet = objectSet;
this.tupleStream = tupleStream;
}

@Override
public Stream<Tuple<Element1, Element2>> tupleStream() {
public Stream<Pair<Element1, Element2>> tupleStream() {
return StreamSupport.stream(tupleStream.spliterator(), false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.github.cbuschka.objset.impl;

import io.github.cbuschka.objset.BiStream;
import io.github.cbuschka.objset.Tuple;
import io.github.cbuschka.objset.Pair;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class BiStreamImpl<Element1, Element2> implements BiStream<Element1, Element2> {
private final Stream<Tuple<Element1, Element2>> source;
private final Stream<Pair<Element1, Element2>> source;

public BiStreamImpl(Stream<Tuple<Element1, Element2>> source) {
public BiStreamImpl(Stream<Pair<Element1, Element2>> source) {
this.source = source;
}

private Stream<Tuple<Element1, Element2>> tupleStream() {
private Stream<Pair<Element1, Element2>> tupleStream() {
return StreamSupport.stream(source.spliterator(), false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public UniSelectImpl(ObjectSetImpl objectSet, Iterable<Element1> elements) {
@Override
public <Element2, KeyType> FilterableBiSelect<Element1, Element2> join(Class<Element2> element2Type, Function<Element1, KeyType> element1KeyFunc, Function<Element2, KeyType> element2KeyFunc) {
List<Element2> right = objectSet.getElementsFor(element2Type);
return new BiSelectImpl<>(objectSet, Joins.innerJoin(elements, element1KeyFunc, right, element2KeyFunc, Tuple::of));
return new BiSelectImpl<>(objectSet, Joins.innerJoin(elements, element1KeyFunc, right, element2KeyFunc, Pair::of));
}

@Override
public <Element2, KeyType> BiSelect<Element1, Element2> leftOuterJoin(Class<Element2> element2Type, Function<Element1, KeyType> element1KeyFunc, Function<Element2, KeyType> element2KeyFunc) {
List<Element2> right = objectSet.getElementsFor(element2Type);
return new BiSelectImpl<>(objectSet, Joins.leftOuterJoin(elements, element1KeyFunc, right, element2KeyFunc, Tuple::of));
return new BiSelectImpl<>(objectSet, Joins.leftOuterJoin(elements, element1KeyFunc, right, element2KeyFunc, Pair::of));
}

@Override
Expand Down
122 changes: 64 additions & 58 deletions src/test/java/io/github/cbuschka/objset/BiOpsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.cbuschka.objset;

import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -15,62 +16,67 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BiOpsTest {
@Mock
private Person person1;
@Mock
private Person person2;
@Mock
private Address address1;
@Mock
private Address address2;

@Test
void joinTwoEntities() {
when(person1.getId()).thenReturn(1L);
when(person2.getId()).thenReturn(2L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);

List<Tuple<Person, Address>> result = ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.toList();

assertThat(result).containsExactly(Tuple.of(person1, address1), Tuple.of(person1, address2));
}

@Test
void leftOuterJoinTwoEntities() {
when(person1.getId()).thenReturn(1L);
when(person2.getId()).thenReturn(2L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);

List<Tuple<Person, Address>> result = ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.select(Person.class)
.leftOuterJoin(Address.class, Person::getId, Address::getPersonId)
.toList();

assertThat(result).containsExactly(Tuple.of(person1, address1), Tuple.of(person1, address2),
Tuple.of(person2, null));
}

@Test
void filteredOfTwoEntities() {
when(person1.getId()).thenReturn(1L);
when(person2.getId()).thenReturn(2L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);

List<Tuple<Person, Address>> result = ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.select(Person.class)
.leftOuterJoin(Address.class, Person::getId, Address::getPersonId)
.where((i, s) -> s != address2)
.toList();

assertThat(result).containsExactly(Tuple.of(person1, address1), Tuple.of(person2, null));
}

@Mock
private Person person1;
@Mock
private Person person2;
@Mock
private Address address1;
@Mock
private Address address2;
private List<Pair<Person, Address>> result;

@Test
void joinTwoEntities() {
givenIsPerson1WithTwoAddresses();
givenIsPerson2WithoutAddress();

whenQueried(objSet -> objSet.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.toList());

assertThat(result).containsExactly(Pair.of(person1, address1), Pair.of(person1, address2));
}

private void givenIsPerson2WithoutAddress() {
when(person2.getId()).thenReturn(2L);
}

private void givenIsPerson1WithTwoAddresses() {
when(person1.getId()).thenReturn(1L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);
}

@Test
void leftOuterJoinTwoEntities() {
givenIsPerson1WithTwoAddresses();
givenIsPerson2WithoutAddress();

whenQueried(objSet -> objSet.select(Person.class)
.leftOuterJoin(Address.class, Person::getId, Address::getPersonId)
.toList());

assertThat(result).containsExactly(Pair.of(person1, address1), Pair.of(person1, address2),
Pair.of(person2, null));
}

@Test
void filteredOfTwoEntities() {
givenIsPerson1WithTwoAddresses();
givenIsPerson2WithoutAddress();

whenQueried(objSet -> objSet.select(Person.class)
.leftOuterJoin(Address.class, Person::getId, Address::getPersonId)
.where((i, s) -> s != address2)
.toList());

assertThat(result).containsExactly(Pair.of(person1, address1), Pair.of(person2, null));
}

private void whenQueried(Function<ObjectSet, List<Pair<Person, Address>>> func) {
this.result = func.apply(ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2)));
}
}
4 changes: 2 additions & 2 deletions src/test/java/io/github/cbuschka/objset/InnerJoinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ private void thenResultIsEmpty() {
}

private void whenInnerJoined() {
this.result = toList(Joins.innerJoin(left, s -> s.charAt(0), right, s -> s.charAt(0), Tuple::of));
this.result = toList(Joins.innerJoin(left, s -> s.charAt(0), right, s -> s.charAt(0), Pair::of));
}

private List<String> toList(Iterable<Tuple<String, String>> tuples) {
private List<String> toList(Iterable<Pair<String, String>> tuples) {
return StreamSupport.stream(tuples.spliterator(), false)
.map(t -> t.element1() + ":" + t.element2())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ private void thenResultIsEmpty() {
}

private void whenLeftOuterJoined() {
this.result = toList(Joins.leftOuterJoin(left, s -> s.charAt(0), right, s -> s.charAt(0), Tuple::of));
this.result = toList(Joins.leftOuterJoin(left, s -> s.charAt(0), right, s -> s.charAt(0), Pair::of));
}

private List<String> toList(Iterable<Tuple<String, String>> tuples) {
private List<String> toList(Iterable<Pair<String, String>> tuples) {
return StreamSupport.stream(tuples.spliterator(), false)
.map(t -> t.element1() + ":" + t.element2())
.collect(Collectors.toList());
Expand Down
98 changes: 52 additions & 46 deletions src/test/java/io/github/cbuschka/objset/TriOpsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.cbuschka.objset;

import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -15,57 +16,62 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class TriOpsTest {
@Mock
private Person person1;
@Mock
private Person person2;
@Mock
private Address address1;
@Mock
private Address address2;
@Mock
private Order order1;
@Mock
private Item item1;

@Test
void joinThreeEntities() {
when(person1.getId()).thenReturn(1L);
when(person2.getId()).thenReturn(2L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);
when(order1.getPersonId()).thenReturn(1L);
@Mock
private Person person1;
@Mock
private Person person2;
@Mock
private Address address1;
@Mock
private Address address2;
@Mock
private Order order1;

List<Triple<Person, Address, Order>> result = ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.with(Order.class, List.of(order1))
.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.join(Order.class, (i, s) -> i.getId(), Order::getPersonId)
.toList();
private List<Triple<Person, Address, Order>> result;

@Test
void joinThreeEntities() {
givenPerson1WithTwoAddressesAndOrder();
givenPerson2WithoutAddressesAndOrder();

assertThat(result).containsExactly(Triple.of(person1, address1, order1),
Triple.of(person1, address2, order1));
}
whenQueried(objSet -> objSet.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.join(Order.class, (person, address) -> person.getId(), Order::getPersonId)
.toList());

@Test
void filteredOfThreeEntities() {
when(person1.getId()).thenReturn(1L);
when(person2.getId()).thenReturn(2L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);
when(order1.getPersonId()).thenReturn(1L);
assertThat(result).containsExactly(Triple.of(person1, address1, order1),
Triple.of(person1, address2, order1));
}

List<Triple<Person, Address, Order>> result = ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.with(Order.class, List.of(order1))
.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.join(Order.class, (i, s) -> i.getId(), Order::getPersonId)
.where((i, s, ss) -> s != address2)
.toList();
private void whenQueried(Function<ObjectSet, List<Triple<Person, Address, Order>>> func) {
this.result = func.apply(ObjectSet.of(Person.class, List.of(person1, person2))
.with(Address.class, List.of(address1, address2))
.with(Order.class, List.of(order1)));
}

assertThat(result).containsExactly(Triple.of(person1, address1, order1));
}
private void givenPerson2WithoutAddressesAndOrder() {
when(person2.getId()).thenReturn(2L);
}

private void givenPerson1WithTwoAddressesAndOrder() {
when(person1.getId()).thenReturn(1L);
when(address1.getPersonId()).thenReturn(1L);
when(address2.getPersonId()).thenReturn(1L);
when(order1.getPersonId()).thenReturn(1L);
}

@Test
void filteredOfThreeEntities() {
givenPerson1WithTwoAddressesAndOrder();
givenPerson2WithoutAddressesAndOrder();

whenQueried(objSet -> objSet.select(Person.class)
.join(Address.class, Person::getId, Address::getPersonId)
.join(Order.class, (person, address) -> person.getId(), Order::getPersonId)
.where((i, s, ss) -> s != address2)
.toList());

assertThat(result).containsExactly(Triple.of(person1, address1, order1));
}
}
Loading

0 comments on commit a79d8dd

Please sign in to comment.