Skip to content

Commit e13a2cb

Browse files
authored
Merge pull request #49 from dacharyc/mongosh-test-suite-poc
Mongosh test suite PoC
2 parents 010451c + a1d9396 commit e13a2cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5203
-47
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
# IDE files
1313
## Visual Studio Code files
1414
.vscode
15+
1516
## JetBrains IDE files
1617
*.idea
18+
1719
## Python virtual environment
1820
*/venv
1921

2022
# Language specific output
2123
## Node.js dependencies
22-
*/node_modules
24+
node_modules
25+
2326
## Java build files
2427
*/target
2528

29+
# Mongosh temporary files
30+
temp

generated-usage-examples/javascript/create-index-basic.snippet.example.js renamed to generated-usage-examples/javascript/driver/create-index-basic.snippet.example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MongoClient } from 'mongodb';
22

33
export async function createIndexBasic() {
4-
// connect to your Atlas deployment
4+
// connect to the Atlas deployment
55
const uri = "<connection-string>";
66
const client = new MongoClient(uri);
77
try {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#! /bin/bash
22

33
PROJECT=$(git rev-parse --show-toplevel)
4-
JS_EXAMPLES=$PROJECT/javascript/examples
5-
GENERATED_EXAMPLES=$PROJECT/generated-examples/javascript
4+
JS_EXAMPLES=$PROJECT/usage-examples/javascript/driver/examples
5+
GENERATED_EXAMPLES=$PROJECT/generated-usage-examples/javascript/driver
66

77
echo "Bluehawking JavaScript examples"
88
npx bluehawk snip $JS_EXAMPLES -o $GENERATED_EXAMPLES

usage-examples/javascript/package-lock.json renamed to usage-examples/javascript/driver/package-lock.json

Lines changed: 40 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# mongosh Code Example Test PoC
2+
3+
This is a PoC to explore testing mongosh code examples for MongoDB documentation.
4+
5+
The structure of this JavaScript project is as follows:
6+
7+
- `/examples`: This directory contains example code and output to validate.
8+
- `/tests`: This directory contains the test infrastructure to actually run
9+
the tests by invoking the example code.
10+
11+
While running the tests, this test suite creates files in a `/temp` directory. We concatenate
12+
the code example with the necessary commands to connect to the deployment and use the correct
13+
database.
14+
15+
## To run the tests locally
16+
17+
### Create a MongoDB Docker Deployment
18+
19+
To run these tests locally, you need a MongoDB Docker deployment. Make sure Docker is
20+
running, and then:
21+
22+
1. Pull the MongoDB image from Docker Hub:
23+
24+
```shell
25+
docker pull mongo
26+
```
27+
2. Run the container:
28+
29+
```shell
30+
docker run --name mongodb-test -d -p 27017:27017 mongo
31+
```
32+
33+
3. Verify the container is running:
34+
35+
```shell
36+
docker ps
37+
```
38+
39+
The output resembles:
40+
41+
```text
42+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43+
ef70cce38f26 mongo "/usr/local/bin/runn…" 29 hours ago Up 29 hours (healthy) 127.0.0.1:63201->27017/tcp mongodb-test
44+
```
45+
46+
You may note the actual port is different than `27017`, if something else is already running on
47+
`27017` on your machine. Note the port next to the IP address for running the tests. Alternately, you can get just
48+
the port info for your container using the following command:
49+
50+
```shell
51+
docker port mongodb-test
52+
```
53+
54+
The output resembles:
55+
56+
```text
57+
27017/tcp -> 0.0.0.0:27017
58+
27017/tcp -> [::]:27017
59+
```
60+
61+
### Create a .env file
62+
63+
Create a file named `.env` at the root of the `/mongosh` directory.
64+
Add the following values to your .env file, substituting the port where your local deployment
65+
is running:
66+
67+
```
68+
CONNECTION_STRING="mongodb://localhost:63201"
69+
CONNECTION_PORT="63201"
70+
```
71+
72+
### Install the dependencies
73+
74+
This test suite requires you to have `Node.js` v20 or newer installed. If you
75+
do not yet have Node installed, refer to
76+
[the Node.js installation page](https://nodejs.org/en/download/package-manager)
77+
for details.
78+
79+
From the root of the `/mongosh` directory, run the following command to install
80+
dependencies:
81+
82+
```
83+
npm install
84+
```
85+
86+
### Run the tests
87+
88+
You can run tests from the command line or through your IDE.
89+
90+
#### Run All Tests from the command line
91+
92+
From the `/mongosh` directory, run:
93+
94+
```
95+
npm test
96+
```
97+
98+
This invokes the following command from the `package.json` `test` key:
99+
100+
```
101+
jest --runInBand --detectOpenHandles
102+
```
103+
104+
In the above command:
105+
106+
- `jest` is the command to run the test suite
107+
- `--runInBand` is a flag that specifies only running one test at a time
108+
to avoid collisions when creating/editing/dropping indexes. Otherwise, Jest
109+
defaults to running tests in parallel.
110+
- `--detectOpenHandles` is a flag that tells Jest to track resource handles or async
111+
operations that remain open after the tests are complete. These can cause the test suite
112+
to hang, and this flag tells Jest to report info about these instances.
113+
114+
#### Run Test Suites from the command line
115+
116+
You can run all the tests in a given test suite (file).
117+
118+
From the `/mongosh` directory, run:
119+
120+
```
121+
npm test -- -t '<text string from the 'describe()' block you want to run>'
122+
```
123+
124+
#### Run Individual Tests from the command line
125+
126+
You can run a single test within a given test suite (file).
127+
128+
From the `/mongosh` directory, run:
129+
130+
```
131+
npm test -- -t '<text string from the 'it()' block you want to run>'
132+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"_id": "2014-04-04",
4+
"totalSaleAmount": Decimal128("200"),
5+
"averageQuantity": 15,
6+
"count": 2
7+
},
8+
{
9+
"_id": "2014-03-15",
10+
"totalSaleAmount": Decimal128("50"),
11+
"averageQuantity": 10,
12+
"count": 1
13+
},
14+
{
15+
"_id": "2014-03-01",
16+
"totalSaleAmount": Decimal128("40"),
17+
"averageQuantity": 1.5,
18+
"count": 2
19+
}
20+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
db.sales.aggregate([
2+
// First Stage
3+
{
4+
$match : { "date": { $gte: new ISODate("2014-01-01"), $lt: new ISODate("2015-01-01") } }
5+
},
6+
// Second Stage
7+
{
8+
$group : {
9+
_id : { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
10+
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
11+
averageQuantity: { $avg: "$quantity" },
12+
count: { $sum: 1 }
13+
}
14+
},
15+
// Third Stage
16+
{
17+
$sort : { totalSaleAmount: -1 }
18+
}
19+
])
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
_id: null,
4+
totalSaleAmount: Decimal128('452.5'),
5+
averageQuantity: 7.875,
6+
count: 8
7+
}
8+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
db.sales.aggregate([
2+
{
3+
$group : {
4+
_id : null,
5+
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
6+
averageQuantity: { $avg: "$quantity" },
7+
count: { $sum: 1 }
8+
}
9+
}
10+
])

0 commit comments

Comments
 (0)