summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/mongoimport/options_test.go
blob: 55950a5d5c7716d5ad39b554d8791047d7643000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package mongoimport

import (
	"fmt"
	"testing"

	"github.com/mongodb/mongo-tools/common/db"
	"github.com/mongodb/mongo-tools/common/options"
	. "github.com/smartystreets/goconvey/convey"
)

// Regression test for TOOLS-1741
func TestWriteConcernWithURIParsing(t *testing.T) {
	Convey("With an IngestOptions and ToolsOptions", t, func() {

		// create an 'EnabledOptions' to determine what options should be able to be
		// parsed and set form the input.
		enabled := options.EnabledOptions{URI: true}

		// create a new tools options to hold the parsed options
		opts := options.New("", "", enabled)

		// create an 'IngestOptions', which holds the value of the write concern
		// for mongoimport.
		ingestOpts := &IngestOptions{}
		opts.AddOptions(ingestOpts)

		// Specify that a write concern set on the URI is not an error and is a known
		// possible option.
		opts.URI.AddKnownURIParameters(options.KnownURIOptionsWriteConcern)

		Convey("Parsing with no value should leave write concern empty", func() {
			_, err := opts.ParseArgs([]string{})
			So(err, ShouldBeNil)
			So(ingestOpts.WriteConcern, ShouldEqual, "")
			Convey("and building write concern object, WMode should be majority", func() {
				sessionSafety, err := db.BuildWriteConcern(ingestOpts.WriteConcern, "",
					opts.ParsedConnString())
				So(err, ShouldBeNil)
				So(sessionSafety.WMode, ShouldEqual, "majority")
			})
		})

		Convey("Parsing with no writeconcern in URI should not error", func() {
			args := []string{
				"--uri", "mongodb://localhost:27017/test",
			}
			_, err := opts.ParseArgs(args)
			So(err, ShouldBeNil)
			So(ingestOpts.WriteConcern, ShouldEqual, "")
			Convey("and parsing write concern, WMode should be majority", func() {
				sessionSafety, err := db.BuildWriteConcern(ingestOpts.WriteConcern, "",
					opts.ParsedConnString())
				So(err, ShouldBeNil)
				So(sessionSafety, ShouldNotBeNil)
				So(sessionSafety.WMode, ShouldEqual, "majority")
			})
		})
		Convey("Parsing with both writeconcern in URI and command line should error", func() {
			args := []string{
				"--uri", "mongodb://localhost:27017/test",
				"--writeConcern", "majority",
			}
			_, err := opts.ParseArgs(args)
			So(err, ShouldBeNil)
			So(ingestOpts.WriteConcern, ShouldEqual, "majority")
			Convey("and parsing write concern, WMode should be majority", func() {
				_, err := db.BuildWriteConcern(ingestOpts.WriteConcern, "",
					opts.ParsedConnString())
				So(err, ShouldResemble, fmt.Errorf("cannot specify writeConcern string and connectionString object"))
			})
		})
	})
}