summaryrefslogtreecommitdiff
path: root/test/test_usv.py
blob: 5a013ae61bcf96f211947369487aedaa6696a6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pytest

from usv import Usv

class MockUsvSerial(object):
    def __init__(self, *args, **kwargs):
        self.buf = []
    
    def open(self):
        pass

    def write(self, buf):
        self.buf.insert(1, buf)

    def read(self, size):
        cmd = self.buf.pop()

        if cmd == '\r':
            return '\r=>'
        elif cmd == 'd 1\r':
            return '01 V In      219\r=>'
        elif cmd == 'd 2\r':
            return '02 V Out     215\r=>'
        elif cmd == 'd 5\r':
            return '05 VA Out    108\r=>'
        elif cmd == 'd 7\r':
            return '07 V Batt   52.7\r=>'
        elif cmd == 'd 8\r':
            return '08 Freq 50.01 Hz\r=>'
        elif cmd == 'd 16\r':
            return '16 FullLoad% 007\r=>'
        elif cmd == 'd 17\r':
            return '17 Watts      83\r=>'
        elif cmd == 'd 18\r':
            return '18 PF  1.00 ----\r=>'
        elif cmd == 'd 19\r':
            return '19 CrestF   1.44\r=>'
        elif cmd == 'd 20\r':  
            return '20 #PwrOut   157\r=>'
        elif cmd == 'd 23\r':
            return '23 InvMin 0231.2\r=>'


class TestUsv(object):
    def test_usv(self, monkeypatch):
        monkeypatch.setattr('usv.usv.Serial', MockUsvSerial)
        
        usv_handle = Usv('/dev/usv')
    
        fields = usv_handle.read()
    
        assert len(fields) == 11

        assert fields['acvoltsin'] == 219.0
        assert fields['acvoltsout'] == 215.0
        assert fields['vaout'] == 108.0
        assert fields['vbattery'] == 52.7
        assert fields['frequency'] == 50.01
        assert fields['fullload'] == 7.0
        assert fields['watts'] == 83.0
        assert fields['powerfactor'] == 1.0
        assert fields['crestfactor'] == 1.44
        assert fields['powerout'] == 157.0
        assert fields['inverterminutes'] == 231.2