test_interface.py 20.9 KB
Newer Older
Vincent Michel's avatar
Vincent Michel committed
1
import pytest
Vincent Michel's avatar
Vincent Michel committed
2
import numpy
Vincent Michel's avatar
Vincent Michel committed
3
4
5
6
7
8
import mock


@pytest.fixture
def interface():
    with mock.patch('cffi.FFI.dlopen') as dlopen:
Vincent Michel's avatar
Vincent Michel committed
9
10
11
12
        with mock.patch('handel.interface.check_error'):
            from handel import interface
            interface.handel = dlopen.return_value
            yield interface
Vincent Michel's avatar
Vincent Michel committed
13
14


Vincent Michel's avatar
Vincent Michel committed
15
16
# Initializing handel

Vincent Michel's avatar
Vincent Michel committed
17
18
19
def test_init(interface):
    m = interface.handel.xiaInit
    m.return_value = 0
Vincent Michel's avatar
Vincent Michel committed
20
    assert interface.init(u'somefile') is None
21
    m.assert_called_once_with(b'somefile')
Vincent Michel's avatar
Vincent Michel committed
22
23
24
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
    # Reset
25
    m.reset_mock()
Vincent Michel's avatar
Vincent Michel committed
26
    interface.check_error.reset_mock()
27
28
29
    # Should work with bytes too
    assert interface.init(b'somefile') is None
    m.assert_called_once_with(b'somefile')
Vincent Michel's avatar
Vincent Michel committed
30
31
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
32
33
34
35
36
37


def test_init_handel(interface):
    m = interface.handel.xiaInitHandel
    m.return_value = 0
    assert interface.init_handel() is None
Vincent Michel's avatar
Vincent Michel committed
38
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
39
40
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
41
42
43
44
45
46
47


def test_exit(interface):
    m = interface.handel.xiaExit
    m.return_value = 0
    assert interface.exit() is None
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
48
49
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
50
51


Vincent Michel's avatar
Vincent Michel committed
52
53
# Detectors

Vincent Michel's avatar
Vincent Michel committed
54
55
56
57
def test_new_detector(interface):
    m = interface.handel.xiaNewDetector
    m.return_value = 0
    assert interface.new_detector('somealias') is None
58
    m.assert_called_once_with(b'somealias')
Vincent Michel's avatar
Vincent Michel committed
59
60
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74


def test_get_num_detectors(interface):
    m = interface.handel.xiaGetNumDetectors

    def side_effect(arg):
        arg[0] = 3
        return 0

    m.side_effect = side_effect
    assert interface.get_num_detectors() == 3
    m.assert_called_once()
    arg = m.call_args[0][0]
    m.assert_called_once_with(arg)
Vincent Michel's avatar
Vincent Michel committed
75
76
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94


def test_get_detectors(interface):
    m1 = interface.handel.xiaGetNumDetectors
    m2 = interface.handel.xiaGetDetectors

    def side_effect_1(arg):
        arg[0] = 3
        return 0

    def side_effect_2(lst):
        lst[0][0:5] = b'name1'
        lst[1][0:5] = b'name2'
        lst[2][0:5] = b'name3'
        return 0

    m1.side_effect = side_effect_1
    m2.side_effect = side_effect_2
95
    expected = 'name1', 'name2', 'name3'
Vincent Michel's avatar
Vincent Michel committed
96
97
98
99
    assert interface.get_detectors() == expected
    m2.assert_called_once()
    arg = m2.call_args[0][0]
    m2.assert_called_once_with(arg)
Vincent Michel's avatar
Vincent Michel committed
100
101
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)
Vincent Michel's avatar
Vincent Michel committed
102
103


