from fasthtml.common import * from sqlite_minutils.db import Database import json import eval_code def test_some_sqldb_inmem(): db = Database(memory=True) assert db is not None db_tables = db.t session_states = db_tables.session_states if session_states not in db.t: session_states.create(prompt=str, session_id=str, id=int, folder=str, pk='id') Session_state = session_states.dataclass() sess = Session_state(prompt="Some prompt", folder="Somewhere") session_states.insert(sess) session_states.insert(sess) session_states.insert(sess) sess2 = Session_state(prompt="Some prompt", folder="Somewhere new") # insert AND get the newly inserted ID g = session_states.insert(sess2) assert g.id > 3 g.prompt = "New prompt" # Update and check if it can be retrieved session_states.update(g) # retrieval newg = session_states(where=f"id={g.id}") assert len(newg) == 1 assert newg[0].prompt == "New prompt" def test_sqlite_jsonops(): db = Database(memory=True) ans_js = eval_code.eval_the_piece_of_c_code(None, "") enh_ans_js = eval_code.add_evaluation_fields_on_js_answer(ans_js) qa_table = db.t.question_answers.create(id=int, session_id=str, question=str, answerjs=str, pk="id") qa_cls = qa_table.dataclass() qa1 = qa_cls(question="Question", answerjs=enh_ans_js) assert qa1.id is None qa1 = qa_table.insert(qa1) assert qa1.id >= 0 newans = json.loads(qa1.answerjs) newans[2]["EVAL"] = eval_code.EVAL_ANSWER_POSITIVE qa1.answerjs = newans qa_table.update(qa1) got_qa = qa_table(where=f"id == {qa1.id}") assert "EVAL\": 1" in got_qa[0].answerjs