Skip to content

Commit

Permalink
chore(tests): Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esloch committed Sep 11, 2023
1 parent dd41593 commit fa7c471
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 55 deletions.
2 changes: 1 addition & 1 deletion pyreaddbc/readdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ._readdbc import ffi, lib


def read_dbc(filename, encoding="iso-8859-1", raw=False):
def read_dbc(filename, encoding="utf-8", raw=False):
"""
Opens a DBC file and return its contents as a pandas
Dataframe.
Expand Down
Binary file added tests/data/sids.dbc
Binary file not shown.
78 changes: 24 additions & 54 deletions tests/test_pyreaddbc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# import gzip
import gzip
import os
import unittest
from pathlib import Path
Expand All @@ -12,43 +12,40 @@
path_root = Path(__file__).resolve().parent.parent
data_files = path_root / "tests/data"
dbc_file = str(data_files / "STPI2206.dbc")
temp_dbf_file = str(data_files / "STPI2206.dbf")

dbc_files = [
"ZIKABR16.dbc",
"STPI2206.dbc",
"sids.dbc",
]


class TestDBCChecks(unittest.TestCase):
def test_read_dbc(self):
df = read_dbc(dbc_file)
self.assertIsInstance(df, pd.DataFrame)
self.assertFalse(df.empty)
self.assertGreater(len(df.columns), 0)
self.assertGreater(len(df), 0)
self.assertDataFrameValid(df)

def test_dbc2dbf(self):
temp_dbf_file = str(data_files / "STPI2206.dbf")
dbc2dbf(str(dbc_file), temp_dbf_file)
self.assertTrue(os.path.isfile(temp_dbf_file))
df = read_dbc_dbf(temp_dbf_file)
self.assertIsInstance(df, pd.DataFrame)
self.assertFalse(df.empty)
self.assertGreater(len(df.columns), 0)
self.assertGreater(len(df), 0)
self.assertDataFrameValid(df)

def test_read_dbc_dbf(self):
df = read_dbc_dbf(dbc_file)
self.assertIsInstance(df, pd.DataFrame)
self.assertFalse(df.empty)
self.assertGreater(len(df.columns), 0)
self.assertGreater(len(df), 0)
self.assertDataFrameValid(df)

def test_dbf_to_csvgz(self):
temp_csvgz_file = str(data_files / "STPI2206.csv.gz")
dbf_to_csvgz(dbc_file, encoding='iso-8859-1')
dbf_to_csvgz(temp_dbf_file, encoding='iso-8859-1')
self.assertTrue(os.path.isfile(temp_csvgz_file))
# with gzip.open(temp_csvgz_file, "rt") as gzfile:
# df = pd.read_csv(gzfile)
# assert isinstance(df, pd.DataFrame)
# assert not df.empty
# assert len(df.columns) > 0
# assert len(df) > 0
with gzip.open(temp_csvgz_file, "rt") as gzfile:
df = pd.read_csv(gzfile)
assert isinstance(df, pd.DataFrame)
assert not df.empty
assert len(df.columns) > 2
assert len(df) > 0

@pytest.mark.skipif
def test_encoding(self):
Expand All @@ -66,40 +63,7 @@ def test_encoding(self):
detected_encoding, common_encodings
) # Check if detected encoding is in the list

def test_dbc_file_size(self):
"""
Test if a DBC file is malformed or truncated.
Check if the file size is a multiple of 4 bytes, which is the size of
a CAN message in a DBC file. Takes a file path as input and uses the
os.path.getsize function to get the file size in bytes.
The function then checks if the file size is a multiple of 4 bytes
by checking if the remainder of the division of the file size by 4
is not zero. If the file size is not a multiple of 4 bytes,
the function prints an error message indicating that the file
is malformed or truncated and returns False. Otherwise,
the function prints a message indicating that the file is
a valid DBC file and returns True.
"""
# Get the file size
file_size = os.path.getsize(dbc_file)

# Check if the file size is a multiple of 4 bytes
if file_size % 4 != 0:
self.fail(f"{dbc_file} is a malformed or truncated DBC file.")

print(f"{dbc_file} is a valid DBC file.")

def test_dbc_file_header(self):
"""
Reads the first 32 bytes of the file,
which contain the file header.
The function checks if the header is valid by verifying that
it starts with the bytes VJDB and contains a
byte sequence of 01 00 00 00 at offset 8. If the header is valid,
the function prints a message indicating that the file is
a valid DBC file and returns True. Otherwise,
it prints an error message and returns False.
"""
dbc_file = str(data_files / "STPI2206.dbc")
with open(dbc_file, 'rb') as dbc_file:
# Read the first 32 bytes of the file
Expand All @@ -117,6 +81,12 @@ def test_dbc_file_header(self):
print(f"{dbc_file} is a valid DBC file.")
return True

def assertDataFrameValid(self, df):
self.assertIsInstance(df, pd.DataFrame)
self.assertFalse(df.empty)
self.assertGreater(len(df.columns), 0)
self.assertGreater(len(df), 0)


if __name__ == '__main__':
unittest.main()

0 comments on commit fa7c471

Please sign in to comment.