|
| 1 | +// Copyright 2024 Dolthub, Inc. |
| 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 output |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/dolthub/go-mysql-server/sql" |
| 21 | +) |
| 22 | + |
| 23 | +func Test_SetConfig(t *testing.T) { |
| 24 | + RunScripts(t, []ScriptTest{ |
| 25 | + { |
| 26 | + Name: "set_config", |
| 27 | + Assertions: []ScriptTestAssertion{ |
| 28 | + { |
| 29 | + // non-namespaced, non-system settings result in an error: "unrecognized configuration parameter" |
| 30 | + Query: "SELECT set_config('doesnotexist', '42', false);", |
| 31 | + ExpectedErr: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + Query: "SELECT set_config('', 'bar', false);", |
| 35 | + ExpectedErr: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + Query: "SELECT set_config(NULL, 'bar', false);", |
| 39 | + ExpectedErr: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + // Set a system config setting |
| 43 | + Query: "SELECT set_config('search_path', '123', false);", |
| 44 | + Expected: []sql.Row{{"123"}}, |
| 45 | + }, |
| 46 | + { |
| 47 | + Query: "SELECT current_setting('search_path');", |
| 48 | + Expected: []sql.Row{{"123"}}, |
| 49 | + }, |
| 50 | + { |
| 51 | + // Set a user config setting in a custom namespace |
| 52 | + Query: "SELECT set_config('mynamespace.var', 'bar', false);", |
| 53 | + Expected: []sql.Row{{"bar"}}, |
| 54 | + }, |
| 55 | + { |
| 56 | + Query: "SELECT current_setting('mynamespace.var');", |
| 57 | + Expected: []sql.Row{{"bar"}}, |
| 58 | + }, |
| 59 | + { |
| 60 | + // Only text values are supported |
| 61 | + Query: "SELECT set_config('myvars.boo', 3.14159, false);", |
| 62 | + ExpectedErr: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + // All settings must be text |
| 66 | + Query: "SELECT set_config('myvars.boo', 3.14159::text, false);", |
| 67 | + Expected: []sql.Row{{"3.14159"}}, |
| 68 | + }, |
| 69 | + { |
| 70 | + Query: "SELECT current_setting('myvars.boo');", |
| 71 | + Expected: []sql.Row{{"3.14159"}}, |
| 72 | + }, |
| 73 | + { |
| 74 | + // A NULL value is turned into the empty string |
| 75 | + Query: "SELECT set_config('myvars.nullval', NULL, false);", |
| 76 | + Expected: []sql.Row{{""}}, |
| 77 | + }, |
| 78 | + { |
| 79 | + Query: "SELECT current_setting('myvars.nullval');", |
| 80 | + Expected: []sql.Row{{""}}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | +} |
0 commit comments