Completed
Push — master ( 44c72f...fa0a1c )
by Olivier
56s
created

TestClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 48.84 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 21
loc 43
rs 10
wmc 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
2
import unittest
3
import sys
4
print("SYS:PATH", sys.path)
5
sys.path.insert(0, "python-opcua")
6
sys.path.insert(0, "opcua-widgets")
7
import os
8
print("PWD", os.getcwd())
9
10
from opcua import ua
11
from opcua import Server
12
13
from PyQt5.QtCore import QTimer, QSettings, QModelIndex, Qt, QCoreApplication
14
from PyQt5.QtWidgets import QApplication
15
from PyQt5.QtTest import QTest
16
17
from uaclient.mainwindow import Window
18
19
20
class TestClient(unittest.TestCase):
21
    def setUp(self):
22
        self.server = Server()
23
        url = "opc.tcp://localhost:48400/freeopcua/server/"
24
        self.server.set_endpoint(url)
25
        self.server.start()
26
        self.client = Window()
27
        self.client.ui.addrComboBox.setCurrentText(url)
28
        self.client.connect()
29
30
    def tearDown(self):
31
        self.client.disconnect()
32
        self.server.stop()
33
34
    def get_attr_value(self, text):
35
        idxlist = self.client.attrs_ui.model.match(self.client.attrs_ui.model.index(0, 0), Qt.DisplayRole, text,  1, Qt.MatchExactly | Qt.MatchRecursive)
36
        idx = idxlist[0]
37
        idx = idx.sibling(idx.row(), 1)
38
        item = self.client.attrs_ui.model.itemFromIndex(idx)
39
        return item.data(Qt.UserRole).value
40
41
    def test_select_objects(self):
42
        objects = self.server.nodes.objects
43
        self.client.tree_ui.set_current_node("Objects")
44
        self.assertEqual(objects, self.client.tree_ui.get_current_node())
45
        self.assertGreater(self.client.attrs_ui.model.rowCount(), 6)
46
        print("REF COUNT", self.client.refs_ui.model.rowCount())
47
        self.assertGreater(self.client.refs_ui.model.rowCount(), 1)
48
49
        data = self.get_attr_value("NodeId")
50
        self.assertEqual(data, objects.nodeid)
51
52
    def test_select_server_node(self):
53
        self.client.tree_ui.set_current_node("Objects")
54
        self.client.tree_ui.expand_current_node()  # must be expanded to load children
55
        server_node = self.server.nodes.server
56
        self.client.tree_ui.set_current_node("Server")
57
        self.assertEqual(server_node, self.client.tree_ui.get_current_node())
58
        self.assertGreater(self.client.attrs_ui.model.rowCount(), 6)
59
        self.assertGreater(self.client.refs_ui.model.rowCount(), 10)
60
61
        data = self.get_attr_value("NodeId")
62
        self.assertEqual(data, server_node.nodeid)
63
64
65
66
if __name__ == "__main__":
67
    app = QApplication(sys.argv)
68
    unittest.main()
69
70
71