|
| 1 | +package organization |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + orgv1 "github.com/appuio/control-api/apis/organization/v1" |
| 11 | + |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | +) |
| 16 | + |
| 17 | +func TestOrganizationStorage_ConvertToTable(t *testing.T) { |
| 18 | + tests := map[string]struct { |
| 19 | + obj runtime.Object |
| 20 | + tableOptions runtime.Object |
| 21 | + fail bool |
| 22 | + nrRows int |
| 23 | + }{ |
| 24 | + "GivenEmptyOrg_ThenSingleRow": { |
| 25 | + obj: &orgv1.Organization{}, |
| 26 | + nrRows: 1, |
| 27 | + }, |
| 28 | + "GivenOrg_ThenSingleRow": { |
| 29 | + obj: &orgv1.Organization{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{Name: "foo"}, |
| 31 | + Spec: orgv1.OrganizationSpec{ |
| 32 | + DisplayName: "bar", |
| 33 | + }, |
| 34 | + }, |
| 35 | + nrRows: 1, |
| 36 | + }, |
| 37 | + "GivenOrgList_ThenMultipleRow": { |
| 38 | + obj: &orgv1.OrganizationList{ |
| 39 | + Items: []orgv1.Organization{ |
| 40 | + {}, |
| 41 | + {}, |
| 42 | + {}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + nrRows: 3, |
| 46 | + }, |
| 47 | + "GivenNil_ThenFail": { |
| 48 | + obj: nil, |
| 49 | + fail: true, |
| 50 | + }, |
| 51 | + "GivenNonOrg_ThenFail": { |
| 52 | + obj: &corev1.Pod{}, |
| 53 | + fail: true, |
| 54 | + }, |
| 55 | + "GivenNonOrgList_ThenFail": { |
| 56 | + obj: &corev1.PodList{}, |
| 57 | + fail: true, |
| 58 | + }, |
| 59 | + } |
| 60 | + orgStore := &organizationStorage{} |
| 61 | + for name, tc := range tests { |
| 62 | + t.Run(name, func(t *testing.T) { |
| 63 | + table, err := orgStore.ConvertToTable(context.TODO(), tc.obj, tc.tableOptions) |
| 64 | + if tc.fail { |
| 65 | + require.Error(t, err) |
| 66 | + return |
| 67 | + } |
| 68 | + require.NoError(t, err) |
| 69 | + assert.Len(t, table.Rows, tc.nrRows) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments