|
| 1 | +local stubs = require('enapter.ucm.stubs') |
| 2 | + |
| 3 | +_G.inspect = require('inspect') |
| 4 | + |
| 5 | +describe('generic can', function() |
| 6 | + local can, ucm_stubs, cursor |
| 7 | + |
| 8 | + local function prepare_peer_stub() |
| 9 | + local peer_stub = stubs.new_dummy_ucm_peer() |
| 10 | + ucm_stubs.stubs = { peer_stub } |
| 11 | + peer_stub.execute_command = function() return 'completed', { cursor = cursor } end |
| 12 | + return peer_stub |
| 13 | + end |
| 14 | + |
| 15 | + before_each(function() |
| 16 | + ucm_stubs = stubs.setup_enapter_ucm() |
| 17 | + |
| 18 | + package.loaded['enapter.ucm.generics.can'] = false |
| 19 | + can = require('enapter.ucm.generics.can').new() |
| 20 | + can:setup('test_ucm_id', {}) |
| 21 | + end) |
| 22 | + |
| 23 | + after_each(function() |
| 24 | + stubs.teardown_generic_can() |
| 25 | + stubs.teardown_enapter_ucm() |
| 26 | + end) |
| 27 | + |
| 28 | + it('shoud execute read command when setup', function() |
| 29 | + local messages = { |
| 30 | + { msg_id = 0x123 }, |
| 31 | + { msg_id = 0x789 }, |
| 32 | + } |
| 33 | + local timeout = math.random(5000) |
| 34 | + |
| 35 | + local peer_stub = prepare_peer_stub() |
| 36 | + local s = spy.on(peer_stub, 'execute_command') |
| 37 | + assert.is_nil(can:setup('', { test_cmd = messages }, timeout)) |
| 38 | + assert.spy(s).was_called(1) |
| 39 | + assert |
| 40 | + .spy(s) |
| 41 | + .was_called_with(peer_stub, 'read', match.same({ msg_ids = { 0x123, 0x789 } }), { timeout = timeout }) |
| 42 | + end) |
| 43 | + |
| 44 | + it('should store cursor', function() |
| 45 | + cursor = math.random(100) |
| 46 | + assert.is_nil(can:setup('', { test_cur = {} })) |
| 47 | + |
| 48 | + local peer_stub = prepare_peer_stub() |
| 49 | + |
| 50 | + local s = spy.on(peer_stub, 'execute_command') |
| 51 | + local _, err = can:get('test_cur') |
| 52 | + assert.is_nil(err) |
| 53 | + assert.spy(s).was_called(1) |
| 54 | + assert |
| 55 | + .spy(s) |
| 56 | + .was_called_with(peer_stub, 'read', match.same({ msg_ids = {} }), { timeout = 1000 }) |
| 57 | + |
| 58 | + peer_stub = prepare_peer_stub() |
| 59 | + local s2 = spy.on(peer_stub, 'execute_command') |
| 60 | + _, err = can:get('test_cur') |
| 61 | + assert.is_nil(err) |
| 62 | + assert.spy(s2).was_called(1) |
| 63 | + assert |
| 64 | + .spy(s2) |
| 65 | + .was_called_with(peer_stub, 'read', match.same({ cursor = cursor, msg_ids = {} }), { timeout = 1000 }) |
| 66 | + end) |
| 67 | + |
| 68 | + it('should not get for unknow subscription', function() |
| 69 | + local _, err = can:get('unknown') |
| 70 | + assert.is.equals("subscritpion with name 'unknown' is not exists", err) |
| 71 | + end) |
| 72 | + |
| 73 | + it('should handle error from generic can io', function() |
| 74 | + assert.is_nil(can:setup('', { test_can_error = {} })) |
| 75 | + |
| 76 | + local peer_stub = prepare_peer_stub() |
| 77 | + peer_stub.execute_command = function() return 'completed', nil, 'can error' end |
| 78 | + |
| 79 | + local _, err = can:get('test_can_error') |
| 80 | + assert.is.equals('command failed: can error', err) |
| 81 | + end) |
| 82 | + |
| 83 | + it('should handle non-completed state from generic can io', function() |
| 84 | + assert.is_nil(can:setup('', { test_non_completed = {} })) |
| 85 | + |
| 86 | + local peer_stub = prepare_peer_stub() |
| 87 | + peer_stub.execute_command = function() return 'non-completed', { errmsg = 'error msg' } end |
| 88 | + |
| 89 | + local _, err = can:get('test_non_completed') |
| 90 | + assert.is.equals('command failed: non-completed: {errmsg = "error msg"}', err) |
| 91 | + end) |
| 92 | + |
| 93 | + it('should get with name and names', function() |
| 94 | + local messages = { |
| 95 | + { |
| 96 | + name = 't_name', |
| 97 | + msg_id = 0x318, |
| 98 | + parser = function() return 'test name' end, |
| 99 | + }, |
| 100 | + { |
| 101 | + names = { 'n_1', 'n_2' }, |
| 102 | + msg_id = 0x418, |
| 103 | + parser = function() return { 1, 2 } end, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + assert.is_nil(can:setup('', { test_names = messages })) |
| 108 | + |
| 109 | + local peer_stub = prepare_peer_stub() |
| 110 | + peer_stub.execute_command = function() return 'completed', { results = { { 'r1' }, { 'r2' } } } end |
| 111 | + |
| 112 | + local ret, err = can:get('test_names') |
| 113 | + assert.is_nil(err) |
| 114 | + |
| 115 | + assert.is_same({ t_name = 'test name', n_1 = 1, n_2 = 2 }, ret) |
| 116 | + end) |
| 117 | + |
| 118 | + it('should differ pass data to multi_msg', function() |
| 119 | + local test_data = { 'r1', 'r2' } |
| 120 | + local messages = { |
| 121 | + { |
| 122 | + name = 't_multi', |
| 123 | + msg_id = 0x318, |
| 124 | + multi_msg = true, |
| 125 | + parser = function(datas) return datas[1] .. datas[2] end, |
| 126 | + }, |
| 127 | + { |
| 128 | + name = 't_single', |
| 129 | + msg_id = 0x418, |
| 130 | + parser = function(data) return data end, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + assert.is_nil(can:setup('', { test_multi = messages })) |
| 135 | + |
| 136 | + local peer_stub = prepare_peer_stub() |
| 137 | + peer_stub.execute_command = function() |
| 138 | + return 'completed', { results = { test_data, test_data } } |
| 139 | + end |
| 140 | + |
| 141 | + local ret, err = can:get('test_multi') |
| 142 | + assert.is_nil(err) |
| 143 | + |
| 144 | + assert.is_same({ t_multi = 'r1r2', t_single = 'r2' }, ret) |
| 145 | + end) |
| 146 | + |
| 147 | + it('should handle errors from parser function', function() |
| 148 | + local messages = { |
| 149 | + { |
| 150 | + name = 'fail', |
| 151 | + msg_id = 0x404, |
| 152 | + parser = function() error('not found') end, |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + assert.is_nil(can:setup('', { test_names = messages })) |
| 157 | + |
| 158 | + local peer_stub = prepare_peer_stub() |
| 159 | + peer_stub.execute_command = function() return 'completed', { results = { { 'any data' } } } end |
| 160 | + |
| 161 | + local _, err = can:get('test_names') |
| 162 | + assert.is_equals( |
| 163 | + 'data processing failed [msg_id=0x404 data={97 110 121 32 100 97 116 97 }]: <place>: not found', |
| 164 | + err:gsub('spec/generic_can_spec.lua:%d+', '<place>') |
| 165 | + ) |
| 166 | + end) |
| 167 | +end) |
0 commit comments