Skip to content

Commit 541ffbf

Browse files
committed
test: Added tests
1 parent a095aa1 commit 541ffbf

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

v3/integrations/nroci/nroci_nosql_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

v3/integrations/nroci/nroci_nosql.go renamed to v3/integrations/nroci/nrocinosql.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ func extractHostPort(endpoint string) (host, port string) {
116116
}
117117

118118
func extractRequestFields(req any) (string, string, string) {
119-
var collection, statement, databaseName string
119+
var collection, statement, namespace string
120120
switch r := req.(type) {
121121
case *nosqldb.TableRequest:
122122
collection = r.TableName
123123
statement = r.Statement
124-
databaseName = r.Namespace
124+
namespace = r.Namespace
125125
default:
126126
// keep strings empty
127127
}
128-
return collection, statement, databaseName
128+
return collection, statement, namespace
129129
}
130130

131131
// executeWithDatastoreSegment is a generic helper function that executes a query with a given function from the
@@ -146,12 +146,12 @@ func executeWithDatastoreSegment[T any, R any](
146146

147147
// Extract host and port from config endpoint
148148
host, port := extractHostPort(cw.Config.Endpoint)
149-
collection, statement, databaseName := extractRequestFields(rw.ClientRequest)
149+
collection, statement, namespace := extractRequestFields(rw.ClientRequest)
150150
sgmt := newrelic.DatastoreSegment{
151151
StartTime: txn.StartSegmentNow(),
152152
Product: newrelic.DatastoreOracle,
153153
Collection: collection,
154-
DatabaseName: databaseName,
154+
DatabaseName: namespace, // using the namespace as the database name in this instance
155155
ParameterizedQuery: statement,
156156
Host: host,
157157
PortPathOrID: port,
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2020 New Relic Corporation. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package nroci
4+
5+
import (
6+
"testing"
7+
8+
"github.com/oracle/nosql-go-sdk/nosqldb"
9+
)
10+
11+
func Test_extractRequestFields(t *testing.T) {
12+
// as we move into other types of requests, I will refactor this test to take in those types as well
13+
tests := []struct {
14+
name string // description of this test case
15+
// Named input parameters for target function.
16+
req any
17+
want string
18+
want2 string
19+
want3 string
20+
}{
21+
{
22+
name: "Default case should return 3 empty strings with non-used type",
23+
req: &nosqldb.SystemRequest{},
24+
want: "",
25+
want2: "",
26+
want3: "",
27+
},
28+
{
29+
name: "Should populate all 3 strings with *nosqldb.TableRequest",
30+
req: &nosqldb.TableRequest{
31+
TableName: "table1",
32+
Statement: `SELECT * FROM table1 WHERE param="value"`,
33+
Namespace: "oci_test",
34+
},
35+
want: "table1",
36+
want2: `SELECT * FROM table1 WHERE param="value"`,
37+
want3: "oci_test",
38+
},
39+
{
40+
name: "Should return empty string with *nosqldb.TableRequest",
41+
req: &nosqldb.TableRequest{},
42+
want: "",
43+
want2: "",
44+
want3: "",
45+
},
46+
}
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
got, got2, got3 := extractRequestFields(tt.req)
50+
if got != tt.want {
51+
t.Errorf("extractRequestFields() = %v, want %v", got, tt.want)
52+
}
53+
if got2 != tt.want2 {
54+
t.Errorf("extractRequestFields() = %v, want %v", got2, tt.want2)
55+
}
56+
if got3 != tt.want3 {
57+
t.Errorf("extractRequestFields() = %v, want %v", got3, tt.want3)
58+
}
59+
})
60+
}
61+
}
62+
63+
func Test_extractHostPort(t *testing.T) {
64+
tests := []struct {
65+
name string // description of this test case
66+
// Named input parameters for target function.
67+
endpoint string
68+
want string
69+
want2 string
70+
}{
71+
{
72+
name: "Endpoint is empty should return 2 empty strings",
73+
endpoint: "",
74+
want: "",
75+
want2: "",
76+
},
77+
{
78+
name: "url.Parse returns an error. Should return 2 empty strings",
79+
endpoint: "not:an.en#d()point",
80+
want: "",
81+
want2: "",
82+
},
83+
{
84+
name: "Host and port both exist in a valid url with https scheme",
85+
endpoint: "https://ocitest:8080",
86+
want: "ocitest",
87+
want2: "8080",
88+
},
89+
{
90+
name: "Host and port both exist in a valid url with http scheme",
91+
endpoint: "http://ocitest:8080",
92+
want: "ocitest",
93+
want2: "8080",
94+
},
95+
{
96+
name: "Host exists but port does not in a valid url with https scheme",
97+
endpoint: "https://ocitest",
98+
want: "ocitest",
99+
want2: "443",
100+
},
101+
{
102+
name: "Host exists but port does not in a valid url with http scheme",
103+
endpoint: "http://ocitest",
104+
want: "ocitest",
105+
want2: "80",
106+
},
107+
{
108+
name: "Host exists and pot exist but no scheme provided",
109+
endpoint: "ocitest",
110+
want: "ocitest",
111+
want2: "80",
112+
},
113+
}
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
got, got2 := extractHostPort(tt.endpoint)
117+
if got != tt.want {
118+
t.Errorf("extractHostPort() -> host = %v, want %v", got, tt.want)
119+
}
120+
if got2 != tt.want2 {
121+
t.Errorf("extractHostPort() -> port = %v, want %v", got2, tt.want2)
122+
}
123+
})
124+
}
125+
}

0 commit comments

Comments
 (0)