Skip to content

Commit

Permalink
Add tests for internal/view.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Dec 13, 2020
1 parent b994b1d commit d47b2e4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
24 changes: 6 additions & 18 deletions internal/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type View struct {
DiffIntvl [2]int // Columns interval for diff
Cols []string // Columns names
Ncols int // Number of columns returned by query, used as a right border for OrderKey
OrderKey int // Number of column used for order
OrderKey int // Index of column used for order
OrderDesc bool // Order direction: descending (true) or ascending (false)
UniqueKey int // Unique key that used on rows comparing when building diffs, by default it's zero which is OK in almost all contexts
ColsWidth map[int]int // Set width for columns and control an aligning
Aligned bool // Is aligning calculated?
Msg string // Show this text in Cmdline when switching to this unit
Filters map[int]*regexp.Regexp // Storage for filter patterns: key is the column index, value - regexp pattern
UniqueKey int // index of column used as unique key when comparing rows during diffs, by default it's zero which is OK in almost all views
ColsWidth map[int]int // Width used for columns and control an aligning
Aligned bool // Flag shows aligning is calculated or not
Msg string // Show this text in Cmdline when switching to this view
Filters map[int]*regexp.Regexp // Filter patterns: key is the column index, value - regexp pattern
Refresh time.Duration // Number of seconds between update view.
ShowExtra int // Specifies extra stats should be enabled on the view.
}
Expand Down Expand Up @@ -52,7 +52,6 @@ func New() Views {
Msg: "Show replication statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatTablesUnit describes how to handle pg_stat_all_tables and pg_statio_all_tables views
"tables": {
Name: "tables",
QueryTmpl: query.PgStatTablesDefault,
Expand All @@ -64,7 +63,6 @@ func New() Views {
Msg: "Show tables statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatIndexesUnit describes how to handle pg_stat_all_indexes and pg_statio_all_indexes views
"indexes": {
Name: "indexes",
QueryTmpl: query.PgStatIndexesDefault,
Expand All @@ -76,7 +74,6 @@ func New() Views {
Msg: "Show indexes statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgTablesSizesUnit describes how to handle statistics about tables sizes
"sizes": {
Name: "sizes",
QueryTmpl: query.PgTablesSizesDefault,
Expand All @@ -88,7 +85,6 @@ func New() Views {
Msg: "Show tables sizes statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatFunctionsUnit describes how to handle pg_stat_user_functions view
"functions": {
Name: "functions",
QueryTmpl: query.PgStatFunctionsDefault,
Expand All @@ -100,7 +96,6 @@ func New() Views {
Msg: "Show functions statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatVacuumUnit describes how to handle pg_stat_progress_vacuum view
"progress_vacuum": {
Name: "progress_vacuum",
QueryTmpl: query.PgStatProgressVacuumDefault,
Expand All @@ -112,7 +107,6 @@ func New() Views {
Msg: "Show vacuum progress statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatProgressClusterUnit describes how to handle pg_stat_progress_cluster view
"progress_cluster": {
Name: "progress_cluster",
QueryTmpl: query.PgStatProgressClusterDefault,
Expand All @@ -135,7 +129,6 @@ func New() Views {
Msg: "Show create index/reindex progress statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgStatActivityUnit describes how to handle pg_stat_activity view
"activity": {
Name: "activity",
QueryTmpl: query.PgStatActivityDefault,
Expand All @@ -147,7 +140,6 @@ func New() Views {
Msg: "Show activity statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgSSTimingUnit describes how to handle pg_stat_statements view with timing stats
"statements_timings": {
Name: "statements_timings",
QueryTmpl: query.PgStatStatementsTimingDefault,
Expand All @@ -160,7 +152,6 @@ func New() Views {
Msg: "Show statements timings statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgSSGeneralUnit describes how to handle pg_stat_statements view with general stats
"statements_general": {
Name: "statements_general",
QueryTmpl: query.PgStatStatementsGeneralDefault,
Expand All @@ -173,7 +164,6 @@ func New() Views {
Msg: "Show statements general statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgSSIoUnit describes how to handle pg_stat_statements view with stats related to buffers IO
"statements_io": {
Name: "statements_io",
QueryTmpl: query.PgStatStatementsIoDefault,
Expand All @@ -186,7 +176,6 @@ func New() Views {
Msg: "Show statements IO statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgSSTempUnit describes how to handle pg_stat_statements view with stats related to temp files IO
"statements_temp": {
Name: "statements_temp",
QueryTmpl: query.PgStatStatementsTempDefault,
Expand All @@ -199,7 +188,6 @@ func New() Views {
Msg: "Show statements temp files statistics",
Filters: map[int]*regexp.Regexp{},
},
// PgSSLocalUnit describes how to handle pg_stat_statements view with stats related to local buffers IO
"statements_local": {
Name: "statements_local",
QueryTmpl: query.PgStatStatementsLocalDefault,
Expand Down
81 changes: 81 additions & 0 deletions internal/view/view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package view

import (
"github.com/lesovsky/pgcenter/internal/query"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNew(t *testing.T) {
v := New()
assert.Equal(t, 15, len(v)) // 15 is the total number of views have to be returned
}

func TestViews_Configure(t *testing.T) {
testcases := []struct {
version int
trackCommit string
}{
{version: 130000, trackCommit: "on"},
{version: 130000, trackCommit: "off"},
{version: 120000, trackCommit: "on"},
{version: 120000, trackCommit: "off"},
{version: 110000, trackCommit: "on"},
{version: 110000, trackCommit: "off"},
{version: 90600, trackCommit: "on"},
{version: 90600, trackCommit: "off"},
{version: 90500, trackCommit: "on"},
{version: 90500, trackCommit: "off"},
{version: 90400, trackCommit: "on"},
{version: 90400, trackCommit: "off"},
}

for _, tc := range testcases {
views := New()
views.Configure(tc.version, tc.trackCommit)

switch tc.version {
case 130000:
if tc.trackCommit == "on" {
assert.Equal(t, query.PgStatReplicationExtended, views["replication"].QueryTmpl)
assert.Equal(t, 17, views["replication"].Ncols)
} else {
assert.Equal(t, query.PgStatReplicationDefault, views["replication"].QueryTmpl)
}
case 120000:
if tc.trackCommit == "on" {
assert.Equal(t, query.PgStatReplicationExtended, views["replication"].QueryTmpl)
assert.Equal(t, 17, views["replication"].Ncols)
} else {
assert.Equal(t, query.PgStatReplicationDefault, views["replication"].QueryTmpl)
}
assert.Equal(t, query.PgStatStatementsTiming12, views["statements_timings"].QueryTmpl)
case 110000:
if tc.trackCommit == "on" {
assert.Equal(t, query.PgStatReplicationExtended, views["replication"].QueryTmpl)
assert.Equal(t, 17, views["replication"].Ncols)
} else {
assert.Equal(t, query.PgStatReplicationDefault, views["replication"].QueryTmpl)
}
assert.Equal(t, query.PgStatDatabase11, views["databases"].QueryTmpl)
assert.Equal(t, 17, views["databases"].Ncols)
assert.Equal(t, [2]int{1, 15}, views["databases"].DiffIntvl)
case 90600:
if tc.trackCommit == "on" {
assert.Equal(t, query.PgStatReplication96Extended, views["replication"].QueryTmpl)
assert.Equal(t, 14, views["replication"].Ncols)
} else {
assert.Equal(t, query.PgStatReplication96, views["replication"].QueryTmpl)
assert.Equal(t, 12, views["replication"].Ncols)
}
assert.Equal(t, query.PgStatActivity96, views["activity"].QueryTmpl)
assert.Equal(t, 13, views["activity"].Ncols)
case 90500:
assert.Equal(t, query.PgStatActivity95, views["activity"].QueryTmpl)
assert.Equal(t, 12, views["activity"].Ncols)
case 90400:
assert.Equal(t, query.PgStatReplication96, views["replication"].QueryTmpl)
assert.Equal(t, 12, views["activity"].Ncols)
}
}
}

0 comments on commit d47b2e4

Please sign in to comment.