Spaces:
Sleeping
Sleeping
File size: 1,727 Bytes
333a6de 17cd01c 333a6de 17cd01c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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
|