Skip to content

Commit cabca31

Browse files
author
Isabella Siu
committed
Update Makefile to run examples and fix bugs in examples
GODRIVER-787 GODRIVER-768 Change-Id: Ie8bd245045eda0ed7bc7baadeef533f6aceacc3b
1 parent 5ea86b8 commit cabca31

File tree

3 files changed

+57
-86
lines changed

3 files changed

+57
-86
lines changed

.lint-whitelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ bson/internal/jsonparser/parser.go:1137:9: if block ends with a return statement
6161
bson/internal/jsonparser/parser.go:1146:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
6262
bson/internal/jsonparser/parser_test.go:1361:5: var testJson should be testJSON
6363
bson/internal/jsonpretty/pretty.go:7:1: comment on exported type Options should be of the form "Options ..." (with optional leading article)
64+
examples/documentation_examples/examples.go:10:1: don't use an underscore in package name

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ UNSTABLE_PKGS = $(shell etc/list_pkgs.sh ./x)
66
UNSTABLE_TEST_PKGS = $(shell etc/list_test_pkgs.sh ./x)
77
TAG_PKG = $(shell etc/list_pkgs.sh ./tag)
88
TAG_TEST_PKG = $(shell etc/list_test_pkgs.sh ./tag)
9-
PKGS = $(BSON_PKGS) $(MONGO_PKGS) $(UNSTABLE_PKGS) $(TAG_PKG)
10-
TEST_PKGS = $(BSON_TEST_PKGS) $(MONGO_TEST_PKGS) $(UNSTABLE_TEST_PKGS) $(TAG_PKG)
9+
EXAMPLES_PKGS = $(shell etc/list_pkgs.sh ./examples)
10+
EXAMPLES_TEST_PKGS = $(shell etc/list_test_pkgs.sh ./examples)
11+
PKGS = $(BSON_PKGS) $(MONGO_PKGS) $(UNSTABLE_PKGS) $(TAG_PKG) $(EXAMPLES_PKGS)
12+
TEST_PKGS = $(BSON_TEST_PKGS) $(MONGO_TEST_PKGS) $(UNSTABLE_TEST_PKGS) $(TAG_PKG) $(EXAMPLES_TEST_PKGS)
1113

1214
TEST_TIMEOUT = 600
1315

examples/documentation_examples/examples.go

Lines changed: 52 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ func requireCursorLength(t *testing.T, cursor *mongo.Cursor, length int) {
3131
require.Equal(t, i, length)
3232
}
3333

