Skip to content

Commit

Permalink
added non-US unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpeckham committed Mar 5, 2024
1 parent 6078875 commit 20df81a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/vin/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
DATABASE_PATH = str(files("vin").joinpath("vehicle.db"))


def regex(value, pattern) -> bool:
def regexp_shim(value, pattern) -> bool:
"""REGEXP shim for SQLite versions bundled with Python 3.11 and earlier"""
match = re.match(pattern, value)
# print(f"value {value} pattern {pattern} {'match' if match else ''}")
return match is not None


connection = sqlite3.connect(DATABASE_PATH, detect_types=sqlite3.PARSE_DECLTYPES)
connection.create_function("REGEXP", 2, regexp_shim)
regex_expression_placeholder = "REGEXP(?, pattern.vds)"

connection.row_factory = sqlite3.Row
connection.create_function("REGEXP", 2, regex)


def query(sql: str, args: tuple = ()) -> list[sqlite3.Row]:
Expand Down Expand Up @@ -101,7 +103,7 @@ def decode_vin(wmi: str, vds: str, model_year: int | None = None) -> dict | None
return None


DECODE_VIN_SQL = """
DECODE_VIN_SQL = f"""
select
pattern.id,
pattern.vds,
Expand Down Expand Up @@ -132,15 +134,15 @@ def decode_vin(wmi: str, vds: str, model_year: int | None = None) -> dict | None
where
pattern.wmi = ?
and ? between pattern.from_year and pattern.to_year
and REGEXP (?, pattern.vds)
and {regex_expression_placeholder}
order by
pattern.from_year desc,
coalesce(pattern.updated, pattern.created) desc,
pattern.id asc;
"""
"""Sort order is important. Best match and most recent patterns on top."""

DECODE_VIN_WITHOUT_MODEL_YEAR_SQL = """
DECODE_VIN_WITHOUT_MODEL_YEAR_SQL = f"""
select
pattern.id,
pattern.vds,
Expand Down Expand Up @@ -170,7 +172,7 @@ def decode_vin(wmi: str, vds: str, model_year: int | None = None) -> dict | None
left join electrification_level on electrification_level.id = pattern.electrification_level_id
where
pattern.wmi = ?
and REGEXP (?, pattern.vds)
and {regex_expression_placeholder}
order by
pattern.from_year desc,
coalesce(pattern.updated, pattern.created) desc,
Expand Down
20 changes: 20 additions & 0 deletions tests/cars/test_europe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,23 @@ def test_italy():
v = VIN("JTMW53FV60D023016", fix_check_digit=True, decode_model_year=False)
assert v.make == "Toyota"
assert v.model == "RAV4"


def test_kia():
v = VIN("KNAPX81GBNK005247", fix_check_digit=True, decode_model_year=False)
assert v.body_class == "Wagon"
assert v.make == "Kia"
assert v.manufacturer == "Kia Corporation"
assert v.model == ""
assert v.series == "GLS / JSL / TAX (Middle Grade)"
assert v.trim == ""


def test_genesis():
v = VIN("KMTG341ABLU064324", fix_check_digit=True, decode_model_year=False)
assert v.body_class == "Sedan/Saloon"
assert v.make == "Genesis"
assert v.manufacturer == "Hyundai Motor Co"
assert v.model == "G70"
assert v.series == "2.0T, 2.0T Sport Prestige"
assert v.trim == ""

0 comments on commit 20df81a

Please sign in to comment.