|
| 1 | +/* |
| 2 | +Copyright 2023 The Dapr Authors |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package postgres |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestMetadata(t *testing.T) { |
| 25 | + t.Run("missing connection string", func(t *testing.T) { |
| 26 | + m := psqlMetadata{} |
| 27 | + props := map[string]string{} |
| 28 | + |
| 29 | + err := m.InitWithMetadata(props) |
| 30 | + require.Error(t, err) |
| 31 | + require.ErrorContains(t, err, "connection string") |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("has connection string", func(t *testing.T) { |
| 35 | + m := psqlMetadata{} |
| 36 | + props := map[string]string{ |
| 37 | + "connectionString": "foo", |
| 38 | + } |
| 39 | + |
| 40 | + err := m.InitWithMetadata(props) |
| 41 | + require.NoError(t, err) |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("default timeout", func(t *testing.T) { |
| 45 | + m := psqlMetadata{} |
| 46 | + props := map[string]string{ |
| 47 | + "connectionString": "foo", |
| 48 | + } |
| 49 | + |
| 50 | + err := m.InitWithMetadata(props) |
| 51 | + require.NoError(t, err) |
| 52 | + assert.Equal(t, 20*time.Second, m.Timeout) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("invalid timeout", func(t *testing.T) { |
| 56 | + m := psqlMetadata{} |
| 57 | + props := map[string]string{ |
| 58 | + "connectionString": "foo", |
| 59 | + "timeout": "NaN", |
| 60 | + } |
| 61 | + |
| 62 | + err := m.InitWithMetadata(props) |
| 63 | + require.Error(t, err) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("positive timeout", func(t *testing.T) { |
| 67 | + m := psqlMetadata{} |
| 68 | + props := map[string]string{ |
| 69 | + "connectionString": "foo", |
| 70 | + "timeout": "42", |
| 71 | + } |
| 72 | + |
| 73 | + err := m.InitWithMetadata(props) |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, 42*time.Second, m.Timeout) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("zero timeout", func(t *testing.T) { |
| 79 | + m := psqlMetadata{} |
| 80 | + props := map[string]string{ |
| 81 | + "connectionString": "foo", |
| 82 | + "timeout": "0", |
| 83 | + } |
| 84 | + |
| 85 | + err := m.InitWithMetadata(props) |
| 86 | + require.Error(t, err) |
| 87 | + }) |
| 88 | +} |
0 commit comments