Skip to content

draft: augment yaml definitions with test schema #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: v2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions generator/config/accumulator/accumulator.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $schema: ../schema.json
name: $accumulator
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep quotes around strings? This style changes make the diff hard to read. Also, I use quotes by default around every string to prevent unexpected parsing rule from Yaml.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, looks like the quotes usage is inconsistent in these files - I'd be inclined to define a prettier rule and reformat them in a separate PR to make this one less noisy if that'd make sense to you. The reason for these getting removed is that the way we're adding the schema information is parsing the yaml, fetching the schema from the docs, then writing it, which results in the file getting reformatted based on the tool conventions (we use https://www.npmjs.com/package/js-yaml here).

link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/
type:
- accumulator
encode: object
Expand Down Expand Up @@ -52,15 +52,14 @@ arguments:
- string
description: |
The language used in the $accumulator code.

tests:
-
name: 'Use $accumulator to Implement the $avg Operator'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use--accumulator-to-implement-the--avg-operator'
name: Use $accumulator to Implement the $avg Operator
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use--accumulator-to-implement-the--avg-operator
pipeline:
-
$group:
_id: '$author'
_id: $author
avgCopies:
$accumulator:
init:
Expand All @@ -73,7 +72,8 @@ tests:
function(state, numCopies) {
return { count: state.count + 1, sum: state.sum + numCopies }
}
accumulateArgs: [ "$copies" ]
accumulateArgs:
- $copies
merge:
$code: |-
function(state1, state2) {
Expand All @@ -87,16 +87,33 @@ tests:
function(state) {
return (state.sum / state.count)
}
lang: 'js'

lang: js
schema:
books:
_id:
types:
-
bsonType: Number
title:
types:
-
bsonType: String
author:
types:
-
bsonType: String
copies:
types:
-
bsonType: Number
-
name: 'Use initArgs to Vary the Initial State by Group'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use-initargs-to-vary-the-initial-state-by-group'
name: Use initArgs to Vary the Initial State by Group
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use-initargs-to-vary-the-initial-state-by-group
pipeline:
-
$group:
_id:
city: '$city'
city: $city
restaurants:
$accumulator:
init:
Expand All @@ -105,8 +122,8 @@ tests:
return { max: city === userProfileCity ? 3 : 1, restaurants: [] }
}
initArgs:
- '$city'
- 'Bettles'
- $city
- Bettles
accumulate:
$code: |-
function(state, restaurantName) {
Expand All @@ -115,7 +132,8 @@ tests:
}
return state;
}
accumulateArgs: ['$name']
accumulateArgs:
- $name
merge:
$code: |-
function(state1, state2) {
Expand All @@ -129,4 +147,22 @@ tests:
function(state) {
return state.restaurants
}
lang: 'js'
lang: js
schema:
restaurants:
_id:
types:
-
bsonType: Number
name:
types:
-
bsonType: String
city:
types:
-
bsonType: String
cuisine:
types:
-
bsonType: String
86 changes: 67 additions & 19 deletions generator/config/accumulator/addToSet.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $schema: ../schema.json
name: $addToSet
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/'
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/
type:
- accumulator
- window
Expand All @@ -13,35 +13,83 @@ arguments:
name: expression
type:
- expression

tests:
-
name: 'Use in $group Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/#use-in--group-stage'
name: Use in $group Stage
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/#use-in--group-stage
pipeline:
- $group:
-
$group:
_id:
day:
$dayOfYear:
date: $date
year:
$year:
date: $date
itemsSold:
$addToSet: $item
schema:
sales:
_id:
day:
$dayOfYear:
date: '$date'
year:
$year:
date: '$date'
itemsSold:
$addToSet: '$item'
types:
-
bsonType: Number
item:
types:
-
bsonType: String
price:
types:
-
bsonType: Number
quantity:
types:
-
bsonType: Number
date:
types:
-
bsonType: Date
-
name: 'Use in $setWindowFields Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/#use-in--setwindowfields-stage'
name: Use in $setWindowFields Stage
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/#use-in--setwindowfields-stage
pipeline:
-
$setWindowFields:
partitionBy: '$state'
partitionBy: $state
sortBy:
orderDate: 1
output:
cakeTypesForState:
$addToSet: '$type'
$addToSet: $type
window:
documents:
- 'unbounded'
- 'current'
- unbounded
- current
schema:
cakeSales:
_id:
types:
-
bsonType: Number
type:
types:
-
bsonType: String
orderDate:
types:
-
bsonType: Date
state:
types:
-
bsonType: String
price:
types:
-
bsonType: Number
quantity:
types:
-
bsonType: Number
77 changes: 63 additions & 14 deletions generator/config/accumulator/avg.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $schema: ../schema.json
name: $avg
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/'
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/
type:
- accumulator
- window
Expand All @@ -15,31 +15,80 @@ arguments:
- resolvesToNumber
tests:
-
name: 'Use in $group Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--group-stage'
name: Use in $group Stage
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--group-stage
pipeline:
- $group:
_id: '$item'
-
$group:
_id: $item
avgAmount:
$avg:
$multiply:
- '$price'
- '$quantity'
- $price
- $quantity
avgQuantity:
$avg: '$quantity'
$avg: $quantity
schema:
sales:
_id:
types:
-
bsonType: Number
item:
types:
-
bsonType: String
price:
types:
-
bsonType: Number
quantity:
types:
-
bsonType: Number
date:
types:
-
bsonType: Date
-
name: 'Use in $setWindowFields Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--setwindowfields-stage'
name: Use in $setWindowFields Stage
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--setwindowfields-stage
pipeline:
-
$setWindowFields:
partitionBy: '$state'
partitionBy: $state
sortBy:
orderDate: 1
output:
averageQuantityForState:
$avg: '$quantity'
$avg: $quantity
window:
documents:
- 'unbounded'
- 'current'
- unbounded
- current
schema:
cakeSales:
_id:
types:
-
bsonType: Number
type:
types:
-
bsonType: String
orderDate:
types:
-
bsonType: Date
state:
types:
-
bsonType: String
price:
types:
-
bsonType: Number
quantity:
types:
-
bsonType: Number
Loading
Loading