Skip to content

Commit

Permalink
add initial set of unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <[email protected]>
  • Loading branch information
everettraven committed Jan 5, 2024
1 parent 2c9996a commit 8830ece
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
buoy
dist/
dist/

cover.out
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ GOLANGCI_LINT_ARGS ?=
lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT) run $(GOLANGCI_LINT_ARGS)
unit:
go test ./...
go test ./... -coverprofile=cover.out -covermode=atomic

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/sahilm/fuzzy v0.1.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
github.com/tidwall/gjson v1.17.0
k8s.io/api v0.28.1
k8s.io/apimachinery v0.28.1
Expand All @@ -21,6 +22,7 @@ require (
require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
Expand Down
36 changes: 36 additions & 0 deletions pkg/charm/models/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package models

import (
"testing"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/models/panels"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/everettraven/buoy/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestDashboardUpdate(t *testing.T) {
panels := []tea.Model{
panels.NewItem(types.Item{
PanelBase: types.PanelBase{
Name: "test",
},
}, viewport.New(10, 10), styles.Theme{}),
}

d := NewDashboard(DefaultDashboardKeys, styles.Theme{}, panels...)

t.Log("WindowSizeUpdate")
d.Update(tea.WindowSizeMsg{Width: 50, Height: 50})
assert.Equal(t, 50, d.width)

t.Log("toggle detailed help")
d.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("ctrl+h")})
assert.True(t, d.help.ShowAll)

t.Log("quit the program")
_, cmd := d.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
assert.Equal(t, cmd(), tea.Quit())
}
32 changes: 32 additions & 0 deletions pkg/charm/models/panels/item_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package panels

import (
"errors"
"testing"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/everettraven/buoy/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestItemUpdate(t *testing.T) {
item := NewItem(types.Item{}, viewport.New(10, 10), styles.Theme{})
item.Update(tea.WindowSizeMsg{Width: 50, Height: 50})
assert.Equal(t, 50, item.viewport.Width)
assert.Equal(t, 25, item.viewport.Height)
}

func TestItemViewWithError(t *testing.T) {
item := NewItem(types.Item{}, viewport.New(10, 10), styles.Theme{})
err := errors.New("some error")
item.SetError(err)
assert.Equal(t, err.Error(), item.View())
}

func TestViewWithContent(t *testing.T) {
item := NewItem(types.Item{}, viewport.New(50, 50), styles.Theme{})
item.SetContent("some content")
assert.Contains(t, item.View(), "some content")
}
89 changes: 89 additions & 0 deletions pkg/charm/models/panels/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package panels

import (
"errors"
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/stretchr/testify/assert"
)

func TestEnterSearchMode(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
assert.Equal(t, logs.mode, modeSearching)
}

func TestExecuteSearch(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
logs.Update(tea.KeyMsg{Type: tea.KeyEnter})
assert.Equal(t, logs.mode, modeSearched)
}

func TestExitSearchMode(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.mode = modeSearching
logs.searchbar.Focus()
logs.Update(tea.KeyMsg{Type: tea.KeyEsc})
assert.Equal(t, logs.mode, modeLogs)
assert.False(t, logs.searchbar.Focused())

logs.mode = modeSearched
logs.searchbar.Focus()
logs.Update(tea.KeyMsg{Type: tea.KeyEsc})
assert.Equal(t, logs.mode, modeLogs)
assert.False(t, logs.searchbar.Focused())
}

func TestSearchModeToggle(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
logs.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
assert.True(t, logs.strictSearch)
logs.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
assert.False(t, logs.strictSearch)
}

func TestSearchLogs(t *testing.T) {
t.Log("strict search")
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.strictSearch = true
logs.content = "some log line\nlog line with a search term\n"
logs.viewport.Width = 50
logs.searchbar.SetValue("search")
match := logs.searchLogs()
assert.Equal(t, "log line with a search term\n", match)

t.Log("fuzzy search")
logs.searchbar.SetValue("sll")
logs.strictSearch = false
match = logs.searchLogs()
assert.Equal(t, "some log line\n", match)
}

func TestLogsWindowSizeUpdate(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.Update(tea.WindowSizeMsg{Width: 100, Height: 100})
assert.Equal(t, logs.viewport.Width, 100)
assert.Equal(t, logs.viewport.Height, 50)
}

func TestLogsAddContent(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
logs.AddContent("some log line\n")
assert.Equal(t, "\nsome log line\n", logs.content)
assert.True(t, logs.contentUpdated)

logs.Update(tea.KeyMsg{Type: tea.KeyDown})
assert.False(t, logs.contentUpdated)
}

func TestLogsViewWithError(t *testing.T) {
logs := NewLogs(DefaultLogsKeys, nil, styles.Theme{})
err := errors.New("some error")
logs.SetError(err)
assert.Equal(t, err, logs.err)
assert.Equal(t, err.Error(), logs.View())
}
6 changes: 5 additions & 1 deletion pkg/charm/models/panels/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (m *Table) View() string {
case modeView:
return m.viewport.View()
default:
return "?"
return fmt.Sprintf("unknown table state. table.mode=%q", m.mode)
}
}

