Skip to content

Commit

Permalink
refactored to options
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jun 21, 2024
1 parent 2e22adc commit 1bcfe44
Show file tree
Hide file tree
Showing 209 changed files with 1,794 additions and 1,640 deletions.
2 changes: 1 addition & 1 deletion audits/src/main/kotlin/dev/morphia/audits/RstAuditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class RstAuditor(val type: OperatorType) {
} else {
method.setBody(
"""
|testPipeline(dev.morphia.test.ServerVersion.ANY, false, true, aggregation -> aggregation
|testPipeline(new dev.morphia.test.util.ActionTestOptions().serverVersion(dev.morphia.test.ServerVersion.ANY).removeIds(false).orderMatters(true), aggregation -> aggregation
| .pipeline(
|
|)); """
Expand Down
417 changes: 161 additions & 256 deletions core/src/test/java/dev/morphia/test/TemplatedTestBase.java

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions core/src/test/java/dev/morphia/test/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public abstract class TestBase extends MorphiaTestSetup {
}
}

protected DriverVersion minDriver = DriverVersion.v41;

public TestBase() {
}

Expand Down Expand Up @@ -362,11 +360,6 @@ private void assertSameType(String path, Object actual, Object expected) {
}
}

@BeforeMethod
private void setDriverMinimum() {
minDriver = DriverVersion.v43;
}

