|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sql |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/google/sqlcommenter/go/core" |
| 22 | +) |
| 23 | + |
| 24 | +func TestWithComment_NoContext(t *testing.T) { |
| 25 | + testBasicConn := &mockConn{} |
| 26 | + testCases := []struct { |
| 27 | + desc string |
| 28 | + commenterOptions core.CommenterOptions |
| 29 | + query string |
| 30 | + wantQuery string |
| 31 | + }{ |
| 32 | + { |
| 33 | + desc: "empty commenter options", |
| 34 | + commenterOptions: core.CommenterOptions{}, |
| 35 | + query: "SELECT 1;", |
| 36 | + wantQuery: "SELECT 1;", |
| 37 | + }, |
| 38 | + { |
| 39 | + desc: "only enable DBDriver", |
| 40 | + commenterOptions: core.CommenterOptions{ |
| 41 | + Config: core.CommenterConfig{EnableDBDriver: true}, |
| 42 | + }, |
| 43 | + query: "SELECT 1;", |
| 44 | + wantQuery: "SELECT 1/*db_driver=database%2Fsql%3A*/;", |
| 45 | + }, |
| 46 | + { |
| 47 | + desc: "enable DBDriver and pass static tag driver name", |
| 48 | + commenterOptions: core.CommenterOptions{ |
| 49 | + Config: core.CommenterConfig{EnableDBDriver: true}, |
| 50 | + Tags: core.StaticTags{DriverName: "postgres"}, |
| 51 | + }, |
| 52 | + query: "SELECT 1;", |
| 53 | + wantQuery: "SELECT 1/*db_driver=database%2Fsql%3Apostgres*/;", |
| 54 | + }, |
| 55 | + { |
| 56 | + desc: "enable DBDriver and pass all static tags", |
| 57 | + commenterOptions: core.CommenterOptions{ |
| 58 | + Config: core.CommenterConfig{EnableDBDriver: true}, |
| 59 | + Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"}, |
| 60 | + }, |
| 61 | + query: "SELECT 1;", |
| 62 | + wantQuery: "SELECT 1/*db_driver=database%2Fsql%3Apostgres*/;", |
| 63 | + }, |
| 64 | + { |
| 65 | + desc: "enable other tags and pass all static tags", |
| 66 | + commenterOptions: core.CommenterOptions{ |
| 67 | + Config: core.CommenterConfig{EnableDBDriver: true, EnableApplication: true, EnableFramework: true}, |
| 68 | + Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"}, |
| 69 | + }, |
| 70 | + query: "SELECT 1;", |
| 71 | + wantQuery: "SELECT 1/*application=app-1,db_driver=database%2Fsql%3Apostgres*/;", |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, tc := range testCases { |
| 75 | + testConn := newSQLCommenterConn(testBasicConn, tc.commenterOptions) |
| 76 | + ctx := context.Background() |
| 77 | + if got, want := testConn.withComment(ctx, tc.query), tc.wantQuery; got != want { |
| 78 | + t.Errorf("testConn.withComment(ctx, %q) = %q, want = %q", tc.query, got, want) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestWithComment_WithContext(t *testing.T) { |
| 84 | + testBasicConn := &mockConn{} |
| 85 | + testCases := []struct { |
| 86 | + desc string |
| 87 | + commenterOptions core.CommenterOptions |
| 88 | + ctx context.Context |
| 89 | + query string |
| 90 | + wantQuery string |
| 91 | + }{ |
| 92 | + { |
| 93 | + desc: "empty commenter options", |
| 94 | + commenterOptions: core.CommenterOptions{}, |
| 95 | + ctx: getContextWithKeyValue( |
| 96 | + map[string]string{ |
| 97 | + "route": "listData", |
| 98 | + "framework": "custom-golang", |
| 99 | + }, |
| 100 | + ), |
| 101 | + query: "SELECT 1;", |
| 102 | + wantQuery: "SELECT 1;", |
| 103 | + }, |
| 104 | + { |
| 105 | + desc: "only all options but context has few tags", |
| 106 | + commenterOptions: core.CommenterOptions{ |
| 107 | + Config: core.CommenterConfig{ |
| 108 | + EnableDBDriver: true, |
| 109 | + EnableRoute: true, |
| 110 | + EnableFramework: true, |
| 111 | + EnableController: true, |
| 112 | + EnableAction: true, |
| 113 | + EnableTraceparent: true, |
| 114 | + EnableApplication: true, |
| 115 | + }, |
| 116 | + Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"}, |
| 117 | + }, |
| 118 | + ctx: getContextWithKeyValue( |
| 119 | + map[string]string{ |
| 120 | + "route": "listData", |
| 121 | + "framework": "custom-golang", |
| 122 | + }, |
| 123 | + ), |
| 124 | + query: "SELECT 1;", |
| 125 | + wantQuery: "SELECT 1/*application=app-1,db_driver=database%2Fsql%3Apostgres,framework=custom-golang,route=listData*/;", |
| 126 | + }, |
| 127 | + { |
| 128 | + desc: "only all options but context contains all tags", |
| 129 | + commenterOptions: core.CommenterOptions{ |
| 130 | + Config: core.CommenterConfig{ |
| 131 | + EnableDBDriver: true, |
| 132 | + EnableRoute: true, |
| 133 | + EnableFramework: true, |
| 134 | + EnableController: true, |
| 135 | + EnableAction: true, |
| 136 | + EnableTraceparent: true, |
| 137 | + EnableApplication: true, |
| 138 | + }, |
| 139 | + Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"}, |
| 140 | + }, |
| 141 | + ctx: getContextWithKeyValue( |
| 142 | + map[string]string{ |
| 143 | + "route": "listData", |
| 144 | + "framework": "custom-golang", |
| 145 | + "controller": "custom-controller", |
| 146 | + "action": "any action", |
| 147 | + }, |
| 148 | + ), |
| 149 | + query: "SELECT 1;", |
| 150 | + wantQuery: "SELECT 1/*action=any+action,application=app-1,db_driver=database%2Fsql%3Apostgres,framework=custom-golang,route=listData*/;", |
| 151 | + }, |
| 152 | + } |
| 153 | + for _, tc := range testCases { |
| 154 | + testConn := newSQLCommenterConn(testBasicConn, tc.commenterOptions) |
| 155 | + if got, want := testConn.withComment(tc.ctx, tc.query), tc.wantQuery; got != want { |
| 156 | + t.Errorf("testConn.withComment(ctx, %q) = %q, want = %q", tc.query, got, want) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func getContextWithKeyValue(vals map[string]string) context.Context { |
| 162 | + ctx := context.Background() |
| 163 | + for k, v := range vals { |
| 164 | + ctx = context.WithValue(ctx, k, v) |
| 165 | + } |
| 166 | + return ctx |
| 167 | +} |
0 commit comments