From e07d4ed65d9f8e7cae35d350e6910c9f3918cfac Mon Sep 17 00:00:00 2001 From: Phenix Rizen Date: Mon, 29 Jul 2019 16:46:20 -0700 Subject: [PATCH] added additonal test files --- escape_test.go | 14 +++++++++++ options_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ pagable_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ response_test.go | 18 ++++++++++++++ utils_test.go | 23 ++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 escape_test.go create mode 100644 options_test.go create mode 100644 pagable_test.go create mode 100644 response_test.go create mode 100644 utils_test.go diff --git a/escape_test.go b/escape_test.go new file mode 100644 index 0000000..7ea8916 --- /dev/null +++ b/escape_test.go @@ -0,0 +1,14 @@ +package gocosmosdb + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEscapeSQL(t *testing.T) { + assert := assert.New(t) + + sql := escapeSQL("SELECT * FROM root \n\r\x00\x1a\" \\ r") + assert.Equal("SELECT * FROM root \\n\\r\\0\\Z\\\" \\\\ r", sql) +} diff --git a/options_test.go b/options_test.go new file mode 100644 index 0000000..d0eec5a --- /dev/null +++ b/options_test.go @@ -0,0 +1,60 @@ +package gocosmosdb + +import ( + "context" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCallOptions(t *testing.T) { + assert := assert.New(t) + opts := []CallOption{} + opts = append(opts, PartitionKey("test")) + opts = append(opts, Upsert()) + opts = append(opts, Limit(100)) + opts = append(opts, Continuation("continueToken")) + opts = append(opts, ConsistencyLevel("Strong")) + opts = append(opts, SessionToken("sessionToken")) + opts = append(opts, CrossPartition()) + opts = append(opts, IfMatch("eTag")) + opts = append(opts, IfNoneMatch("eTag")) + opts = append(opts, IfModifiedSince("someDate")) + opts = append(opts, ChangeFeed()) + opts = append(opts, ThroughputRUs(400)) + opts = append(opts, PartitionKeyRangeID(0)) + opts = append(opts, EnableQueryScan()) + opts = append(opts, EnableParallelizeCrossPartitionQuery()) + ctx := context.WithValue(context.Background(), "foo", "bar") + opts = append(opts, WithContext(ctx)) + + link := "http://localhost:8080" + req, err := http.NewRequest("POST", link, nil) + if err != nil { + assert.Nil(err) + } + r := ResourceRequest(link, req) + for i := 0; i < len(opts); i++ { + if err := opts[i](r); err != nil { + assert.Nil(err) + } + } + + assert.Equal([]string{"[\"test\"]"}, r.Header[HeaderPartitionKey]) + assert.Equal([]string{"true"}, r.Header[HeaderUpsert]) + assert.Equal([]string{"100"}, r.Header[HeaderMaxItemCount]) + assert.Equal([]string{"continueToken"}, r.Header[HeaderContinuation]) + assert.Equal([]string{"Strong"}, r.Header[HeaderConsistencyLevel]) + assert.Equal([]string{"sessionToken"}, r.Header[HeaderSessionToken]) + assert.Equal([]string{"true"}, r.Header[HeaderCrossPartition]) + assert.Equal([]string{"eTag"}, r.Header[HeaderIfMatch]) + assert.Equal([]string{"eTag"}, r.Header[HeaderIfNonMatch]) + assert.Equal([]string{"someDate"}, r.Header[HeaderIfModifiedSince]) + //assert.Equal([]string{"Incremental feed"}, r.Header[HeaderAIM]) + assert.Equal([]string{"400"}, r.Header[HeaderOfferThroughput]) + assert.Equal([]string{"0"}, r.Header[HeaderPartitionKeyRangeID]) + assert.Equal([]string{"true"}, r.Header[HeaderEnableScan]) + assert.Equal([]string{"true"}, r.Header[HeaderParalelizeCrossPartition]) + assert.Equal(ctx, r.rContext) +} diff --git a/pagable_test.go b/pagable_test.go new file mode 100644 index 0000000..3a31773 --- /dev/null +++ b/pagable_test.go @@ -0,0 +1,61 @@ +package gocosmosdb + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPagable(t *testing.T) { + assert := assert.New(t) + resp1 := `{ + "_rid": "d9RzAJRFKgw=", + "Documents": [ + { + "id": "SalesOrder1", + "ponumber": "PO18009186470", + "_rid": "d9RzAJRFKgwBAAAAAAAAAA==", + "_self": "dbs/d9RzAA==/colls/d9RzAJRFKgw=/docs/d9RzAJRFKgwBAAAAAAAAAA==/", + "_etag": "\"0000d986-0000-0000-0000-56f9e25b0000\"", + "_ts": 1459216987, + "_attachments": "attachments/" + } + ], + "_count": 2 + }` + resp2 := `{ + "_rid": "d9RzAJRFKgw=", + "Documents": [ + { + "id": "SalesOrder2", + "ponumber": "PO15428132599", + "_rid": "d9RzAJRFKgwCAAAAAAAAAA==", + "_self": "dbs/d9RzAA==/colls/d9RzAJRFKgw=/docs/d9RzAJRFKgwCAAAAAAAAAA==/", + "_etag": "\"0000da86-0000-0000-0000-56f9e25b0000\"", + "_ts": 1459216987, + "_attachments": "attachments/" + } + ], + "_count": 2 + }` + s := ServerFactory(resp1, resp2) + defer s.Close() + client := New(s.URL, Config{MasterKey: "YXJpZWwNCg=="}, log) + docs := []testDoc{} + query := &QueryWithParameters{ + Query: "SELECT * FROM root r WHERE r._ts > @_ts", + Parameters: []QueryParameter{ + QueryParameter{ + Name: "@_ts", + Value: 1459216957, + }, + }, + } + pg := client.NewPagableQuery("dbs/d9RzAA==/colls/d9RzAJRFKgw=", query, 1, &docs) + assert.NotNil(pg) + assert.IsType(&PagableQuery{}, pg) + pg.Next() + assert.Equal("SalesOrder1", docs[0].Id) + pg.Next() + assert.Equal("SalesOrder2", docs[0].Id) +} diff --git a/response_test.go b/response_test.go new file mode 100644 index 0000000..56a0137 --- /dev/null +++ b/response_test.go @@ -0,0 +1,18 @@ +package gocosmosdb + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExpectStatusCodes(t *testing.T) { + assert := assert.New(t) + + st1 := expectStatusCode(200) + assert.Equal(true, st1(200)) + + st2 := expectStatusCodeXX(400) + assert.Equal(true, st2(499)) + +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..1371062 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,23 @@ +package gocosmosdb + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestUtils(t *testing.T) { + assert := assert.New(t) + + uuid := genId() + assert.Equal(36, len(uuid)) + + exp := Expirable{} + exp.SetTTL(100 * time.Second) + assert.Equal(int64(100), exp.TTL) + + b, err := stringify([]byte("foo")) + assert.Nil(err) + assert.Equal([]byte("foo"), b) +}