test_interface.py 17.7 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


def test_stop_run(interface):
Vincent Michel's avatar
Vincent Michel committed
140
    # Init
Vincent Michel's avatar
Vincent Michel committed
141
142
    m = interface.handel.xiaStopRun
    m.return_value = 0
Vincent Michel's avatar
Vincent Michel committed
143
    # First test
Vincent Michel's avatar
Vincent Michel committed
144
145
    assert interface.stop_run(1) is None
    m.assert_called_once_with(1)
Vincent Michel's avatar
Vincent Michel committed
146
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
147
148
    interface.check_error.assert_called_once_with(0)
    # Reset
Vincent Michel's avatar
Vincent Michel committed
149
    m.reset_mock()
Vincent Michel's avatar
Vincent Michel committed
150
151
    interface.check_error.reset_mock()
    # Second test
Vincent Michel's avatar
Vincent Michel committed
152
153
    assert interface.stop_run(2) is None
    m.assert_called_once_with(2)
Vincent Michel's avatar
Vincent Michel committed
154
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
155
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
156
157


158
def test_get_spectrum_length(interface):
Vincent Michel's avatar
Vincent Michel committed
159
160
161
162
163
164
165
    m = interface.handel.xiaGetRunData

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

    m.side_effect = side_effect
166
    assert interface.get_spectrum_length(1) == 10
Vincent Michel's avatar
Vincent Michel committed
167
168
    m.assert_called_once()
    arg = m.call_args[0][2]
Vincent Michel's avatar
Vincent Michel committed
169
    m.assert_called_once_with(1, b'mca_length', arg)
Vincent Michel's avatar
Vincent Michel committed
170
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
171
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
172
173


174
def test_get_spectrum(interface):
Vincent Michel's avatar
Vincent Michel committed
175
176
177
    m = interface.handel.xiaGetRunData

    def side_effect(channel, dtype, arg):
Vincent Michel's avatar
Vincent Michel committed
178
        if dtype == b'mca_length':
Vincent Michel's avatar
Vincent Michel committed
179
180
            arg[0] = 10
            return 0
Vincent Michel's avatar
Vincent Michel committed
181
        if dtype == b'mca':
Vincent Michel's avatar
Vincent Michel committed
182
183
184
185
186
187
            for x in range(10):
                arg[x] = x
            return 0
        assert False

    m.side_effect = side_effect
Vincent Michel's avatar
Vincent Michel committed
188
    expected = numpy.array(range(10), dtype='uint32')
189
    diff = interface.get_spectrum(1) == expected
Vincent Michel's avatar
Vincent Michel committed
190
191
192
    assert diff.all()
    m.assert_called()
    arg = m.call_args[0][2]
Vincent Michel's avatar
Vincent Michel committed
193
    m.assert_called_with(1, b'mca', arg)
Vincent Michel's avatar
Vincent Michel committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
    # Make sure errors have been checked
    interface.check_error.assert_called_with(0)


# 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
248
    expected = numpy.array(range(10), dtype='uint32')
Vincent Michel's avatar
Vincent Michel committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    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
264
    interface.check_error.assert_called_with(0)
Vincent Michel's avatar
Vincent Michel committed
265
266
267
268
269
270
271
272


# System

def test_load_system(interface):
    m = interface.handel.xiaLoadSystem
    m.return_value = 0
    assert interface.load_system('somefile') is None
273
    m.assert_called_once_with(b'handel_ini', b'somefile')
Vincent Michel's avatar
Vincent Michel committed
274
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
275
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
276
277
278
279
280
281


def test_save_system(interface):
    m = interface.handel.xiaSaveSystem
    m.return_value = 0
    assert interface.save_system('somefile') is None
282
    m.assert_called_once_with(b'handel_ini', b'somefile')
Vincent Michel's avatar
Vincent Michel committed
283
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
284
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
285
286
287
288
289
290
291


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
292
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
293
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
294
295
296
297
298
299
300
301
302


# 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
303
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
304
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
305
306
307
308
309
310
311


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
312
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
313
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
314
315
316
317
318
319
320


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
321
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
322
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
323
324
325
326
327
328


def test_set_log_output(interface):
    m = interface.handel.xiaSetLogOutput
    m.return_value = 0
    assert interface.set_log_output('somefile') is None
329
    m.assert_called_once_with(b'somefile')
Vincent Michel's avatar
Vincent Michel committed
330
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
331
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
332
333
334
335
336
337
338


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
339
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
340
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
341
342


Vincent Michel's avatar
Vincent Michel committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# 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
397
    interface.check_error.assert_called_once_with(0)
Vincent Michel's avatar
Vincent Michel committed
398
399


400
401
402
403
404
405
406
407
408
409
410
411
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
412
    interface.check_error.assert_called_once_with(0)
413
414
415
416
417
418
419
420
421
422
423
424
425
426


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
427
428
429
430
431
432
433
434
435
436
437
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
    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
471
472
473
    interface.check_error.assert_called_with(0)


Vincent Michel's avatar
Vincent Michel committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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)


528
529
530
531
532
533
534
535
536
537
# 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
538
    assert interface.get_acquisition_value('test', channel=1) == 2.3
539
    arg = m.call_args[0][2]
540
    m.assert_called_once_with(1, b'test', arg)
Vincent Michel's avatar
Vincent Michel committed
541
542
543
544
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)


545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
566
567
568
def test_remove_acquisition_value(interface):
    m = interface.handel.xiaRemoveAcquisitionValues
    m.return_value = 0
569
570
571

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

Vincent Michel's avatar
Vincent Michel committed
574
575
    # Make sure errors have been checked
    interface.check_error.assert_called_once_with(0)
576
577
578
579
580
    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
581
582


583
def test_apply_acquisition_values(interface):
Vincent Michel's avatar
Vincent Michel committed
584
585
    m = interface.handel.xiaBoardOperation
    m.return_value = 0
586
587
588

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

Vincent Michel's avatar
Vincent Michel committed
592
    # Make sure errors have been checked
Vincent Michel's avatar
Vincent Michel committed
593
    interface.check_error.assert_called_once_with(0)
594
595
596
597
598
599
600
601
    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)
602
603


Vincent Michel's avatar
Vincent Michel committed
604
605
# Debugging

606
def test_get_handel_version(interface):
Vincent Michel's avatar
Vincent Michel committed
607
608
609
610
611
612
    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
613
    assert interface.get_handel_version() == (1, 2, 3)
Vincent Michel's avatar
Vincent Michel committed
614
    m.assert_called_once()
Vincent Michel's avatar
Vincent Michel committed
615
    # xiaGetVersionInfo does not return an error code
Vincent Michel's avatar
Vincent Michel committed
616
617
618
619
620


# Files

def test_get_config_files(interface):
Vincent Michel's avatar
Vincent Michel committed
621
622
    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
623
624
625


def test_get_config(interface):
Vincent Michel's avatar
Vincent Michel committed
626
    # Using string paths
Vincent Michel's avatar
Vincent Michel committed
627
    filename = interface.get_config_files('.')[0]
Vincent Michel's avatar
Vincent Michel committed
628
    conf = interface.get_config('.', filename)
Vincent Michel's avatar
Vincent Michel committed
629
    assert conf['detector definitions'][0]['alias'] == 'detector1'
Vincent Michel's avatar
Vincent Michel committed
630
    with pytest.raises(IOError):
Vincent Michel's avatar
Vincent Michel committed
631
632
633
634
635
636
637
638
        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')