public static class ZDTCodecProvider implements CodecProvider {
@Override
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
Expand Down
21 changes: 11 additions & 10 deletions core/src/test/java/dev/morphia/test/TestTransactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Reference;
import dev.morphia.query.filters.Filters;
import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.mapping.lazy.TestLazyCircularReference.ReferencedEntity;
import dev.morphia.test.mapping.lazy.TestLazyCircularReference.RootEntity;
import dev.morphia.test.models.Rectangle;
Expand All @@ -34,7 +33,7 @@
import static org.testng.Assert.assertNull;

//@Tags(@Tag("transactions"))
public class TestTransactions extends AggregationTest {
public class TestTransactions extends dev.morphia.test.TemplatedTestBase {
@BeforeMethod
public void before() {
checkForReplicaSet();
Expand Down Expand Up @@ -168,14 +167,16 @@ public void merge() {
@Test(testName = "transactional aggregations")
public void aggregation() {
getDs().withTransaction(session -> {
testPipeline(ServerVersion.ANY, false, false, aggregation -> {
loadData("aggTest2", 2);
return aggregation
.lookup(Lookup.lookup("aggTest2")
.localField("item")
.foreignField("sku")
.as("inventory_docs"));
});
testPipeline(
new dev.morphia.test.util.ActionTestOptions().serverVersion(ServerVersion.ANY).removeIds(false).orderMatters(false),
aggregation -> {
loadData("aggTest2", 2);
return aggregation
.lookup(Lookup.lookup("aggTest2")
.localField("item")
.foreignField("sku")
.as("inventory_docs"));
});
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,9 @@

package dev.morphia.test.aggregation;

import java.util.List;
import java.util.function.Function;

import dev.morphia.aggregation.Aggregation;
import dev.morphia.test.ServerVersion;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.Comparanator;

import org.bson.Document;

@SuppressWarnings({ "unused", "MismatchedQueryAndUpdateOfCollection" })
public class AggregationTest extends TemplatedTestBase {

public void testPipeline(ServerVersion serverVersion,
Function<Aggregation<Document>, Aggregation<Document>> pipeline) {
testPipeline(serverVersion, true, true, pipeline);
}

public void testPipeline(ServerVersion serverVersion,
boolean removeIds,
boolean orderMatters,
Function<Aggregation<Document>, Aggregation<Document>> pipeline) {
checkMinServerVersion(serverVersion);
checkMinDriverVersion(minDriver);
var resourceName = discoverResourceName();
validateTestName(resourceName);
loadData(resourceName, EXAMPLE_TEST_COLLECTION);
loadIndex(resourceName, EXAMPLE_TEST_COLLECTION);

List<Document> actual = runPipeline(resourceName, pipeline.apply(getDs().aggregate(EXAMPLE_TEST_COLLECTION)));

if (!skipDataCheck) {
List<Document> expected = loadExpected(resourceName);

actual = removeIds ? removeIds(actual) : actual;
expected = removeIds ? removeIds(expected) : expected;

try {
Comparanator.of(null, actual, expected, orderMatters).compare();
} catch (AssertionError e) {
throw new AssertionError("%s\n\n actual: %s".formatted(e.getMessage(), toString(actual, "\n\t")),
e);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.test.aggregation.expressions;

import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

Expand All @@ -9,15 +10,16 @@
import static dev.morphia.aggregation.stages.Projection.project;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAbs extends AggregationTest {
public class TestAbs extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/abs/example1
*
*/
@Test(testName = "main")
public void testExample1() {
testPipeline(ANY, false, true, aggregation -> aggregation
.pipeline(project().include("delta", abs(subtract("$startTemp", "$endTemp")))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation
.pipeline(project().include("delta", abs(subtract("$startTemp", "$endTemp")))));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import java.util.List;

import dev.morphia.aggregation.stages.Group;
import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

import static dev.morphia.aggregation.expressions.AccumulatorExpressions.accumulator;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAccumulator extends AggregationTest {
public class TestAccumulator extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/accumulator/example1
*
*/
@Test(testName = "Use ``$accumulator`` to Implement the ``$avg`` Operator")
public void testExample1() {
skipActionCheck();
testPipeline(ANY, false, false,
testPipeline(
new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(false).skipActionCheck(true),
aggregation -> aggregation.pipeline(Group.group(Group.id("$author")).field("avgCopies", accumulator("""
function() {
return { count: 0, sum: 0 }
Expand Down Expand Up @@ -47,29 +48,32 @@ public void testExample1() {
*/
@Test(testName = "Use ``initArgs`` to Vary the Initial State by Group")
public void testExample2() {
skipActionCheck();
testPipeline(ANY, false, false, aggregation -> aggregation
.pipeline(Group.group(Group.id().field("city", "$city")).field("restaurants", accumulator("""
function(city, userProfileCity) { \s
return {
max: city === userProfileCity ? 3 : 1, \s
restaurants: [] \s
}\s
}""", """
function(state, restaurantName) { \s
if (state.restaurants.length < state.max) {
state.restaurants.push(restaurantName);
}
return state;
}""", List.of("$name"), """
function(state1, state2) { \s
return {
max: state1.max,
restaurants: state1.restaurants.concat(state2.restaurants).slice(0, state1.max)
}\s
}""").initArgs(List.of("$city", "Bettles")).finalizeFunction("""
function(state) {
return state.restaurants
}"""))));
testPipeline(
new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(false).skipActionCheck(true),
aggregation -> aggregation.pipeline(Group.group(Group.id().field("city", "$city")).field("restaurants",
accumulator("""
function(city, userProfileCity) { \s
return {
max: city === userProfileCity ? 3 : 1, \s
restaurants: [] \s
}\s
}""", """
function(state, restaurantName) { \s
if (state.restaurants.length < state.max) {
state.restaurants.push(restaurantName);
}
return state;
}""", List.of("$name"),
"""
function(state1, state2) { \s
return {
max: state1.max,
restaurants: state1.restaurants.concat(state2.restaurants).slice(0, state1.max)
}\s
}""")
.initArgs(List.of("$city", "Bettles")).finalizeFunction("""
function(state) {
return state.restaurants
}"""))));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.test.aggregation.expressions;

import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

Expand All @@ -10,15 +11,16 @@
import static dev.morphia.aggregation.stages.AddFields.addFields;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAcos extends AggregationTest {
public class TestAcos extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/acos/example1
*
*/
@Test(testName = "main :: Inverse Cosine of Value in Degrees")
public void testExample1() {
testPipeline(ANY, false, true, aggregation -> aggregation
.pipeline(addFields().field("angle_a", radiansToDegrees(acos(divide("$side_b", "$hypotenuse"))))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation.pipeline(
addFields().field("angle_a", radiansToDegrees(acos(divide("$side_b", "$hypotenuse"))))));

}

Expand All @@ -28,8 +30,9 @@ public void testExample1() {
*/
@Test(testName = "main :: Inverse Cosine of Value in Radians")
public void testExample2() {
testPipeline(ANY, false, true, aggregation -> aggregation
.pipeline(addFields().field("angle_a", acos(divide("$side_b", "$hypotenuse")))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation
.pipeline(addFields().field("angle_a", acos(divide("$side_b", "$hypotenuse")))));

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.test.aggregation.expressions;

import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

Expand All @@ -9,15 +10,16 @@
import static dev.morphia.aggregation.stages.AddFields.addFields;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAcosh extends AggregationTest {
public class TestAcosh extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/acosh/example1
*
*/
@Test(testName = "main :: Inverse Hyperbolic Cosine in Degrees")
public void testExample1() {
testPipeline(ANY, false, true, aggregation -> aggregation
.pipeline(addFields().field("y-coordinate", radiansToDegrees(acosh("$x-coordinate")))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation
.pipeline(addFields().field("y-coordinate", radiansToDegrees(acosh("$x-coordinate")))));

}

Expand All @@ -27,7 +29,7 @@ public void testExample1() {
*/
@Test(testName = "main :: Inverse Hyperbolic Cosine in Radians")
public void testExample2() {
testPipeline(ANY, false, true,
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation.pipeline(addFields().field("y-coordinate", acosh("$x-coordinate"))));

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package dev.morphia.test.aggregation.expressions;

import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

import static dev.morphia.aggregation.expressions.MathExpressions.add;
import static dev.morphia.aggregation.stages.Projection.project;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAdd extends AggregationTest {
public class TestAdd extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/add/example1
*
*/
@Test(testName = "Add Numbers")
public void testExample1() {
testPipeline(ANY, false, true, (aggregation) -> aggregation
.pipeline(project().include("item").include("total", add("$price", "$fee"))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
(aggregation) -> aggregation
.pipeline(project().include("item").include("total", add("$price", "$fee"))));
}

/**
Expand All @@ -25,8 +27,9 @@ public void testExample1() {
*/
@Test(testName = "Perform Addition on a Date")
public void testExample2() {
testPipeline(ANY, false, true, aggregation -> aggregation
.pipeline(project().include("item", 1).include("billing_date", add("$date", 259200000))));
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(true),
aggregation -> aggregation
.pipeline(project().include("item", 1).include("billing_date", add("$date", 259200000))));

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.morphia.test.aggregation.expressions;

import dev.morphia.aggregation.stages.Group;
import dev.morphia.test.aggregation.AggregationTest;
import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.testng.annotations.Test;

Expand All @@ -14,14 +15,14 @@
import static dev.morphia.query.Sort.ascending;
import static dev.morphia.test.ServerVersion.ANY;

public class TestAddToSet extends AggregationTest {
public class TestAddToSet extends TemplatedTestBase {
/**
* test data: dev/morphia/test/aggregation/expressions/addToSet/example1
*
*/
@Test(testName = "Use in ``$group`` Stage")
public void testExample1() {
testPipeline(ANY, false, false,
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(false),
aggregation -> aggregation
.pipeline(group(Group.id().field("day", dayOfYear("$date")).field("year", year("$date")))
.field("itemsSold", addToSet("$item")))
Expand All @@ -38,7 +39,7 @@ public void testExample1() {
public void testExample2() {
if (1 == 1)
return;
testPipeline(ANY, false, false,
testPipeline(new ActionTestOptions().serverVersion(ANY).removeIds(false).orderMatters(false),
aggregation -> aggregation.setWindowFields(setWindowFields().partitionBy("$state")
.sortBy(ascending("orderDate")).output(output("cakeTypesForState").operator(addToSet("$type"))
.window().documents("unbounded", "current"))));
Expand Down
Loading

0 comments on commit 1bcfe44

Please sign in to comment.