File: test_copy.py

package info (click to toggle)
python-canmatrix 1.2~github-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,012 kB
  • sloc: xml: 30,201; python: 14,631; makefile: 31; sh: 7
file content (114 lines) | stat: -rw-r--r-- 4,972 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
import canmatrix.canmatrix
import canmatrix.copy


def test_merge():
    matrix1 = canmatrix.canmatrix.CanMatrix()
    frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
    frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
    matrix1.add_frame(frame1)

    matrix2 = canmatrix.canmatrix.CanMatrix()
    frame2 = canmatrix.canmatrix.Frame("Frame2", arbitration_id=2)
    matrix2.add_frame(frame2)

    matrix1.merge([matrix2])
    assert len(matrix1.frames) == 2


def test_copy_ecu_with_frames():
    matrix1 = canmatrix.canmatrix.CanMatrix()
    frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
    frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
    matrix1.add_frame(frame1)

    matrix2 = canmatrix.canmatrix.CanMatrix()
    frame2 = canmatrix.canmatrix.Frame("Frame2", arbitration_id=2, transmitters= ["ECU"])
    matrix2.add_frame(frame2)
    matrix2.update_ecu_list()

    canmatrix.copy.copy_ecu_with_frames("ECU", matrix2, matrix1)

    assert len(matrix1.frames) == 2
    assert len(matrix1.ecus) == 1


def test_copy_ecu_without_frames():
    matrix1 = canmatrix.canmatrix.CanMatrix()
    frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
    frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
    matrix1.add_frame(frame1)

    matrix2 = canmatrix.canmatrix.CanMatrix()
    frame2 = canmatrix.canmatrix.Frame("Frame2", arbitration_id=2, transmitters= ["ECU"])
    matrix2.add_frame(frame2)
    matrix2.update_ecu_list()
    matrix2.add_ecu_defines("attrib", "STRING")
    matrix2.ecu_by_name("ECU").add_attribute("attrib", "attribValue")

    canmatrix.copy.copy_ecu("ECU", matrix2, matrix1)

    assert len(matrix1.frames) == 1
    assert len(matrix1.ecus) == 1
    assert matrix1.ecu_by_name("ECU") is not None


def test_copy_ecu_with_attributes():
    matrix1 = canmatrix.canmatrix.CanMatrix()
    frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
    frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
    matrix1.add_frame(frame1)
    matrix1.add_ecu_defines("some_ecu_define", "STRING")

    matrix2 = canmatrix.canmatrix.CanMatrix()
    frame2 = canmatrix.canmatrix.Frame("Frame2", arbitration_id=2, transmitters= ["ECU"])
    matrix2.add_frame(frame2)
    matrix2.update_ecu_list()
    matrix2.add_ecu_defines("Node Address", "INT 0 255")
    matrix2.add_ecu_defines("attrib", "STRING")
    matrix2.add_ecu_defines("some_ecu_define", "STRING")
    matrix2.add_define_default("some_ecu_define", "default_value")
    matrix2.ecu_by_name("ECU").add_attribute("attrib", "attribValue")
    matrix2.ecu_by_name("ECU").add_attribute("Node Address", 42)

    canmatrix.copy.copy_ecu("ECU", matrix2, matrix1)

    assert len(matrix1.frames) == 1
    assert len(matrix1.ecus) == 1
    assert matrix1.ecu_by_name("ECU") is not None
    assert matrix1.ecu_by_name("ECU").attribute("Node Address") == '42'
    assert matrix1.ecu_by_name("ECU").attribute("some_ecu_define", matrix1) == "default_value"


def test_copy_frame_default_attributes():
    source = canmatrix.canmatrix.CanMatrix()
    frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
    signal = canmatrix.canmatrix.Signal("Signal1")
    frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
    frame1.add_signal(signal)
    source.add_frame(frame1)
    source.add_frame_defines("some_attribute", "STRING")
    source.add_define_default("some_attribute", "source_frame_default")
    source.add_signal_defines("some_signal_attribute", "STRING")
    source.add_define_default("some_signal_attribute", "source_sig_default")
    source.add_frame_defines("some_attribute_without_default", "STRING")

    #test if default value only defined in source and copied to target
    target = canmatrix.canmatrix.CanMatrix()
    target.add_frame_defines("some_attribute_without_default", "STRING")
    target.add_define_default("some_attribute_without_default", "0")
    canmatrix.copy.copy_frame(frame1.arbitration_id, source, target)
    assert target.frames[0].attribute("some_attribute", target) == "source_frame_default"
    assert target.frames[0].signals[0].attribute("some_signal_attribute", target) == "source_sig_default"
    assert target.frames[0].attribute("some_attribute_without_default", target) == "0"

    # test if define already exists, but has another default value:
    target2 = canmatrix.canmatrix.CanMatrix()
    target2.add_frame_defines("some_attribute", "STRING")
    target2.add_define_default("some_attribute", "target_frame_default")
    target2.add_signal_defines("some_signal_attribute", "STRING")
    target2.add_define_default("some_signal_attribute", "target_sig_default")
    canmatrix.copy.copy_frame(frame1.arbitration_id, source, target2)
    assert target2.frames[0].attribute("some_attribute", target2) == "source_frame_default"
    assert target2.frames[0].signals[0].attribute("some_signal_attribute", target2) == "source_sig_default"