You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
themis/fmt_test.go

105 lines
2.7 KiB

package themis
import (
"context"
"database/sql"
"fmt"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFormatRows(t *testing.T) {
db, err := sql.Open("sqlite3", fmt.Sprintf(TEST_CONN_STRING_PATTERN, "format-rows"))
require.NoError(t, err)
store, err := NewStore(db, zerolog.Nop())
assert.NoError(t, err)
rows, err := store.db.Query("SELECT provinces.name, provinces.region, provinces.area, provinces.trade_node FROM provinces WHERE area = 'Gascony'")
assert.NoError(t, err)
fmtd, err := FormatRows(context.Background(), rows)
assert.NoError(t, err)
assert.Equal(t, `| name | region | area | trade_node |
| -------- | ------ | ------- | ---------- |
| Labourd | France | Gascony | Bordeaux |
| Armagnac | France | Gascony | Bordeaux |
| Béarn | France | Gascony | Bordeaux |
| Foix | France | Gascony | Bordeaux |
`, fmtd)
}
func TestFormatRowsAggregated(t *testing.T) {
db, err := sql.Open("sqlite3", fmt.Sprintf(TEST_CONN_STRING_PATTERN, "format-rows"))
require.NoError(t, err)
store, err := NewStore(db, zerolog.Nop())
assert.NoError(t, err)
rows, err := store.db.Query("SELECT count(1) as total, trade_node from provinces where region = 'France' group by trade_node")
assert.NoError(t, err)
fmtd, err := FormatRows(context.Background(), rows)
assert.NoError(t, err)
assert.Equal(t, `| total | trade_node |
| ----- | --------------- |
| 25 | Bordeaux |
| 24 | Champagne |
| 8 | English Channel |
| 4 | Genoa |
| 5 | Valencia |
`, fmtd)
}
func TestFormatRowsInvalidQuery(t *testing.T) {
db, err := sql.Open("sqlite3", fmt.Sprintf(TEST_CONN_STRING_PATTERN, "format-rows"))
require.NoError(t, err)
store, err := NewStore(db, zerolog.Nop())
assert.NoError(t, err)
_, err = store.db.Query("SELECT count(name), distinct(trade_node) from provinces where region = 'France'")
assert.Error(t, err)
}
func TestFormatStringSlice(t *testing.T) {
tests := []struct {
name string
s []string
want string
}{
{
name: "empty",
s: []string{},
want: "",
},
{
name: "single",
s: []string{"foo"},
want: "foo",
},
{
name: "two",
s: []string{"foo", "bar"},
want: "foo and bar",
},
{
name: "three",
s: []string{"foo", "bar", "baz"},
want: "foo, bar and baz",
},
{
name: "four",
s: []string{"foo", "bar", "baz", "biz"},
want: "foo, bar, baz and biz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatStringSlice(tt.s); got != tt.want {
t.Errorf("FormatStringSlice() = %v, want %v", got, tt.want)
}
})
}
}