aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/jackc/pgservicefile/pgservicefile_test.go
blob: e5cc3eef67b72698de26a293d0feb9ea9a0cdc3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package pgservicefile_test

import (
	"bytes"
	"testing"

	"github.com/jackc/pgservicefile"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestParseServicefile(t *testing.T) {
	buf := bytes.NewBufferString(`# A comment
[abc]
host=abc.example.com
port=9999
dbname=abcdb
user=abcuser
# Another comment

[def]
host = def.example.com
dbname = defdb
user = defuser
application_name = has space
`)

	servicefile, err := pgservicefile.ParseServicefile(buf)
	require.NoError(t, err)
	require.NotNil(t, servicefile)

	assert.Len(t, servicefile.Services, 2)
	assert.Equal(t, "abc", servicefile.Services[0].Name)
	assert.Equal(t, "def", servicefile.Services[1].Name)

	abc, err := servicefile.GetService("abc")
	require.NoError(t, err)
	assert.Equal(t, servicefile.Services[0], abc)
	assert.Len(t, abc.Settings, 4)
	assert.Equal(t, "abc.example.com", abc.Settings["host"])
	assert.Equal(t, "9999", abc.Settings["port"])
	assert.Equal(t, "abcdb", abc.Settings["dbname"])
	assert.Equal(t, "abcuser", abc.Settings["user"])

	def, err := servicefile.GetService("def")
	require.NoError(t, err)
	assert.Equal(t, servicefile.Services[1], def)
	assert.Len(t, def.Settings, 4)
	assert.Equal(t, "def.example.com", def.Settings["host"])
	assert.Equal(t, "defdb", def.Settings["dbname"])
	assert.Equal(t, "defuser", def.Settings["user"])
	assert.Equal(t, "has space", def.Settings["application_name"])
}

func TestParseServicefileWithInvalidFile(t *testing.T) {
	buf := bytes.NewBufferString("Invalid syntax\n")

	servicefile, err := pgservicefile.ParseServicefile(buf)
	assert.Error(t, err)
	assert.Nil(t, servicefile)
}