Completed
Pull Request — master (#158)
by Olivier
02:32
created

HistoryCommon.create_var()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 8
rs 9.4285
1
import time
2
from datetime import datetime, timedelta
3
import unittest
4
5
from opcua import Client
6
from opcua import Server
7
from opcua import ua
8
from opcua.server.history_sql import HistorySQLite
9
10
from tests_common import CommonTests, add_server_methods
11
12
port_num1 = 48530
13
port_num2 = 48530
14
15
16
17
class HistoryCommon(object):
18
    srv = Server
19
    clt = Client
20
21
    def start_server_and_client(self):
22
        self.srv = Server()
23
        self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1)
24
        self.srv.start()
25
26
        self.clt = Client('opc.tcp://localhost:%d' % port_num1)
27
        self.clt.connect()
28
29
    def stop_server_and_client(self):
30
        self.clt.disconnect()
31
        self.srv.stop()
32
33
    def create_var(self):
34
        o = self.srv.get_objects_node()
35
        self.values = [i for i in range(20)]
36
        self.var = o.add_variable(3, "history_var", 0)
37
        self.srv.iserver.enable_history(self.var, period=None, count=10)
38
        for i in self.values:
39
            self.var.set_value(i)
40
        time.sleep(1)
41
42
    def test_history_read_one(self):
43
        res = self.var.read_raw_history(None, None, 1)
44
        self.assertEqual(len(res), 1)
45
        self.assertEqual(res[0].Value.Value, self.values[-1])
46
47
    # no start and no end is not defined by spec, return reverse order
48
    def test_history_read_none(self):
49
        # FIXME not sure this once is supported by spec
50
        res = self.var.read_raw_history(None, None, 0)
51
        self.assertEqual(len(res), 20)
52
        self.assertEqual(res[0].Value.Value, self.values[-1])  # self.values was 0
53
        self.assertEqual(res[-1].Value.Value, self.values[0])  # self.values was -1
54
55
    # no start and no end is not defined by spec, return reverse order
56
    def test_history_read_last_3(self):
57
        res = self.var.read_raw_history(None, None, 3)
58
        self.assertEqual(len(res), 3)
59
        self.assertEqual(res[-1].Value.Value, self.values[-3])  # self.values was -1
60
        self.assertEqual(res[0].Value.Value, self.values[-1])  # self.values was -3
61
62
    # no start and no end is not defined by spec, return reverse order
63
    def test_history_read_all2(self):
64
        res = self.var.read_raw_history(None, None, 9999)
65
        self.assertEqual(len(res), 20)
66
        self.assertEqual(res[-1].Value.Value, self.values[0])  # self.values was -1
67
        self.assertEqual(res[0].Value.Value, self.values[-1])  # self.values was 0
68
69
    # only has end time, should return reverse order
70
    def test_history_read_2_with_end(self):
71
        now = datetime.utcnow()
72
        old = now - timedelta(days=6)
73
74
        res = self.var.read_raw_history(None, now, 2)
75
        self.assertEqual(len(res), 2)
76
        self.assertEqual(res[-1].Value.Value, self.values[-2])  # self.values was -1
77
    
78 View Code Duplication
    def test_history_read_all(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
79
        now = datetime.utcnow()
80
        old = now - timedelta(days=6)
81
82
        res = self.var.read_raw_history(old, now, 0)
83
        self.assertEqual(len(res), 20)
84
        self.assertEqual(res[-1].Value.Value, self.values[-1])
85
        self.assertEqual(res[0].Value.Value, self.values[0])
86
87 View Code Duplication
    def test_history_read_5_in_timeframe(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
        now = datetime.utcnow()
89
        old = now - timedelta(days=6)
90
91
        res = self.var.read_raw_history(old, now, 5)
92
        self.assertEqual(len(res), 5)
93
        self.assertEqual(res[-1].Value.Value, self.values[4])
94
        self.assertEqual(res[0].Value.Value, self.values[0])
95
96
    # start time greater than end time, should return reverse order
97 View Code Duplication
    def test_history_read_5_in_timeframe_start_greater_than_end(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
        now = datetime.utcnow()
99
        old = now - timedelta(days=6)
100
101
        res = self.var.read_raw_history(now, old, 5)
102
        self.assertEqual(len(res), 5)
103
        self.assertEqual(res[-1].Value.Value, self.values[-5])
104
        self.assertEqual(res[0].Value.Value, self.values[-1])
105
106
107
108
class TestHistory(unittest.TestCase, HistoryCommon):
109
110
    @classmethod
111
    def setUpClass(self):
112
        self.start_server_and_client(self)
113
        self.create_var(self)
114
115
    @classmethod
116
    def tearDownClass(self):
117
        self.stop_server_and_client(self)
118
119
120
class TestHistorySQL(unittest.TestCase, HistoryCommon):
121
    @classmethod
122
    def setUpClass(self):
123
        self.start_server_and_client(self)
124
        self.srv.iserver.history_manager.set_storage(HistorySQLite(":memory:"))
125
        self.create_var(self)
126
127
    @classmethod
128
    def tearDownClass(self):
129
        self.stop_server_and_client(self)
130
131
132
133
134
135
136
137
138
139
140