-
Notifications
You must be signed in to change notification settings - Fork 5
Mongosh test suite PoC #49
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ab5c839
Move JS Driver examples to a Driver directory in JS
dacharyc a8e1c96
Add mongosh test suite PoC
dacharyc 73394ff
Apply suggestions from code review
dacharyc a1d9396
Apply suggestions from review, remove protected character from descri…
dacharyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ipt/create-index-basic.snippet.example.js → ...ver/create-index-basic.snippet.example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
usage-examples/javascript/bluehawk.sh → usage-examples/javascript/driver/bluehawk.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
#! /bin/bash | ||
|
||
PROJECT=$(git rev-parse --show-toplevel) | ||
JS_EXAMPLES=$PROJECT/javascript/examples | ||
GENERATED_EXAMPLES=$PROJECT/generated-examples/javascript | ||
JS_EXAMPLES=$PROJECT/usage-examples/javascript/driver/examples | ||
GENERATED_EXAMPLES=$PROJECT/generated-usage-examples/javascript/driver | ||
|
||
echo "Bluehawking JavaScript examples" | ||
npx bluehawk snip $JS_EXAMPLES -o $GENERATED_EXAMPLES |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
83 changes: 40 additions & 43 deletions
83
usage-examples/javascript/package-lock.json → ...mples/javascript/driver/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# mongosh Code Example Test PoC | ||
|
||
This is a PoC to explore testing mongosh code examples for MongoDB documentation. | ||
|
||
The structure of this JavaScript project is as follows: | ||
|
||
- `/examples`: This directory contains example code and output to validate. | ||
- `/tests`: This directory contains the test infrastructure to actually run | ||
the tests by invoking the example code. | ||
|
||
While running the tests, this test suite creates files in a `/temp` directory. We concatenate | ||
the code example with the necessary commands to connect to the deployment and use the correct | ||
database. | ||
|
||
## To run the tests locally | ||
|
||
### Create a MongoDB Docker Deployment | ||
|
||
To run these tests locally, you need a MongoDB Docker deployment. Make sure Docker is | ||
running, and then: | ||
|
||
1. Pull the MongoDB image from Docker Hub: | ||
|
||
```shell | ||
docker pull mongo | ||
``` | ||
2. Run the container: | ||
|
||
```shell | ||
docker run --name mongodb-test -d -p 27017:27017 mongo | ||
``` | ||
|
||
3. Verify the container is running: | ||
|
||
```shell | ||
docker ps | ||
``` | ||
|
||
The output resembles: | ||
|
||
```text | ||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | ||
ef70cce38f26 mongo "/usr/local/bin/runn…" 29 hours ago Up 29 hours (healthy) 127.0.0.1:63201->27017/tcp mongodb-test | ||
``` | ||
|
||
You may note the actual port is different than `27017`, if something else is already running on | ||
`27017` on your machine. Note the port next to the IP address for running the tests. Alternately, you can get just | ||
the port info for your container using the following command: | ||
|
||
```shell | ||
docker port mongodb-test | ||
``` | ||
|
||
The output resembles: | ||
|
||
```text | ||
27017/tcp -> 0.0.0.0:27017 | ||
27017/tcp -> [::]:27017 | ||
``` | ||
|
||
### Create a .env file | ||
|
||
Create a file named `.env` at the root of the `/mongosh` directory. | ||
Add the following values to your .env file, substituting the port where your local deployment | ||
is running: | ||
|
||
``` | ||
CONNECTION_STRING="mongodb://localhost:63201" | ||
CONNECTION_PORT="63201" | ||
``` | ||
|
||
### Install the dependencies | ||
|
||
This test suite requires you to have `Node.js` v20 or newer installed. If you | ||
do not yet have Node installed, refer to | ||
[the Node.js installation page](https://nodejs.org/en/download/package-manager) | ||
for details. | ||
|
||
From the root of the `/mongosh` directory, run the following command to install | ||
dependencies: | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
### Run the tests | ||
|
||
You can run tests from the command line or through your IDE. | ||
|
||
#### Run All Tests from the command line | ||
|
||
From the `/mongosh` directory, run: | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
This invokes the following command from the `package.json` `test` key: | ||
|
||
``` | ||
jest --runInBand --detectOpenHandles | ||
``` | ||
|
||
In the above command: | ||
|
||
- `jest` is the command to run the test suite | ||
- `--runInBand` is a flag that specifies only running one test at a time | ||
to avoid collisions when creating/editing/dropping indexes. Otherwise, Jest | ||
defaults to running tests in parallel. | ||
- `--detectOpenHandles` is a flag that tells Jest to track resource handles or async | ||
operations that remain open after the tests are complete. These can cause the test suite | ||
to hang, and this flag tells Jest to report info about these instances. | ||
|
||
#### Run Test Suites from the command line | ||
|
||
You can run all the tests in a given test suite (file). | ||
|
||
From the `/mongosh` directory, run: | ||
|
||
``` | ||
npm test -- -t '<text string from the 'describe()' block you want to run>' | ||
``` | ||
|
||
#### Run Individual Tests from the command line | ||
|
||
You can run a single test within a given test suite (file). | ||
|
||
From the `/mongosh` directory, run: | ||
|
||
``` | ||
npm test -- -t '<text string from the 'it()' block you want to run>' | ||
``` |
20 changes: 20 additions & 0 deletions
20
usage-examples/javascript/mongosh/examples/aggregation/operators/group-by-day-output.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"_id": "2014-04-04", | ||
"totalSaleAmount": Decimal128("200"), | ||
"averageQuantity": 15, | ||
"count": 2 | ||
}, | ||
{ | ||
"_id": "2014-03-15", | ||
"totalSaleAmount": Decimal128("50"), | ||
"averageQuantity": 10, | ||
"count": 1 | ||
}, | ||
{ | ||
"_id": "2014-03-01", | ||
"totalSaleAmount": Decimal128("40"), | ||
"averageQuantity": 1.5, | ||
"count": 2 | ||
} | ||
] |
19 changes: 19 additions & 0 deletions
19
usage-examples/javascript/mongosh/examples/aggregation/operators/group-by-day.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
db.sales.aggregate([ | ||
// First Stage | ||
{ | ||
$match : { "date": { $gte: new ISODate("2014-01-01"), $lt: new ISODate("2015-01-01") } } | ||
}, | ||
// Second Stage | ||
{ | ||
$group : { | ||
_id : { $dateToString: { format: "%Y-%m-%d", date: "$date" } }, | ||
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } }, | ||
averageQuantity: { $avg: "$quantity" }, | ||
count: { $sum: 1 } | ||
} | ||
}, | ||
// Third Stage | ||
{ | ||
$sort : { totalSaleAmount: -1 } | ||
} | ||
]) |
8 changes: 8 additions & 0 deletions
8
usage-examples/javascript/mongosh/examples/aggregation/operators/group-by-null-output.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
_id: null, | ||
totalSaleAmount: Decimal128('452.5'), | ||
averageQuantity: 7.875, | ||
count: 8 | ||
} | ||
] |
10 changes: 10 additions & 0 deletions
10
usage-examples/javascript/mongosh/examples/aggregation/operators/group-by-null.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
db.sales.aggregate([ | ||
{ | ||
$group : { | ||
_id : null, | ||
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } }, | ||
averageQuantity: { $avg: "$quantity" }, | ||
count: { $sum: 1 } | ||
} | ||
} | ||
]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why aren't we using npm scripts here? would let us simplify these last sections to running the following from
/mongosh
root without requiring jest installed:or
...if we do keep them as a jest commands, we should:
npm install -g jest
)export
since it's not needed -- the setup already handles itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I did not know this was an option 😅 Updated!