Vincent Michel's avatar
Vincent Michel committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def test_get_detector_from_channel(interface):
    m = interface.handel.xiaDetectorFromDetChan

    def side_effect(channel, alias):
        alias[0:5] = b'name1'
        return 0

    m.side_effect = side_effect
    assert interface.get_detector_from_channel(1) == 'name1'
    arg = m.call_args[0][1]
    m.assert_called_once_with(1, arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


Vincent Michel's avatar
Vincent Michel committed
119
120
121
122
123
# Run control

def test_start_run(interface):
    m = interface.handel.xiaStartRun
    m.return_value = 0
Vincent Michel's avatar
Vincent Michel committed
124
    # First test
Vincent Michel's avatar
Vincent Michel committed
125
126
    assert interface.start_run(1) is None
    m.assert_called_once_with(1, False)
Vincent Michel's avatar
Vincent Michel committed
127
128
129
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
    # Reset
Vincent Michel's avatar
Vincent Michel committed
130
    m.reset_mock()
Vincent Michel's avatar
Vincent Michel committed
131
132
    interface.check_error.reset_mock()
    # Second test
Vincent Michel's avatar
Vincent Michel committed
133
134
    assert interface.start_run(2, True) is None
    m.assert_called_once_with(2, True)
Vincent Michel's avatar
Vincent Michel committed
135
136
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
137
138
139
140
141
142
143
144
    # Reset
    m.reset_mock()
    interface.check_error.reset_mock()
    # Third test
    assert interface.start_run() is None
    m.assert_called_once_with(-1, False)
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
145
146
147


def test_stop_run(interface):
Vincent Michel's avatar
Vincent Michel committed
148
    # Init
Vincent Michel's avatar
Vincent Michel committed
149
150
    m = interface.handel.xiaStopRun
    m.return_value = 0
Vincent Michel's avatar
Vincent Michel committed
151
    # First test
Vincent Michel's avatar
Vincent Michel committed
152
153
    assert interface.stop_run(1) is None
    m.assert_called_once_with(1)
Vincent Michel's avatar
Vincent Michel committed
154
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
155
156
    interface.check_error.assert_called_once_with(0)
    # Reset
Vincent Michel's avatar
Vincent Michel committed
157
    m.reset_mock()
Vincent Michel's avatar
Vincent Michel committed
158
159
    interface.check_error.reset_mock()
    # Second test
Vincent Michel's avatar
Vincent Michel committed
160
161
    assert interface.stop_run(2) is None
    m.assert_called_once_with(2)
Vincent Michel's avatar
Vincent Michel committed
162
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
163
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
164
165
166
167
168
169
170
171
    # Reset
    m.reset_mock()
    interface.check_error.reset_mock()
    # Second test
    assert interface.stop_run() is None
    m.assert_called_once_with(-1)
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
172
173


174
def test_get_spectrum_length(interface):
Vincent Michel's avatar
Vincent Michel committed
175
176
177
178
179
180
181
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        arg[0] = 10
        return 0

    m.side_effect = side_effect
182
    assert interface.get_spectrum_length(1) == 10
Vincent Michel's avatar
Vincent Michel committed
183
184
    m.assert_called_once()
    arg = m.call_args[0][2]
Vincent Michel's avatar
Vincent Michel committed
185
    m.assert_called_once_with(1, b'mca_length', arg)
Vincent Michel's avatar
Vincent Michel committed
186
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
187
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
188
189


190
def test_get_spectrum(interface):
Vincent Michel's avatar
Vincent Michel committed
191
192
193
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
Vincent Michel's avatar
Vincent Michel committed
194
        if dtype == b'mca_length':
Vincent Michel's avatar
Vincent Michel committed
195
196
            arg[0] = 10
            return 0
Vincent Michel's avatar
Vincent Michel committed
197
        if dtype == b'mca':
Vincent Michel's avatar
Vincent Michel committed
198
199
200
201
202
203
            for x in range(10):
                arg[x] = x
            return 0
        assert False

    m.side_effect = side_effect
Vincent Michel's avatar
Vincent Michel committed
204
    expected = numpy.array(range(10), dtype='uint32')
205
    diff = interface.get_spectrum(1) == expected
Vincent Michel's avatar
Vincent Michel committed
206
207
208
    assert diff.all()
    m.assert_called()
    arg = m.call_args[0][2]
Vincent Michel's avatar
Vincent Michel committed
209
    m.assert_called_with(1, b'mca', arg)
Vincent Michel's avatar
Vincent Michel committed
210
211
212
213
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


Vincent Michel's avatar
Vincent Michel committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def test_get_spectrums(interface):
    with mock.patch('handel.interface.get_channels') as m1:
        with mock.patch('handel.interface.get_spectrum') as m2:
            m1.return_value = [0, 1]
            # First test
            spectrums = [[0, 1, 2], [2, 3, 4]]
            m2.side_effect = lambda x: spectrums[x]
            expected = {0: [0, 1, 2], 1: [2, 3, 4]}
            assert interface.get_spectrums() == expected
            m1.assert_called_once_with()
            m2.assert_called_with(1)



def test_is_channel_running(interface):
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        arg[0] = 1
        return 0

    m.side_effect = side_effect
    assert interface.is_channel_running(2) is True
    arg = m.call_args[0][2]
    m.assert_called_once_with(2, b'run_active', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_is_running(interface):
    with mock.patch('handel.interface.get_channels') as m1:
        with mock.patch('handel.interface.is_channel_running') as m2:
            m1.return_value = [0, 1, 2]
            # First test
            m2.side_effect = lambda x: x == 1
            assert interface.is_running() is True
            m1.assert_called_once_with()
            m2.assert_called_with(1)
            # Second test
            m2.side_effect = lambda x: False
            assert interface.is_running() is False


# Statistics

def test_get_module_statistics(interface):
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        arg[3*7:4*7] = [1.0, 2.0, 255, 3, 4, 8, 6]
        return 0

    m.side_effect = side_effect
    with mock.patch('handel.interface.get_module_channels') as m2:
        m2.return_value = [-1, -1, -1, 8]
        expected = {8: interface.Stats(1.0, 2.0, 3, 4, 8, 6, 0.25)}
        assert interface.get_module_statistics('module3') == expected
        m2.assert_called_once_with('module3')
        arg = m.call_args[0][2]
        m.assert_called_once_with(8, b'module_statistics', arg)

    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_get_statistics(interface):
    with mock.patch('handel.interface.get_modules') as m1:
        with mock.patch('handel.interface.get_module_statistics') as m2:
            m1.return_value = ['module1', 'module2']
            # First test
            stats = {'module1': {0: 'some', 1: 'stats'},
                     'module2': {2: 'and', 3: 'some more'}}
            m2.side_effect = lambda x: stats[x]
            expected = {0: 'some', 1: 'stats', 2: 'and', 3: 'some more'}
            assert interface.get_statistics() == expected
            m1.assert_called_once_with()
            m2.assert_called_with('module2')


Vincent Michel's avatar
Vincent Michel committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Buffer

def test_get_buffer_length(interface):
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        arg[0] = 10
        return 0

    m.side_effect = side_effect
    assert interface.get_buffer_length(1) == 10
    arg = m.call_args[0][2]
    m.assert_called_once_with(1, b'buffer_len', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_get_buffer_full(interface):
    with pytest.raises(ValueError) as context:
        interface.get_buffer_full(1, 'very wrong')
    assert 'very wrong' in str(context.value)

    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        arg[0] = 1
        return 0

    m.side_effect = side_effect
    assert interface.get_buffer_full(1, 'a') is True
    arg = m.call_args[0][2]
    m.assert_called_once_with(1, b'buffer_full_a', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_get_buffer(interface):
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
        if dtype == b'buffer_len':
            arg[0] = 10
            return 0
        if dtype == b'buffer_a':
            for x in range(10):
                arg[x] = x
            return 0
        assert False

    m.side_effect = side_effect
Vincent Michel's avatar
Vincent Michel committed
343
    expected = numpy.array(range(10), dtype='uint32')
Vincent Michel's avatar
Vincent Michel committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    diff = interface.get_buffer(1, 'a') == expected
    assert diff.all()
    m.assert_called()
    arg = m.call_args[0][2]
    m.assert_called_with(1, b'buffer_a', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_buffer_done(interface):
    m = interface.handel.xiaBoardOperation
    m.return_value = 0
    assert interface.buffer_done(1, 'b') is None
    m.assert_called_with(1, b'buffer_done', b'b')
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
359
    interface.check_error.assert_called_with(0)
Vincent Michel's avatar
Vincent Michel committed
360
361
362
363
364
365
366
367


# System

def test_load_system(interface):
    m = interface.handel.xiaLoadSystem
    m.return_value = 0
    assert interface.load_system('somefile') is None
368
    m.assert_called_once_with(b'handel_ini', b'somefile')
Vincent Michel's avatar
Vincent Michel committed
369
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
370
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
371
372
373
374
375
376


def test_save_system(interface):
    m = interface.handel.xiaSaveSystem
    m.return_value = 0
    assert interface.save_system('somefile') is None
377
    m.assert_called_once_with(b'handel_ini', b'somefile')
Vincent Michel's avatar
Vincent Michel committed
378
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
379
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
380
381
382
383
384
385
386


def test_start_system(interface):
    m = interface.handel.xiaStartSystem
    m.return_value = 0
    assert interface.start_system() is None
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
387
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
388
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
389
390
391
392
393
394
395
396
397


# Logging

def test_enable_log_output(interface):
    m = interface.handel.xiaEnableLogOutput
    m.return_value = 0
    assert interface.enable_log_output() is None
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
398
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
399
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
400
401
402
403
404
405
406


def test_disable_log_output(interface):
    m = interface.handel.xiaSuppressLogOutput
    m.return_value = 0
    assert interface.disable_log_output() is None
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
407
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
408
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
409
410
411
412
413
414
415


def test_set_log_level(interface):
    m = interface.handel.xiaSetLogLevel
    m.return_value = 0
    assert interface.set_log_level(3) is None
    m.assert_called_once_with(3)
Vincent Michel's avatar
Vincent Michel committed
416
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
417
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
418
419
420
421
422
423


def test_set_log_output(interface):
    m = interface.handel.xiaSetLogOutput
    m.return_value = 0
    assert interface.set_log_output('somefile') is None
424
    m.assert_called_once_with(b'somefile')
Vincent Michel's avatar
Vincent Michel committed
425
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
426
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
427
428
429
430
431
432
433


def test_close_log(interface):
    m = interface.handel.xiaCloseLog
    m.return_value = 0
    assert interface.close_log() is None
    m.assert_called_once_with()
Vincent Michel's avatar
Vincent Michel committed
434
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
435
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
436
437


Vincent Michel's avatar
Vincent Michel committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Modules

def test_get_num_modules(interface):
    m = interface.handel.xiaGetNumModules

    def side_effect(arg):
        arg[0] = 10
        return 0

    m.side_effect = side_effect
    assert interface.get_num_modules() == 10
    arg = m.call_args[0][0]
    m.assert_called_once_with(arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)


def test_get_modules(interface):
    m1 = interface.handel.xiaGetNumModules
    m2 = interface.handel.xiaGetModules

    def side_effect_1(arg):
        arg[0] = 3
        return 0

    def side_effect_2(lst):
        lst[0][0:5] = b'name1'
        lst[1][0:5] = b'name2'
        lst[2][0:5] = b'name3'
        return 0

    m1.side_effect = side_effect_1
    m2.side_effect = side_effect_2
    expected = 'name1', 'name2', 'name3'
    assert interface.get_modules() == expected
    m2.assert_called_once()
    arg = m2.call_args[0][0]
    m2.assert_called_once_with(arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_get_module_from_channel(interface):
    m = interface.handel.xiaModuleFromDetChan

    def side_effect(channel, alias):
        alias[0:5] = b'name1'
        return 0

    m.side_effect = side_effect
    assert interface.get_module_from_channel(1) == 'name1'
    arg = m.call_args[0][1]
    m.assert_called_once_with(1, arg)
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
492
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
493
494


495
496
497
498
499
500
501
502
503
504
505
506
def test_get_module_type(interface):
    m = interface.handel.xiaGetModuleItem

    def side_effect(a, b, c):
        c[0:7] = b'mercury'
        return 0

    m.side_effect = side_effect
    assert interface.get_module_type('module1') == 'mercury'
    arg = m.call_args[0][2]
    m.assert_called_once_with(b'module1', b'module_type', arg)
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
507
    interface.check_error.assert_called_once_with(0)
508
509
510
511
512
513
514
515
516
517
518
519
520
521


def test_get_module_interface(interface):
    m = interface.handel.xiaGetModuleItem

    def side_effect(a, b, c):
        c[0:4] = b'usb2'
        return 0

    m.side_effect = side_effect
    assert interface.get_module_interface('module1') == 'usb2'
    arg = m.call_args[0][2]
    m.assert_called_once_with(b'module1', b'interface', arg)
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    interface.check_error.assert_called_once_with(0)


def test_get_module_number_of_channels(interface):
    m = interface.handel.xiaGetModuleItem

    def side_effect(a, b, c):
        c[0] = 4
        return 0

    m.side_effect = side_effect
    assert interface.get_module_number_of_channels('module1') == 4
    arg = m.call_args[0][2]
    m.assert_called_once_with(b'module1', b'number_of_channels', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)


def test_get_module_channel_at(interface):
    m = interface.handel.xiaGetModuleItem

    def side_effect(a, b, c):
        c[0] = 3
        return 0

    m.side_effect = side_effect
    assert interface.get_module_channel_at('module1', 2) == 3
    arg = m.call_args[0][2]
    m.assert_called_once_with(b'module1', b'channel2_alias', arg)
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)


def test_get_module_channels(interface):
    m = interface.handel.xiaGetModuleItem
    results = [3, 2, 1, 0, 4]

    def side_effect(a, b, c):
        c[0] = results.pop()
        return 0

    m.side_effect = side_effect
    assert interface.get_module_channels('module1') == (0, 1, 2, 3)
    # Make sure errors have been checked
566
567
568
    interface.check_error.assert_called_with(0)


Vincent Michel's avatar
Vincent Michel committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
def test_get_grouped_channels(interface):
    m1 = interface.handel.xiaGetNumModules
    m2 = interface.handel.xiaGetModules
    m3 = interface.handel.xiaGetModuleItem
    results = [3, 2, 1, 0, 4, 7, 6, 5, 4, 4]

    def side_effect_1(arg):
        arg[0] = 2
        return 0

    def side_effect_2(lst):
        lst[0][0:5] = b'name1'
        lst[1][0:5] = b'name2'
        return 0

    def side_effect_3(a, b, c):
        c[0] = results.pop()
        return 0

    m1.side_effect = side_effect_1
    m2.side_effect = side_effect_2
    m3.side_effect = side_effect_3
    assert interface.get_grouped_channels() == ((4, 5, 6, 7), (0, 1, 2, 3))
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


def test_get_channels(interface):
    m1 = interface.handel.xiaGetNumModules
    m2 = interface.handel.xiaGetModules
    m3 = interface.handel.xiaGetModuleItem
    results = [3, 2, 1, 0, 4, 7, 6, 5, 4, 4]

    def side_effect_1(arg):
        arg[0] = 2
        return 0

    def side_effect_2(lst):
        lst[0][0:5] = b'name1'
        lst[1][0:5] = b'name2'
        return 0

    def side_effect_3(a, b, c):
        c[0] = results.pop()
        return 0

    m1.side_effect = side_effect_1
    m2.side_effect = side_effect_2
    m3.side_effect = side_effect_3
    assert interface.get_channels() == (0, 1, 2, 3, 4, 5, 6, 7)
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


623
624
625
626
627
628
629
630
631
632
# Parameters

def test_get_acquistion_value(interface):
    m = interface.handel.xiaGetAcquisitionValues

    def side_effect(a, b, c):
        c[0] = 2.3
        return 0

    m.side_effect = side_effect
633
    assert interface.get_acquisition_value('test', channel=1) == 2.3
634
    arg = m.call_args[0][2]
635
    m.assert_called_once_with(1, b'test', arg)
Vincent Michel's avatar
Vincent Michel committed
636
637
638
639
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)


640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
def test_set_acquisition_value(interface):
    m = interface.handel.xiaSetAcquisitionValues
    m.return_value = 0

    # Single channel
    assert interface.set_acquisition_value('test', 2.3, channel=1) is None
    arg = m.call_args[0][2]
    m.assert_called_once_with(1, b'test', arg)
    assert arg[0] == 2.3

    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
    m.reset_mock()

    # All channels
    assert interface.set_acquisition_value('test', 2.3) is None
    arg = m.call_args[0][2]
    m.assert_called_once_with(-1, b'test', arg)
    assert arg[0] == 2.3


Vincent Michel's avatar
Vincent Michel committed
661
662
663
def test_remove_acquisition_value(interface):
    m = interface.handel.xiaRemoveAcquisitionValues
    m.return_value = 0
664
665
666

    # Single channel
    assert interface.remove_acquisition_value('test', channel=1) is None
Vincent Michel's avatar
Vincent Michel committed
667
    m.assert_called_once_with(1, b'test')
668

Vincent Michel's avatar
Vincent Michel committed
669
670
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
671
672
673
674
675
    m.reset_mock()

    # Multiple channels
    assert interface.remove_acquisition_value('test') is None
    m.assert_called_once_with(-1, b'test')
Vincent Michel's avatar
Vincent Michel committed
676
677


678
def test_apply_acquisition_values(interface):
Vincent Michel's avatar
Vincent Michel committed
679
680
    m = interface.handel.xiaBoardOperation
    m.return_value = 0
681
682
683

    # Single channel
    assert interface.apply_acquisition_values(channel=1) is None
Vincent Michel's avatar
Vincent Michel committed
684
685
    dummy = m.call_args[0][2]
    m.assert_called_once_with(1, b'apply', dummy)
686

Vincent Michel's avatar
Vincent Michel committed
687
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
688
    interface.check_error.assert_called_once_with(0)
689
690
691
692
693
694
695
696
    m.reset_mock()

    # Multiple channel
    with mock.patch('handel.interface.get_grouped_channels') as m2:
        m2.return_value = ((0, ), )
        assert interface.apply_acquisition_values() is None
        dummy = m.call_args[0][2]
        m.assert_called_once_with(0, b'apply', dummy)
697
698


Vincent Michel's avatar
Vincent Michel committed
699
700
# Debugging

701
def test_get_handel_version(interface):
Vincent Michel's avatar
Vincent Michel committed
702
703
704
705
706
707
    m = interface.handel.xiaGetVersionInfo

    def side_effect(a, b, c, d):
        d[0], c[0], b[0], a[0] = b'v', 1, 2, 3

    m.side_effect = side_effect
708
    assert interface.get_handel_version() == (1, 2, 3)
Vincent Michel's avatar
Vincent Michel committed
709
    m.assert_called_once()
Vincent Michel's avatar
Vincent Michel committed
710
    # xiaGetVersionInfo does not return an error code
Vincent Michel's avatar
Vincent Michel committed
711
712
713
714
715


# Files

def test_get_config_files(interface):
Vincent Michel's avatar
Vincent Michel committed
716
717
    assert interface.get_config_files('.') == ['scripts/mercury.ini']
    assert interface.get_config_files(b'.') == [b'scripts/mercury.ini']
Vincent Michel's avatar
Vincent Michel committed
718
719
720


def test_get_config(interface):
Vincent Michel's avatar
Vincent Michel committed
721
    # Using string paths
Vincent Michel's avatar
Vincent Michel committed
722
    filename = interface.get_config_files('.')[0]
Vincent Michel's avatar
Vincent Michel committed
723
    conf = interface.get_config('.', filename)
Vincent Michel's avatar
Vincent Michel committed
724
    assert conf['detector definitions'][0]['alias'] == 'detector1'
Vincent Michel's avatar
Vincent Michel committed
725
    with pytest.raises(IOError):
Vincent Michel's avatar
Vincent Michel committed
726
727
728
729
730
731
732
733
        interface.get_config('.', 'i_dont_exist.ini')

    # Using bytestring paths
    filename = interface.get_config_files(b'.')[0]
    conf = interface.get_config(b'.', filename)
    assert conf['detector definitions'][0]['alias'] == 'detector1'
    with pytest.raises(IOError):
        interface.get_config(b'.', b'i_dont_exist.ini')