|
| 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