|
| 1 | +package managers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Masterminds/squirrel" |
| 11 | + dbtest "github.com/contiamo/go-base/pkg/db/test" |
| 12 | + "github.com/contiamo/go-base/pkg/http/parameters" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +const testTableName = "base_manager_test_table" |
| 17 | + |
| 18 | +func TestGetPageInfo(t *testing.T) { |
| 19 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 20 | + defer cancel() |
| 21 | + _, db := dbtest.GetDatabase(t, createFixture) |
| 22 | + defer db.Close() |
| 23 | + |
| 24 | + cases := []struct { |
| 25 | + name string |
| 26 | + table string |
| 27 | + page parameters.Page |
| 28 | + scope squirrel.Sqlizer |
| 29 | + filter squirrel.Sqlizer |
| 30 | + expected PageInfo |
| 31 | + expErr string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "Returns the valid page info when the scope and filter are set", |
| 35 | + table: testTableName, |
| 36 | + page: parameters.Page{Number: 1, Size: 5}, |
| 37 | + scope: squirrel.Eq{"organization_id": 1}, |
| 38 | + filter: squirrel.Eq{"cats_involved": true}, |
| 39 | + expected: PageInfo{ |
| 40 | + ItemsPerPage: 5, |
| 41 | + Current: 1, |
| 42 | + UnfilteredItemCount: 8, |
| 43 | + ItemCount: 4, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Returns the valid page info when only the scope is set", |
| 48 | + table: testTableName, |
| 49 | + page: parameters.Page{Number: 1, Size: 5}, |
| 50 | + scope: squirrel.Eq{"organization_id": 1}, |
| 51 | + expected: PageInfo{ |
| 52 | + ItemsPerPage: 5, |
| 53 | + Current: 1, |
| 54 | + UnfilteredItemCount: 8, |
| 55 | + ItemCount: 8, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Returns the valid page info when only the filter is set", |
| 60 | + table: testTableName, |
| 61 | + page: parameters.Page{Number: 1, Size: 5}, |
| 62 | + filter: squirrel.Eq{"cats_involved": true}, |
| 63 | + expected: PageInfo{ |
| 64 | + ItemsPerPage: 5, |
| 65 | + Current: 1, |
| 66 | + UnfilteredItemCount: 16, |
| 67 | + ItemCount: 8, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Returns the valid page info when neither filter nor scope is set", |
| 72 | + table: testTableName, |
| 73 | + page: parameters.Page{Number: 1, Size: 5}, |
| 74 | + expected: PageInfo{ |
| 75 | + ItemsPerPage: 5, |
| 76 | + Current: 1, |
| 77 | + UnfilteredItemCount: 16, |
| 78 | + ItemCount: 16, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "Returns error when the table name is wrong", |
| 83 | + table: "wrong", |
| 84 | + page: parameters.Page{Number: 1, Size: 5}, |
| 85 | + expErr: "pq: relation \"wrong\" does not exist", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range cases { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + m := NewBaseManager(db, "test") |
| 92 | + info, err := m.GetPageInfo(ctx, tc.table, tc.page, tc.scope, tc.filter) |
| 93 | + if tc.expErr != "" { |
| 94 | + require.Error(t, err) |
| 95 | + require.Equal(t, tc.expErr, err.Error()) |
| 96 | + return |
| 97 | + } |
| 98 | + require.NoError(t, err) |
| 99 | + require.Equal(t, tc.expected, info) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func createFixture(ctx context.Context, db *sql.DB) error { |
| 105 | + _, err := db.ExecContext(ctx, "CREATE TABLE "+testTableName+`( |
| 106 | + id integer PRIMARY KEY, |
| 107 | + organization_id integer NOT NULL, |
| 108 | + name text NOT NULL, |
| 109 | + description text NOT NULL, |
| 110 | + cats_involved boolean NOT NULL);`) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + builder := squirrel.StatementBuilder. |
| 117 | + PlaceholderFormat(squirrel.Dollar). |
| 118 | + RunWith(db). |
| 119 | + Insert(testTableName). |
| 120 | + Columns("id", "organization_id", "name", "description", "cats_involved") |
| 121 | + |
| 122 | + // 2 orgs with 8 values in each: 4 where cats are not involved and 4 where they are |
| 123 | + for i := 0; i < 2; i++ { |
| 124 | + for j := 0; j < 4; j++ { |
| 125 | + builder = builder.Values( |
| 126 | + 10*i+j, |
| 127 | + i, |
| 128 | + fmt.Sprintf("name-%d", j), |
| 129 | + fmt.Sprintf("decr-%d", j), |
| 130 | + true, |
| 131 | + ) |
| 132 | + } |
| 133 | + for j := 4; j < 8; j++ { |
| 134 | + builder = builder.Values( |
| 135 | + 10*i+j, |
| 136 | + i, |
| 137 | + fmt.Sprintf("name-%d", j), |
| 138 | + fmt.Sprintf("decr-%d", j), |
| 139 | + false, |
| 140 | + ) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + _, err = builder.ExecContext(ctx) |
| 145 | + |
| 146 | + return err |
| 147 | +} |
0 commit comments