From cb7a6e7ab09f472765c2195d37bc4b14f157fb52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 24 Jan 2024 14:25:54 +0100 Subject: [PATCH] PHPLIB-1342 Add tests on Arithmetic Expression Operators --- generator/config/expression/abs.yaml | 12 + generator/config/expression/ceil.yaml | 10 + generator/config/expression/divide.yaml | 12 + generator/config/expression/exp.yaml | 12 + generator/config/expression/floor.yaml | 10 + generator/config/expression/ln.yaml | 10 + generator/config/expression/log.yaml | 15 + generator/config/expression/log10.yaml | 12 + generator/config/expression/mod.yaml | 11 + generator/config/expression/multiply.yaml | 13 + generator/config/expression/pow.yaml | 14 + generator/config/expression/round.yaml | 11 + generator/config/expression/sqrt.yaml | 24 ++ generator/config/expression/trunc.yaml | 11 + tests/Builder/Expression/AbsOperatorTest.php | 32 ++ tests/Builder/Expression/CeilOperatorTest.php | 30 ++ .../Builder/Expression/DivideOperatorTest.php | 31 ++ tests/Builder/Expression/ExpOperatorTest.php | 32 ++ .../Builder/Expression/FloorOperatorTest.php | 30 ++ tests/Builder/Expression/LnOperatorTest.php | 30 ++ .../Builder/Expression/Log10OperatorTest.php | 32 ++ tests/Builder/Expression/LogOperatorTest.php | 35 ++ tests/Builder/Expression/ModOperatorTest.php | 30 ++ .../Expression/MultiplyOperatorTest.php | 32 ++ tests/Builder/Expression/Pipelines.php | 346 ++++++++++++++++++ tests/Builder/Expression/PowOperatorTest.php | 32 ++ .../Builder/Expression/RoundOperatorTest.php | 30 ++ tests/Builder/Expression/SqrtOperatorTest.php | 44 +++ .../Builder/Expression/TruncOperatorTest.php | 30 ++ 29 files changed, 973 insertions(+) create mode 100644 tests/Builder/Expression/AbsOperatorTest.php create mode 100644 tests/Builder/Expression/CeilOperatorTest.php create mode 100644 tests/Builder/Expression/DivideOperatorTest.php create mode 100644 tests/Builder/Expression/ExpOperatorTest.php create mode 100644 tests/Builder/Expression/FloorOperatorTest.php create mode 100644 tests/Builder/Expression/LnOperatorTest.php create mode 100644 tests/Builder/Expression/Log10OperatorTest.php create mode 100644 tests/Builder/Expression/LogOperatorTest.php create mode 100644 tests/Builder/Expression/ModOperatorTest.php create mode 100644 tests/Builder/Expression/MultiplyOperatorTest.php create mode 100644 tests/Builder/Expression/PowOperatorTest.php create mode 100644 tests/Builder/Expression/RoundOperatorTest.php create mode 100644 tests/Builder/Expression/SqrtOperatorTest.php create mode 100644 tests/Builder/Expression/TruncOperatorTest.php diff --git a/generator/config/expression/abs.yaml b/generator/config/expression/abs.yaml index 029b33d..fe29e44 100644 --- a/generator/config/expression/abs.yaml +++ b/generator/config/expression/abs.yaml @@ -11,3 +11,15 @@ arguments: name: value type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/abs/#example' + pipeline: + - + $project: + delta: + $abs: + $subtract: + - '$startTemp' + - '$endTemp' diff --git a/generator/config/expression/ceil.yaml b/generator/config/expression/ceil.yaml index b335677..73c31dd 100644 --- a/generator/config/expression/ceil.yaml +++ b/generator/config/expression/ceil.yaml @@ -13,3 +13,13 @@ arguments: - resolvesToNumber description: | If the argument resolves to a value of null or refers to a field that is missing, $ceil returns null. If the argument resolves to NaN, $ceil returns NaN. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ceil/#example' + pipeline: + - + $project: + value: 1 + ceilingValue: + $ceil: '$value' diff --git a/generator/config/expression/divide.yaml b/generator/config/expression/divide.yaml index 66b4eb0..3b69389 100644 --- a/generator/config/expression/divide.yaml +++ b/generator/config/expression/divide.yaml @@ -17,3 +17,15 @@ arguments: name: divisor type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/divide/#example' + pipeline: + - + $project: + city: 1 + workdays: + $divide: + - '$hours' + - 8 diff --git a/generator/config/expression/exp.yaml b/generator/config/expression/exp.yaml index 9df1cc3..d1f6982 100644 --- a/generator/config/expression/exp.yaml +++ b/generator/config/expression/exp.yaml @@ -11,3 +11,15 @@ arguments: name: exponent type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/exp/#example' + pipeline: + - + $project: + effectiveRate: + $subtract: + - + $exp: '$interestRate' + - 1 diff --git a/generator/config/expression/floor.yaml b/generator/config/expression/floor.yaml index f497e26..4c58562 100644 --- a/generator/config/expression/floor.yaml +++ b/generator/config/expression/floor.yaml @@ -11,3 +11,13 @@ arguments: name: expression type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/floor/#example' + pipeline: + - + $project: + value: 1 + floorValue: + $floor: '$value' diff --git a/generator/config/expression/ln.yaml b/generator/config/expression/ln.yaml index f4660df..f7412ae 100644 --- a/generator/config/expression/ln.yaml +++ b/generator/config/expression/ln.yaml @@ -14,3 +14,13 @@ arguments: - resolvesToNumber description: | Any valid expression as long as it resolves to a non-negative number. For more information on expressions, see Expressions. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ln/#example' + pipeline: + - + $project: + x: '$year' + y: + $ln: '$sales' diff --git a/generator/config/expression/log.yaml b/generator/config/expression/log.yaml index becc324..462eb94 100644 --- a/generator/config/expression/log.yaml +++ b/generator/config/expression/log.yaml @@ -19,3 +19,18 @@ arguments: - resolvesToNumber description: | Any valid expression as long as it resolves to a positive number greater than 1. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log/#example' + pipeline: + - + $project: + bitsNeeded: + $floor: + $add: + - 1 + - + $log: + - '$int' + - 2 diff --git a/generator/config/expression/log10.yaml b/generator/config/expression/log10.yaml index 2ca26e5..77cab07 100644 --- a/generator/config/expression/log10.yaml +++ b/generator/config/expression/log10.yaml @@ -13,3 +13,15 @@ arguments: - resolvesToNumber description: | Any valid expression as long as it resolves to a non-negative number. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/#example' + pipeline: + - + $project: + pH: + $multiply: + - -1 + - + $log10: '$H3O' diff --git a/generator/config/expression/mod.yaml b/generator/config/expression/mod.yaml index 24bfa56..0ac48bd 100644 --- a/generator/config/expression/mod.yaml +++ b/generator/config/expression/mod.yaml @@ -17,3 +17,14 @@ arguments: name: divisor type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mod/#example' + pipeline: + - + $project: + remainder: + $mod: + - '$hours' + - '$tasks' diff --git a/generator/config/expression/multiply.yaml b/generator/config/expression/multiply.yaml index 855616e..5a069cc 100644 --- a/generator/config/expression/multiply.yaml +++ b/generator/config/expression/multiply.yaml @@ -15,3 +15,16 @@ arguments: description: | The arguments can be any valid expression as long as they resolve to numbers. Starting in MongoDB 6.1 you can optimize the $multiply operation. To improve performance, group references at the end of the argument list. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply/#example' + pipeline: + - + $project: + date: 1 + item: 1 + total: + $multiply: + - '$price' + - '$quantity' diff --git a/generator/config/expression/pow.yaml b/generator/config/expression/pow.yaml index 4d9d2b4..71d3be2 100644 --- a/generator/config/expression/pow.yaml +++ b/generator/config/expression/pow.yaml @@ -15,3 +15,17 @@ arguments: name: exponent type: - resolvesToNumber +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/pow/#example' + pipeline: + - + $project: + variance: + $pow: + - + # The builder renders $stdDevPop with the array form, even with a single value + # $stdDevPop: '$scores.score' + $stdDevPop: ['$scores.score'] + - 2 diff --git a/generator/config/expression/round.yaml b/generator/config/expression/round.yaml index 8f0fae2..52e6004 100644 --- a/generator/config/expression/round.yaml +++ b/generator/config/expression/round.yaml @@ -27,3 +27,14 @@ arguments: optional: true description: | Can be any valid expression that resolves to an integer between -20 and 100, exclusive. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/round/#example' + pipeline: + - + $project: + roundedValue: + $round: + - '$value' + - 1 diff --git a/generator/config/expression/sqrt.yaml b/generator/config/expression/sqrt.yaml index 7b7fb06..52f5bb7 100644 --- a/generator/config/expression/sqrt.yaml +++ b/generator/config/expression/sqrt.yaml @@ -13,3 +13,27 @@ arguments: - resolvesToNumber description: | The argument can be any valid expression as long as it resolves to a non-negative number. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sqrt/#example' + pipeline: + - + $project: + distance: + $sqrt: + $add: + - + $pow: + - + $subtract: + - '$p2.y' + - '$p1.y' + - 2 + - + $pow: + - + $subtract: + - '$p2.x' + - '$p1.x' + - 2 diff --git a/generator/config/expression/trunc.yaml b/generator/config/expression/trunc.yaml index 3d58713..f930cf0 100644 --- a/generator/config/expression/trunc.yaml +++ b/generator/config/expression/trunc.yaml @@ -21,3 +21,14 @@ arguments: optional: true description: | Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. Defaults to 0. +tests: + - + name: 'Example' + link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/#example' + pipeline: + - + $project: + truncatedValue: + $trunc: + - '$value' + - 1 diff --git a/tests/Builder/Expression/AbsOperatorTest.php b/tests/Builder/Expression/AbsOperatorTest.php new file mode 100644 index 0000000..829c9c5 --- /dev/null +++ b/tests/Builder/Expression/AbsOperatorTest.php @@ -0,0 +1,32 @@ +assertSamePipeline(Pipelines::AbsExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/CeilOperatorTest.php b/tests/Builder/Expression/CeilOperatorTest.php new file mode 100644 index 0000000..6d78af6 --- /dev/null +++ b/tests/Builder/Expression/CeilOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::CeilExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/DivideOperatorTest.php b/tests/Builder/Expression/DivideOperatorTest.php new file mode 100644 index 0000000..dfccd98 --- /dev/null +++ b/tests/Builder/Expression/DivideOperatorTest.php @@ -0,0 +1,31 @@ +assertSamePipeline(Pipelines::DivideExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/ExpOperatorTest.php b/tests/Builder/Expression/ExpOperatorTest.php new file mode 100644 index 0000000..daa5825 --- /dev/null +++ b/tests/Builder/Expression/ExpOperatorTest.php @@ -0,0 +1,32 @@ +assertSamePipeline(Pipelines::ExpExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/FloorOperatorTest.php b/tests/Builder/Expression/FloorOperatorTest.php new file mode 100644 index 0000000..8aa3c83 --- /dev/null +++ b/tests/Builder/Expression/FloorOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::FloorExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/LnOperatorTest.php b/tests/Builder/Expression/LnOperatorTest.php new file mode 100644 index 0000000..cbbb85c --- /dev/null +++ b/tests/Builder/Expression/LnOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::LnExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/Log10OperatorTest.php b/tests/Builder/Expression/Log10OperatorTest.php new file mode 100644 index 0000000..eb7e6b6 --- /dev/null +++ b/tests/Builder/Expression/Log10OperatorTest.php @@ -0,0 +1,32 @@ +assertSamePipeline(Pipelines::Log10Example, $pipeline); + } +} diff --git a/tests/Builder/Expression/LogOperatorTest.php b/tests/Builder/Expression/LogOperatorTest.php new file mode 100644 index 0000000..f1215d5 --- /dev/null +++ b/tests/Builder/Expression/LogOperatorTest.php @@ -0,0 +1,35 @@ +assertSamePipeline(Pipelines::LogExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/ModOperatorTest.php b/tests/Builder/Expression/ModOperatorTest.php new file mode 100644 index 0000000..5951779 --- /dev/null +++ b/tests/Builder/Expression/ModOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::ModExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/MultiplyOperatorTest.php b/tests/Builder/Expression/MultiplyOperatorTest.php new file mode 100644 index 0000000..546c418 --- /dev/null +++ b/tests/Builder/Expression/MultiplyOperatorTest.php @@ -0,0 +1,32 @@ +assertSamePipeline(Pipelines::MultiplyExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/Pipelines.php b/tests/Builder/Expression/Pipelines.php index d5b78ce..868978d 100644 --- a/tests/Builder/Expression/Pipelines.php +++ b/tests/Builder/Expression/Pipelines.php @@ -10,6 +10,28 @@ enum Pipelines: string { + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/abs/#example + */ + case AbsExample = <<<'JSON' + [ + { + "$project": { + "delta": { + "$abs": { + "$subtract": [ + "$startTemp", + "$endTemp" + ] + } + } + } + } + ] + JSON; + /** * Example * @@ -638,6 +660,26 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ceil/#example + */ + case CeilExample = <<<'JSON' + [ + { + "$project": { + "value": { + "$numberInt": "1" + }, + "ceilingValue": { + "$ceil": "$value" + } + } + } + ] + JSON; + /** * Example * @@ -1710,6 +1752,31 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/divide/#example + */ + case DivideExample = <<<'JSON' + [ + { + "$project": { + "city": { + "$numberInt": "1" + }, + "workdays": { + "$divide": [ + "$hours", + { + "$numberInt": "8" + } + ] + } + } + } + ] + JSON; + /** * Example * @@ -1741,6 +1808,30 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/exp/#example + */ + case ExpExample = <<<'JSON' + [ + { + "$project": { + "effectiveRate": { + "$subtract": [ + { + "$exp": "$interestRate" + }, + { + "$numberInt": "1" + } + ] + } + } + } + ] + JSON; + /** * Example * @@ -1939,6 +2030,26 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/floor/#example + */ + case FloorExample = <<<'JSON' + [ + { + "$project": { + "value": { + "$numberInt": "1" + }, + "floorValue": { + "$floor": "$value" + } + } + } + ] + JSON; + /** * Usage Example * @@ -2630,6 +2741,79 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ln/#example + */ + case LnExample = <<<'JSON' + [ + { + "$project": { + "x": "$year", + "y": { + "$ln": "$sales" + } + } + } + ] + JSON; + + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/log/#example + */ + case LogExample = <<<'JSON' + [ + { + "$project": { + "bitsNeeded": { + "$floor": { + "$add": [ + { + "$numberInt": "1" + }, + { + "$log": [ + "$int", + { + "$numberInt": "2" + } + ] + } + ] + } + } + } + } + ] + JSON; + + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/#example + */ + case Log10Example = <<<'JSON' + [ + { + "$project": { + "pH": { + "$multiply": [ + { + "$numberInt": "-1" + }, + { + "$log10": "$H3O" + } + ] + } + } + } + ] + JSON; + /** * Example * @@ -3017,6 +3201,26 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/mod/#example + */ + case ModExample = <<<'JSON' + [ + { + "$project": { + "remainder": { + "$mod": [ + "$hours", + "$tasks" + ] + } + } + } + ] + JSON; + /** * Example * @@ -3036,6 +3240,32 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply/#example + */ + case MultiplyExample = <<<'JSON' + [ + { + "$project": { + "date": { + "$numberInt": "1" + }, + "item": { + "$numberInt": "1" + }, + "total": { + "$multiply": [ + "$price", + "$quantity" + ] + } + } + } + ] + JSON; + /** * Example * @@ -3221,6 +3451,32 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/pow/#example + */ + case PowExample = <<<'JSON' + [ + { + "$project": { + "variance": { + "$pow": [ + { + "$stdDevPop": [ + "$scores.score" + ] + }, + { + "$numberInt": "2" + } + ] + } + } + } + ] + JSON; + /** * Example * @@ -3937,6 +4193,28 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/round/#example + */ + case RoundExample = <<<'JSON' + [ + { + "$project": { + "roundedValue": { + "$round": [ + "$value", + { + "$numberInt": "1" + } + ] + } + } + } + ] + JSON; + /** * Example * @@ -4643,6 +4921,52 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sqrt/#example + */ + case SqrtExample = <<<'JSON' + [ + { + "$project": { + "distance": { + "$sqrt": { + "$add": [ + { + "$pow": [ + { + "$subtract": [ + "$p2.y", + "$p1.y" + ] + }, + { + "$numberInt": "2" + } + ] + }, + { + "$pow": [ + { + "$subtract": [ + "$p2.x", + "$p1.x" + ] + }, + { + "$numberInt": "2" + } + ] + } + ] + } + } + } + } + ] + JSON; + /** * Use in $project Stage * @@ -5435,6 +5759,28 @@ enum Pipelines: string ] JSON; + /** + * Example + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/#example + */ + case TruncExample = <<<'JSON' + [ + { + "$project": { + "truncatedValue": { + "$trunc": [ + "$value", + { + "$numberInt": "1" + } + ] + } + } + } + ] + JSON; + /** * Obtain the Incrementing Ordinal from a Timestamp Field * diff --git a/tests/Builder/Expression/PowOperatorTest.php b/tests/Builder/Expression/PowOperatorTest.php new file mode 100644 index 0000000..4af8815 --- /dev/null +++ b/tests/Builder/Expression/PowOperatorTest.php @@ -0,0 +1,32 @@ +assertSamePipeline(Pipelines::PowExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/RoundOperatorTest.php b/tests/Builder/Expression/RoundOperatorTest.php new file mode 100644 index 0000000..accae50 --- /dev/null +++ b/tests/Builder/Expression/RoundOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::RoundExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/SqrtOperatorTest.php b/tests/Builder/Expression/SqrtOperatorTest.php new file mode 100644 index 0000000..de33969 --- /dev/null +++ b/tests/Builder/Expression/SqrtOperatorTest.php @@ -0,0 +1,44 @@ +assertSamePipeline(Pipelines::SqrtExample, $pipeline); + } +} diff --git a/tests/Builder/Expression/TruncOperatorTest.php b/tests/Builder/Expression/TruncOperatorTest.php new file mode 100644 index 0000000..45f1826 --- /dev/null +++ b/tests/Builder/Expression/TruncOperatorTest.php @@ -0,0 +1,30 @@ +assertSamePipeline(Pipelines::TruncExample, $pipeline); + } +}