34-
func stringSliceEquals(s1 []string, s2 []string) bool {
35-
if len(s1) != len(s2) {
36-
return false
37-
}
38-
39-
for i := range s1 {
40-
if s1[i] != s2[i] {
41-
return false
42-
}
43-
}
44-
45-
return true
46-
}
47-
4834
func containsKey(doc bsonx.Doc, key ...string) bool {
4935
_, err := doc.LookupErr(key...)
5036
if err != nil {
@@ -53,14 +39,12 @@ func containsKey(doc bsonx.Doc, key ...string) bool {
5339
return true
5440
}
5541

42+
// InsertExamples contains examples for insert operations.
5643
func InsertExamples(t *testing.T, db *mongo.Database) {
57-
err := db.RunCommand(
58-
context.Background(),
59-
bson.D{{"dropDatabase", 1}},
60-
).Err()
61-
require.NoError(t, err)
44+
coll := db.Collection("inventory_insert")
6245

63-
coll := db.Collection("inventory")
46+
err := coll.Drop(context.Background())
47+
require.NoError(t, err)
6448

6549
{
6650
// Start Example 1
@@ -144,14 +128,12 @@ func InsertExamples(t *testing.T, db *mongo.Database) {
144128
}
145129
}
146130

131+
// QueryToplevelFieldsExamples contains examples for querying top-level fields.
147132
func QueryToplevelFieldsExamples(t *testing.T, db *mongo.Database) {
148-
err := db.RunCommand(
149-
context.Background(),
150-
bson.D{{"dropDatabase", 1}},
151-
).Err()
152-
require.NoError(t, err)
133+
coll := db.Collection("inventory_query_top")
153134

154-
coll := db.Collection("inventory")
135+
err := coll.Drop(context.Background())
136+
require.NoError(t, err)
155137

156138
{
157139
// Start Example 6
@@ -314,14 +296,12 @@ func QueryToplevelFieldsExamples(t *testing.T, db *mongo.Database) {
314296

315297
}
316298

299+
// QueryEmbeddedDocumentsExamples contains examples for querying embedded document fields.
317300
func QueryEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
318-
err := db.RunCommand(
319-
context.Background(),
320-
bson.D{{"dropDatabase", 1}},
321-
).Err()
322-
require.NoError(t, err)
301+
coll := db.Collection("inventory_query_embedded")
323302

324-
coll := db.Collection("inventory")
303+
err := coll.Drop(context.Background())
304+
require.NoError(t, err)
325305

326306
{
327307
// Start Example 14
@@ -477,14 +457,12 @@ func QueryEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
477457

478458
}
479459

460+
// QueryArraysExamples contains examples for querying array fields.
480461
func QueryArraysExamples(t *testing.T, db *mongo.Database) {
481-
err := db.RunCommand(
482-
context.Background(),
483-
bson.D{{"dropDatabase", 1}},
484-
).Err()
485-
require.NoError(t, err)
462+
coll := db.Collection("inventory_query_array")
486463

487-
coll := db.Collection("inventory")
464+
err := coll.Drop(context.Background())
465+
require.NoError(t, err)
488466

489467
{
490468
// Start Example 20
@@ -665,14 +643,12 @@ func QueryArraysExamples(t *testing.T, db *mongo.Database) {
665643

666644
}
667645

646+
// QueryArrayEmbeddedDocumentsExamples contains examples for querying fields with arrays and embedded documents.
668647
func QueryArrayEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
669-
err := db.RunCommand(
670-
context.Background(),
671-
bson.D{{"dropDatabase", 1}},
672-
).Err()
673-
require.NoError(t, err)
648+
coll := db.Collection("inventory_query_array_embedded")
674649

675-
coll := db.Collection("inventory")
650+
err := coll.Drop(context.Background())
651+
require.NoError(t, err)
676652

677653
{
678654
// Start Example 29
@@ -896,14 +872,12 @@ func QueryArrayEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
896872
}
897873
}
898874

875+
// QueryNullMissingFieldsExamples contains examples for querying fields that are null or missing.
899876
func QueryNullMissingFieldsExamples(t *testing.T, db *mongo.Database) {
900-
err := db.RunCommand(
901-
context.Background(),
902-
bson.D{{"dropDatabase", 1}},
903-
).Err()
904-
require.NoError(t, err)
877+
coll := db.Collection("inventory_query_null_missing")
905878

906-
coll := db.Collection("inventory")
879+
err := coll.Drop(context.Background())
880+
require.NoError(t, err)
907881

908882
{
909883
// Start Example 38
@@ -976,14 +950,12 @@ func QueryNullMissingFieldsExamples(t *testing.T, db *mongo.Database) {
976950
}
977951
}
978952

953+
// ProjectionExamples contains examples for specifying projections in find operations.
979954
func ProjectionExamples(t *testing.T, db *mongo.Database) {
980-
err := db.RunCommand(
981-
context.Background(),
982-
bson.D{{"dropDatabase", 1}},
983-
).Err()
984-
require.NoError(t, err)
955+
coll := db.Collection("inventory_project")
985956

986-
coll := db.Collection("inventory")
957+
err := coll.Drop(context.Background())
958+
require.NoError(t, err)
987959

988960
{
989961
// Start Example 42
@@ -1115,7 +1087,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
11151087
doc := bsonx.Doc{}
11161088
for cursor.Next(context.Background()) {
11171089
doc = doc[:0]
1118-
err := cursor.Decode(doc)
1090+
err := cursor.Decode(&doc)
11191091
require.NoError(t, err)
11201092

11211093
require.True(t, containsKey(doc, "_id"))
@@ -1152,7 +1124,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
11521124
doc := bsonx.Doc{}
11531125
for cursor.Next(context.Background()) {
11541126
doc = doc[:0]
1155-
err := cursor.Decode(doc)
1127+
err := cursor.Decode(&doc)
11561128
require.NoError(t, err)
11571129

11581130
require.False(t, containsKey(doc, "_id"))
@@ -1188,7 +1160,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
11881160
doc := bsonx.Doc{}
11891161
for cursor.Next(context.Background()) {
11901162
doc = doc[:0]
1191-
err := cursor.Decode(doc)
1163+
err := cursor.Decode(&doc)
11921164
require.NoError(t, err)
11931165

11941166
require.True(t, containsKey(doc, "_id"))
@@ -1225,7 +1197,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
12251197
doc := bsonx.Doc{}
12261198
for cursor.Next(context.Background()) {
12271199
doc = doc[:0]
1228-
err := cursor.Decode(doc)
1200+
err := cursor.Decode(&doc)
12291201
require.NoError(t, err)
12301202

12311203
require.True(t, containsKey(doc, "_id"))
@@ -1234,9 +1206,9 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
12341206
require.True(t, containsKey(doc, "size"))
12351207
require.False(t, containsKey(doc, "instock"))
12361208

1237-
require.True(t, containsKey(doc, "uom", "size"))
1238-
require.False(t, containsKey(doc, "h", "size"))
1239-
require.False(t, containsKey(doc, "w", "size"))
1209+
require.True(t, containsKey(doc, "size", "uom"))
1210+
require.False(t, containsKey(doc, "size", "h"))
1211+
require.False(t, containsKey(doc, "size", "w"))
12401212

12411213
}
12421214

@@ -1265,7 +1237,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
12651237
doc := bsonx.Doc{}
12661238
for cursor.Next(context.Background()) {
12671239
doc = doc[:0]
1268-
err := cursor.Decode(doc)
1240+
err := cursor.Decode(&doc)
12691241
require.NoError(t, err)
12701242

12711243
require.True(t, containsKey(doc, "_id"))
@@ -1274,9 +1246,9 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
12741246
require.True(t, containsKey(doc, "size"))
12751247
require.True(t, containsKey(doc, "instock"))
12761248

1277-
require.False(t, containsKey(doc, "uom", "size"))
1278-
require.True(t, containsKey(doc, "h", "size"))
1279-
require.True(t, containsKey(doc, "w", "size"))
1249+
require.False(t, containsKey(doc, "size", "uom"))
1250+
require.True(t, containsKey(doc, "size", "h"))
1251+
require.True(t, containsKey(doc, "size", "w"))
12801252

12811253
}
12821254

@@ -1307,7 +1279,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
13071279
doc := bsonx.Doc{}
13081280
for cursor.Next(context.Background()) {
13091281
doc = doc[:0]
1310-
err := cursor.Decode(doc)
1282+
err := cursor.Decode(&doc)
13111283
require.NoError(t, err)
13121284

13131285
require.True(t, containsKey(doc, "_id"))
@@ -1360,7 +1332,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
13601332
doc := bsonx.Doc{}
13611333
for cursor.Next(context.Background()) {
13621334
doc = doc[:0]
1363-
err := cursor.Decode(doc)
1335+
err := cursor.Decode(&doc)
13641336
require.NoError(t, err)
13651337

13661338
require.True(t, containsKey(doc, "_id"))
@@ -1378,14 +1350,12 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
13781350
}
13791351
}
13801352

1353+
// UpdateExamples contains examples of update operations.
13811354
func UpdateExamples(t *testing.T, db *mongo.Database) {
1382-
err := db.RunCommand(
1383-
context.Background(),
1384-
bson.D{{"dropDatabase", 1}},
1385-
).Err()
1386-
require.NoError(t, err)
1355+
coll := db.Collection("inventory_update")
13871356

1388-
coll := db.Collection("inventory")
1357+
err := coll.Drop(context.Background())
1358+
require.NoError(t, err)
13891359

13901360
{
13911361
// Start Example 51
@@ -1537,7 +1507,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
15371507
doc := bsonx.Doc{}
15381508
for cursor.Next(context.Background()) {
15391509
doc = doc[:0]
1540-
err := cursor.Decode(doc)
1510+
err := cursor.Decode(&doc)
15411511
require.NoError(t, err)
15421512

15431513
uom, err := doc.LookupErr("size", "uom")
@@ -1594,7 +1564,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
15941564
doc := bsonx.Doc{}
15951565
for cursor.Next(context.Background()) {
15961566
doc = doc[:0]
1597-
err := cursor.Decode(doc)
1567+
err := cursor.Decode(&doc)
15981568
require.NoError(t, err)
15991569

16001570
uom, err := doc.LookupErr("size", "uom")
@@ -1651,7 +1621,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
16511621
doc := bsonx.Doc{}
16521622
for cursor.Next(context.Background()) {
16531623
doc = doc[:0]
1654-
err := cursor.Decode(doc)
1624+
err := cursor.Decode(&doc)
16551625
require.NoError(t, err)
16561626

16571627
require.True(t, containsKey(doc, "_id"))
@@ -1669,14 +1639,12 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
16691639

16701640
}
16711641

1642+
// DeleteExamples contains examples of delete operations.
16721643
func DeleteExamples(t *testing.T, db *mongo.Database) {
1673-
err := db.RunCommand(
1674-
context.Background(),
1675-
bson.D{{"dropDatabase", 1}},
1676-
).Err()
1677-
require.NoError(t, err)
1644+
coll := db.Collection("inventory_delete")
16781645

1679-
coll := db.Collection("inventory")
1646+
err := coll.Drop(context.Background())
1647+
require.NoError(t, err)
16801648

16811649
{
16821650
// Start Example 55

0 commit comments

Comments
 (0)