~mrp/krait

607bf08145b4cb1c816f74c5144556c57a35ad6b — Mark Penner 1 year, 3 months ago bc1fe82
add first test
3 files changed, 56 insertions(+), 0 deletions(-)

M krait/model.py
A krait/tests/__init__.py
A krait/tests/test_insert.py
M krait/model.py => krait/model.py +11 -0
@@ 186,3 186,14 @@ class dataModel:
            for column in columns:
                valid = valid and record.contains(column)
        return valid

    def get_next_id(self, table):
        """find the maximum id in a table and return the next id"""

        id_query = QSqlQuery()
        id_query.prepare("SELECT max(id) FROM :table")
        id_query.bindValue(":table", table)
        id_query.exec_()
        id_query.next()
        max_id = id_query.value(0)
        return int(max_id) + 1 if max_id else 1

A krait/tests/__init__.py => krait/tests/__init__.py +0 -0
A krait/tests/test_insert.py => krait/tests/test_insert.py +45 -0
@@ 0,0 1,45 @@
# -*- coding: utf-8 -*-
"""
@author: mark
"""

from PySide2 import QtSql

from krait.database import opendb
from krait.model import dataModel


def test_insert_inventory():
    """test inserting a new inventory record"""
    opendb(":memory:")
    data_model = dataModel()
    next_id = data_model.get_next_id("inventory")
    date = "2023-02-01"
    country = "Mexico"
    region = "Chiapas"
    weight = 9100
    price = 100
    notes = "note field"
    data_model.addInventory(
        {
            "date": date,
            "country": country,
            "region": region,
            "weight": weight,
            "price": price,
            "notes": notes,
        }
    )
    query = QtSql.QSqlQuery()
    query.prepare(
        f"SELECT id, date, country, region, weight, price, notes FROM inventory WHERE id == {next_id}"
    )
    query.exec_()
    query.next()
    assert query.value(0) == next_id
    assert query.value(1) == date
    assert query.value(2) == country
    assert query.value(3) == region
    assert query.value(4) == weight
    assert query.value(5) == price
    assert query.value(6) == notes