|
| 1 | +package snapshot_browser |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | + "zfs-file-history/internal/data" |
| 7 | + "zfs-file-history/internal/data/diff_state" |
| 8 | + "zfs-file-history/internal/ui/table" |
| 9 | + "zfs-file-history/internal/zfs" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCreateSnapshotBrowserTableCells_ClonesColumn(t *testing.T) { |
| 15 | + entry := &data.SnapshotBrowserEntry{ |
| 16 | + Snapshot: &zfs.Snapshot{ |
| 17 | + Name: "snap-a", |
| 18 | + Properties: zfs.SnapshotProperties{ |
| 19 | + Clones: 42, |
| 20 | + }, |
| 21 | + }, |
| 22 | + DiffState: diff_state.Unknown, |
| 23 | + } |
| 24 | + |
| 25 | + cells := createSnapshotBrowserTableCells(0, []*table.Column{columnClones}, entry) |
| 26 | + |
| 27 | + if assert.Len(t, cells, 1) { |
| 28 | + assert.Equal(t, "42", cells[0].Text) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestCreateSnapshotBrowserTableSortFunction_ClonesAscendingAndDescending(t *testing.T) { |
| 33 | + entries := []*data.SnapshotBrowserEntry{ |
| 34 | + newSnapshotEntryWithClones("low", 0), |
| 35 | + newSnapshotEntryWithClones("high", math.MaxUint64), |
| 36 | + newSnapshotEntryWithClones("mid", 1), |
| 37 | + } |
| 38 | + |
| 39 | + ascending := append([]*data.SnapshotBrowserEntry{}, entries...) |
| 40 | + createSnapshotBrowserTableSortFunction(ascending, columnClones, false) |
| 41 | + assert.Equal(t, []string{"low", "mid", "high"}, snapshotNames(ascending)) |
| 42 | + |
| 43 | + descending := append([]*data.SnapshotBrowserEntry{}, entries...) |
| 44 | + createSnapshotBrowserTableSortFunction(descending, columnClones, true) |
| 45 | + assert.Equal(t, []string{"high", "mid", "low"}, snapshotNames(descending)) |
| 46 | +} |
| 47 | + |
| 48 | +func newSnapshotEntryWithClones(name string, clones uint64) *data.SnapshotBrowserEntry { |
| 49 | + return &data.SnapshotBrowserEntry{ |
| 50 | + Snapshot: &zfs.Snapshot{ |
| 51 | + Name: name, |
| 52 | + Properties: zfs.SnapshotProperties{ |
| 53 | + Clones: clones, |
| 54 | + }, |
| 55 | + }, |
| 56 | + DiffState: diff_state.Unknown, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func snapshotNames(entries []*data.SnapshotBrowserEntry) []string { |
| 61 | + result := make([]string, 0, len(entries)) |
| 62 | + for _, entry := range entries { |
| 63 | + result = append(result, entry.Snapshot.Name) |
| 64 | + } |
| 65 | + return result |
| 66 | +} |
0 commit comments