Skip to content

Commit

Permalink
feat: Improve performance of TestDBCChecks and iterate over all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esloch committed Sep 12, 2023
1 parent fa7c471 commit b59fdb4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
- name: Test with pytest
run: |
poetry run pytest -vv tests/
poetry run pytest -vv -k "ZIKABR21 or STPI2206 or sids" tests/
Binary file removed tests/data/CHIKBR19.dbc
Binary file not shown.
Binary file removed tests/data/ZIKABR16.DBC
Binary file not shown.
Binary file added tests/data/ZIKABR21.dbc
Binary file not shown.
159 changes: 83 additions & 76 deletions tests/test_pyreaddbc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gzip
import os
import unittest
from pathlib import Path

import chardet
Expand All @@ -11,82 +10,90 @@

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",

db_tests = [
"ZIKABR21",
"STPI2206",
"sids",
]


class TestDBCChecks(unittest.TestCase):
def test_read_dbc(self):
df = read_dbc(dbc_file)
self.assertDataFrameValid(df)

def test_dbc2dbf(self):
dbc2dbf(str(dbc_file), temp_dbf_file)
self.assertTrue(os.path.isfile(temp_dbf_file))
df = read_dbc_dbf(temp_dbf_file)
self.assertDataFrameValid(df)

def test_read_dbc_dbf(self):
df = read_dbc_dbf(dbc_file)
self.assertDataFrameValid(df)

def test_dbf_to_csvgz(self):
temp_csvgz_file = str(data_files / "STPI2206.csv.gz")
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) > 2
assert len(df) > 0

@pytest.mark.skipif
def test_encoding(self):
common_encodings = [
'utf-8',
'iso-8859-1',
'cp1252',
'Windows-1252',
] # Add more if needed

detected_encoding = chardet.detect(open(dbc_file, 'rb').read())[
'encoding'
]
self.assertIn(
detected_encoding, common_encodings
) # Check if detected encoding is in the list

def test_dbc_file_header(self):
dbc_file = str(data_files / "STPI2206.dbc")
with open(dbc_file, 'rb') as dbc_file:
# Read the first 32 bytes of the file
header = dbc_file.read(32)

# Check if the header is valid
if (
len(header) != 32
or header[0:4] != b'VJDB'
or header[8:12] != b'\x01\x00\x00\x00'
):
print(f"Error: {dbc_file} is not a valid DBC file.")
return False

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()
@pytest.mark.parametrize("db_test", db_tests)
def test_read_dbc(db_test):
dbc_file = str(data_files / f"{db_test}.dbc")
df = read_dbc(dbc_file)
assert_dataframe_valid(df)


@pytest.mark.parametrize("db_test", db_tests)
def test_dbc2dbf(db_test):
dbc_file = str(data_files / f"{db_test}.dbc")
temp_dbf_file = str(data_files / f"{db_test}.dbf")
dbc2dbf(dbc_file, temp_dbf_file)
assert os.path.isfile(temp_dbf_file)
df = read_dbc_dbf(temp_dbf_file)
assert_dataframe_valid(df)


@pytest.mark.parametrize("db_test", db_tests)
def test_read_dbc_dbf(db_test):
dbc_file = str(data_files / f"{db_test}.dbc")
df = read_dbc_dbf(dbc_file)
assert_dataframe_valid(df)


@pytest.mark.parametrize("db_test", db_tests)
def test_dbf_to_csvgz(db_test):
temp_dbf_file = str(data_files / f"{db_test}.dbf")
temp_csvgz_file = str(data_files / f"{db_test}.csv.gz")
dbf_to_csvgz(temp_dbf_file, encoding='iso-8859-1')
assert 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) > 2
assert len(df) > 0


@pytest.mark.parametrize("db_test", db_tests)
@pytest.mark.skipif
def test_encoding(db_test):
dbc_file = str(data_files / f"{db_test}.dbc")
common_encodings = [
'utf-8',
'iso-8859-1',
'cp1252',
'Windows-1252',
] # Add more if needed

detected_encoding = chardet.detect(open(dbc_file, 'rb').read())['encoding']
assert detected_encoding in common_encodings


@pytest.mark.parametrize("db_test", db_tests)
def test_dbc_file_header(db_test):
dbc_file = str(data_files / f"{db_test}.dbc")
with open(dbc_file, 'rb') as dbc_file:
# Read the first 32 bytes of the file
header = dbc_file.read(32)

# Check if the header is valid
if (
len(header) != 32
or header[0:4] != b'VJDB'
or header[8:12] != b'\x01\x00\x00\x00'
):
print(f"Error: {dbc_file} is not a valid DBC file.")
return False

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


def assert_dataframe_valid(df):
assert isinstance(df, pd.DataFrame)
assert not df.empty
assert len(df.columns) > 0
assert len(df) > 0

0 comments on commit b59fdb4

Please sign in to comment.