|
1 | | -package box |
| 1 | +package box_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "errors" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/stretchr/testify/assert" |
7 | 9 | "github.com/stretchr/testify/require" |
8 | | - |
| 10 | + "github.com/tarantool/go-tarantool/v2/box" |
9 | 11 | "github.com/tarantool/go-tarantool/v2/test_helpers" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func TestNew(t *testing.T) { |
13 | 15 | t.Parallel() |
14 | 16 |
|
15 | | - // Create a box instance with a nil connection. This should lead to a panic later. |
16 | | - b := New(nil) |
| 17 | + // Create a box instance with a nil connection. This should lead to a panic. |
| 18 | + require.Panics(t, func() { box.New(nil) }) |
| 19 | +} |
| 20 | + |
| 21 | +func TestMocked(t *testing.T) { |
| 22 | + t.Run("Box.Info", func(t *testing.T) { |
| 23 | + t.Parallel() |
17 | 24 |
|
18 | | - // Ensure the box instance is not nil (which it shouldn't be), but this is not meaningful |
19 | | - // since we will panic when we call the Info method with the nil connection. |
20 | | - require.NotNil(t, b) |
| 25 | + data := []interface{}{ |
| 26 | + map[string]interface{}{ |
| 27 | + "version": "1.0.0", |
| 28 | + "id": nil, |
| 29 | + "ro": false, |
| 30 | + "uuid": "uuid", |
| 31 | + "pid": 456, |
| 32 | + "status": "status", |
| 33 | + "lsn": 123, |
| 34 | + "replication": nil, |
| 35 | + }, |
| 36 | + } |
| 37 | + mock := test_helpers.NewMockDoer(t, |
| 38 | + test_helpers.NewMockResponse(t, data), |
| 39 | + ) |
| 40 | + b := box.New(&mock) |
21 | 41 |
|
22 | | - // We expect a panic because we are passing a nil connection (nil Doer) to the By function. |
23 | | - // The library does not control this zone, and the nil connection would cause a runtime error |
24 | | - // when we attempt to call methods (like Info) on it. |
25 | | - // This test ensures that such an invalid state is correctly handled by causing a panic, |
26 | | - // as it's outside the library's responsibility. |
27 | | - require.Panics(t, func() { |
| 42 | + info, err := b.Info() |
| 43 | + require.NoError(t, err) |
28 | 44 |
|
29 | | - // Calling Info on a box with a nil connection will result in a panic, since the underlying |
30 | | - // connection (Doer) cannot perform the requested action (it's nil). |
31 | | - _, _ = b.Info() |
| 45 | + assert.Equal(t, "1.0.0", info.Version) |
| 46 | + assert.Equal(t, 456, info.PID) |
32 | 47 | }) |
33 | | -} |
34 | 48 |
|
35 | | -func TestNew_Schema_PassesDoer(t *testing.T) { |
36 | | - t.Parallel() |
| 49 | + t.Run("Box.Schema.User.Info", func(t *testing.T) { |
| 50 | + t.Parallel() |
37 | 51 |
|
38 | | - doer := test_helpers.NewMockDoer(t) |
| 52 | + data := []interface{}{ |
| 53 | + []interface{}{ |
| 54 | + []interface{}{"read,write,execute", "universe", ""}, |
| 55 | + }, |
| 56 | + } |
| 57 | + mock := test_helpers.NewMockDoer(t, |
| 58 | + test_helpers.NewMockResponse(t, data), |
| 59 | + ) |
| 60 | + b := box.New(&mock) |
39 | 61 |
|
40 | | - b := New(&doer) |
41 | | - schemaObject := b.Schema() |
42 | | - assert.Equal(t, &doer, schemaObject.conn) |
| 62 | + privs, err := b.Schema().User().Info(context.Background(), "username") |
| 63 | + require.NoError(t, err) |
43 | 64 |
|
44 | | - userSchemaObject := b.Schema().User() |
45 | | - assert.Equal(t, &doer, userSchemaObject.conn) |
| 65 | + assert.Equal(t, []box.Privilege{ |
| 66 | + { |
| 67 | + Permissions: []box.Permission{ |
| 68 | + box.PermissionRead, |
| 69 | + box.PermissionWrite, |
| 70 | + box.PermissionExecute, |
| 71 | + }, |
| 72 | + Type: box.PrivilegeUniverse, |
| 73 | + Name: "", |
| 74 | + }, |
| 75 | + }, privs) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("Box.Session.Su", func(t *testing.T) { |
| 79 | + t.Parallel() |
46 | 80 |
|
47 | | - sessionObject := b.Session() |
48 | | - assert.Equal(t, &doer, sessionObject.conn) |
| 81 | + mock := test_helpers.NewMockDoer(t, |
| 82 | + test_helpers.NewMockResponse(t, []interface{}{}), |
| 83 | + errors.New("user not found or supplied credentials are invalid"), |
| 84 | + ) |
| 85 | + b := box.New(&mock) |
| 86 | + |
| 87 | + err := b.Session().Su(context.Background(), "admin") |
| 88 | + require.NoError(t, err) |
| 89 | + }) |
49 | 90 | } |
0 commit comments