Expand Down Expand Up @@ -234,6 +234,10 @@ func (m *Table) FetchContentForIndex(index int) (string, error) {
}
}

if rowInfo == nil {
return "", fmt.Errorf("no row data found for selected row %d", index)
}

name := rowInfo.Identifier.String()
if m.scope == meta.RESTScopeNameRoot {
name = rowInfo.Identifier.Name
Expand Down
105 changes: 105 additions & 0 deletions pkg/charm/models/panels/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package panels

import (
"errors"
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/styles"
buoytypes "github.com/everettraven/buoy/pkg/types"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

func TestTableUpdate(t *testing.T) {
t.Log("WindowSizeUpdate")
table := NewTable(DefaultTableKeys, &buoytypes.Table{}, styles.Theme{})
table.Update(tea.WindowSizeMsg{Width: 50, Height: 50})
assert.Equal(t, 50, table.viewport.Width)
assert.Equal(t, 25, table.viewport.Height)

t.Log("toggle view mode on")
table.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("v")})
assert.Equal(t, modeView, table.mode)

t.Log("toggle view mode off")
table.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("v")})
assert.Equal(t, modeTable, table.mode)
}

func TestAddOrUpdate(t *testing.T) {
table := NewTable(DefaultTableKeys, &buoytypes.Table{
Columns: []buoytypes.Column{
{Header: "Name", Width: 10, Path: "metadata.name"},
},
}, styles.Theme{})

t.Log("add a row")
u := &unstructured.Unstructured{}
u.SetName("test")
u.SetNamespace("test-ns")
u.SetUID(types.UID("test"))
table.AddOrUpdate(u)
assert.Len(t, table.rows, 1)
assert.NotNil(t, table.rows[types.UID("test")].Row)
assert.Equal(t, &types.NamespacedName{Namespace: "test-ns", Name: "test"}, table.rows[types.UID("test")].Identifier)

t.Log("update a row")
u.SetName("test2")
table.AddOrUpdate(u)
assert.Len(t, table.rows, 1)
assert.NotNil(t, table.rows[types.UID("test")].Row)
assert.Equal(t, &types.NamespacedName{Namespace: "test-ns", Name: "test2"}, table.rows[types.UID("test")].Identifier)
}

func TestDeleteRow(t *testing.T) {
table := NewTable(DefaultTableKeys, &buoytypes.Table{
Columns: []buoytypes.Column{
{Header: "Name", Width: 10, Path: "metadata.name"},
},
}, styles.Theme{})

t.Log("add a row")
u := &unstructured.Unstructured{}
u.SetName("test")
u.SetNamespace("test-ns")
u.SetUID(types.UID("test"))
table.AddOrUpdate(u)
assert.Len(t, table.rows, 1)

t.Log("delete a row")
table.DeleteRow(types.UID("test"))
assert.Len(t, table.rows, 0)
}

func TestTableView(t *testing.T) {
table := NewTable(DefaultTableKeys, &buoytypes.Table{}, styles.Theme{})

t.Log("view with error state")
err := errors.New("some error")
table.SetError(err)
assert.Equal(t, err.Error(), table.View())

t.Log("view with table mode")
table.SetError(nil)
table.mode = modeTable
assert.Equal(t, table.tableModel.View(), table.View())

t.Log("view with view mode toggled on")
table.mode = modeView
table.viewport.SetContent("floof")
assert.Equal(t, table.viewport.View(), table.View())
}

func TestGetDotNotationValueNonExistentValue(t *testing.T) {
obj := map[string]interface{}{
"foo": map[string]interface{}{
"bar": "baz",
},
}

val, err := getDotNotationValue(obj, "foo.baz")
assert.NoError(t, err)
assert.Equal(t, "n/a", val)
}
29 changes: 29 additions & 0 deletions pkg/charm/models/tabber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package models

import (
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/stretchr/testify/assert"
)

func TestTabberUpdate(t *testing.T) {
tabber := NewTabber(DefaultTabberKeys, styles.Theme{}, Tab{Name: "test", Model: nil}, Tab{Name: "test2", Model: nil})

t.Log("navigate to next tab")
tabber.Update(tea.KeyMsg{Type: tea.KeyTab})
assert.Equal(t, 1, tabber.selected)

t.Log("navigate to previous tab")
tabber.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("shift+tab")})
assert.Equal(t, 0, tabber.selected)

t.Log("navigate to previous tab (out of bounds -> last tab)")
tabber.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("shift+tab")})
assert.Equal(t, 1, tabber.selected)

t.Log("navigate to next tab (out of bounds -> first tab)")
tabber.Update(tea.KeyMsg{Type: tea.KeyTab})
assert.Equal(t, 0, tabber.selected)
}
Loading

0 comments on commit 8830ece

Please sign in to comment.