Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_motohawk(self):
filename = os.path.join('tests', 'files', 'motohawk.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
self.assertEqual(len(db.nodes), 2)
self.assertEqual(db.nodes[0].name, 'PCM1')
self.assertEqual(db.nodes[1].name, 'FOO')
self.assertEqual(len(db.messages), 1)
self.assertEqual(len(db.messages[0].signals[2].receivers), 2)
self.assertEqual(db.messages[0].signals[2].receivers[0], 'PCM1')
self.assertEqual(db.messages[0].signals[2].receivers[1], 'FOO')
self.assertEqual(db.messages[0].signals[1].receivers, [])
with open(filename, 'rb') as fin:
self.assertEqual(db.as_dbc_string().encode('ascii'), fin.read())
def test_motohawk(self):
filename = os.path.join('tests', 'files', 'motohawk.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
self.assertEqual(len(db.nodes), 2)
self.assertEqual(db.nodes[0].name, 'PCM1')
self.assertEqual(db.nodes[1].name, 'FOO')
self.assertEqual(len(db.messages), 1)
self.assertEqual(len(db.messages[0].signals[2].receivers), 2)
self.assertEqual(db.messages[0].signals[2].receivers[0], 'PCM1')
self.assertEqual(db.messages[0].signals[2].receivers[1], 'FOO')
self.assertEqual(db.messages[0].signals[1].receivers, [])
with open(filename, 'rb') as fin:
self.assertEqual(db.as_dbc_string().encode('ascii'), fin.read())
def test_get_message_by_frame_id_and_name(self):
filename = os.path.join('tests', 'files', 'motohawk.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
message = db.get_message_by_name('ExampleMessage')
self.assertEqual(message.name, 'ExampleMessage')
message = db.get_message_by_frame_id(496)
self.assertEqual(message.frame_id, 496)
def test_load_bad_format(self):
with self.assertRaises(cantools.db.UnsupportedDatabaseFormatError):
cantools.db.load(StringIO(''))
def test_attributes(self):
filename = os.path.join('tests', 'files', 'attributes.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
# Signal attributes.
attributes = db.messages[0].signals[0].dbc.attributes
attribute = attributes['TheSignalStringAttribute']
self.assertEqual(attribute.name, 'TheSignalStringAttribute')
self.assertEqual(attribute.value, 'TestString')
self.assertEqual(attribute.definition,
db.dbc.attribute_definitions['TheSignalStringAttribute'])
self.assertEqual(attribute.definition.default_value, '100')
self.assertEqual(attribute.definition.kind, 'SG_')
self.assertEqual(attribute.definition.type_name, 'STRING')
self.assertEqual(attribute.definition.minimum, None)
self.assertEqual(attribute.definition.maximum, None)
self.assertEqual(attribute.definition.choices, None)
def test_attributes(self):
filename = os.path.join('tests', 'files', 'attributes.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
# Signal attributes.
attributes = db.messages[0].signals[0].dbc.attributes
attribute = attributes['TheSignalStringAttribute']
self.assertEqual(attribute.name, 'TheSignalStringAttribute')
self.assertEqual(attribute.value, 'TestString')
self.assertEqual(attribute.definition,
db.dbc.attribute_definitions['TheSignalStringAttribute'])
self.assertEqual(attribute.definition.default_value, '100')
self.assertEqual(attribute.definition.kind, 'SG_')
self.assertEqual(attribute.definition.type_name, 'STRING')
self.assertEqual(attribute.definition.minimum, None)
self.assertEqual(attribute.definition.maximum, None)
self.assertEqual(attribute.definition.choices, None)
def test_setters(self):
filename = os.path.join('tests', 'files', 'attributes.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
# Calling the setters for coverage. Assertions are not
# necessary here since functionality is trivial.
db.nodes[0].name = 'SetterName'
db.nodes[0].comment = 'SetterComment'
db.dbc.attributes['BusType'].value = 'LIN'
db.messages[0].name = 'SetterName'
db.messages[0].frame_id = 0x12121212
db.messages[0].is_extended_frame = True
db.messages[0].length = 6
db.messages[0].comment = 'TheNewComment'
db.messages[0].bus_name = 'TheNewBusName'
db.messages[0].signals[0].name = 'SetterName'
db.messages[0].signals[0].start = 8
db.messages[0].signals[0].length = 8
db.messages[0].signals[0].byte_order = 'big_endian'
def test_refresh(self):
filename = os.path.join('tests', 'files', 'attributes.dbc')
with open(filename, 'r') as fin:
db = cantools.db.load(fin)
message = db.get_message_by_frame_id(0x39)
self.assertEqual(message.name, 'TheMessage')
message.frame_id = 0x40
db.refresh()
message = db.get_message_by_frame_id(0x40)
self.assertEqual(message.name, 'TheMessage')
self.assertEqual(message.frame_id,0x40)
message.name = 'TheNewMessage'
db.refresh()
message = db.get_message_by_name('TheNewMessage')
self.assertEqual(message.name, 'TheNewMessage')
self.assertEqual(message.frame_id, 0x40)
with self.assertRaises(KeyError) as cm:
def test_load_bad_format(self):
with self.assertRaises(cantools.db.UnsupportedDatabaseFormatError):
cantools.db.load(StringIO(''))