def addstuff(): a1 = A(name="first a") a2 = A(name="second a") b1 = B(name="first b", a=a1) b2 = B(name="second b", a=a1) b3 = B(name="third b", a=a2) session = Session() session.add_all([a1, a2, b1, b2, b3]) session.commit()
async def test_custom_id(self): inst = A(pkey=1874, text='test_custom_id', n=1) await inst.save() self.assertEqual(inst.pkey, 1874) await A.get(1874) inst_2 = A(text='test_custom_id', n=2) inst_2.pkey = 1875 await inst_2.save() await A.get(1875)
async def test_select_sqla_core(self): inst = A(text='test_select_sqla_core', n=0) await inst.save() inst = await A.select( A.__table__.select().where(A.c.text == 'test_select_sqla_core')) self.assertIsNotNone(inst) self.assertEqual(inst.text, 'test_select_sqla_core')
async def test_select_raw_sql(self): inst = A(text='test_select_raw_sql', n=0) await inst.save() inst = await A.select('''select * from a where text = :text''', {'text': 'test_select_raw_sql'}) self.assertIsNotNone(inst) self.assertEqual(inst.text, 'test_select_raw_sql')
async def test_update(self): a_inst = A(text='test_update', n=0) await a_inst.save() a_inst.text = 'test_update2' await a_inst.save() a_inst = await A.select(A.c.text == 'test_update2') self.assertIsNotNone(a_inst)
async def main(): print('Simple insert') time_start = time() for i in range(ITERATIONS): await FoxOrm.db.execute(A.__table__.insert(), { 'text': 'test', 'n': i, }) print('- Databases', (time() - time_start) / ITERATIONS) time_start = time() for i in range(ITERATIONS): a_obj = A(text='test2', n=i) await a_obj.save() print('- FoxOrm', (time() - time_start) / ITERATIONS) print('Select all') time_start = time() for i in range(ITERATIONS): data = await FoxOrm.db.fetch_all( A.__table__.select().where(A.__table__.c.text == 'test')) print('- Databases', (time() - time_start) / ITERATIONS) time_start = time() for i in range(ITERATIONS): data = await A.select_all(A.c.text == 'test2') print('- FoxOrm', (time() - time_start) / ITERATIONS) time_start = time() for i in range(ITERATIONS): data = await A.select_all(A.c.text == 'test2', skip_parsing=True) print('- FoxOrm skip_parsing=True', (time() - time_start) / ITERATIONS)
async def test_insert(self): a_inst = A(text='test', n=0) await a_inst.save() self.assertIsNotNone(a_inst.pkey) b_inst = B(text2='test2', n=0) await b_inst.save() self.assertIsNotNone(b_inst.pkey)
async def test_delete(self): for i in range(10): inst = A(text='test_delete', n=i) await inst.save() await A.delete(A.c.text == 'test_delete') objs = await A.select_all(A.c.text == 'test_delete') self.assertEqual(len(objs), 0)
async def test_select_all(self): for i in range(10): inst = A(text='test_select_all', n=1874) await inst.save() objs: List[A] = await A.select_all(A.c.n == 1874) self.assertEqual(len(objs), 10) for obj in objs: self.assertEqual(obj.text, 'test_select_all')
def addstuff(): a1 = A(name="first a") a2 = A(name="second a") b1 = B(name="first b", a=a1) b2 = B(name="second b", a=a1) b3 = B(name="third b", a=a2) session = Session() session.add_all([a1, a2, b1, b2, b3]) version = models.Version() version.created = datetime.datetime.now() session.add(version) session.flush() version_id = version.version_id session.commit() session = Session() for op in session.query(models.Operation): op.version_id = version_id session.commit()
async def test_bad_operation(self): a_inst = A(text='test_bad_operation', n=0) await a_inst.save() with self.assertRaises(ValueError): a_inst.b_objs = 1874 with self.assertRaises(OrmException): a_inst.pkey = 1874 with self.assertRaises(OrmException): await a_inst.fetch_related('pkey')
async def test_m2m(self): a_inst = A(text='test_m2m', n=0) await a_inst.save() b_inst = B(text2='test_m2m', n=0) await b_inst.save() await a_inst.fetch_related('b_objs') self.assertEqual(len(a_inst.b_objs), 0) await a_inst.b_objs.add(b_inst) await b_inst.fetch_related('a_objs') self.assertEqual(len(b_inst.a_objs), 1) self.assertEqual(b_inst.a_objs[0].text, 'test_m2m')
async def test_order_by(self): for i in range(10): inst = A(text='test_order_by', n=i) await inst.save() objs: List[A] = await A.select_all(A.c.text == 'test_order_by', order_by=A.c.n) self.assertEqual(len(objs), 10) i = 0 for obj in objs: self.assertEqual(obj.text, 'test_order_by') self.assertEqual(obj.n, i) i += 1
async def test_m2m_contains(self): a_inst = A(text='test_m2m_contains', n=0) await a_inst.save() await a_inst.b_objs.fetch() for i in range(10): b_inst = B(text2='test_m2m_contains_' + str(i), n=0) await b_inst.save() a_inst.b_objs.add(b_inst) a_inst.b_objs.add(b_inst) last_id = a_inst.b_objs[-1].pkey self.assertIn(last_id, a_inst.b_objs) self.assertIn(a_inst.b_objs[-1], a_inst.b_objs) b_inst = await B.select((B.c.text2 == 'test_m2m_contains_0') & (B.c.n == 0)) self.assertIn(b_inst, a_inst.b_objs)
async def test_m2m_2(self): a_inst = A(text='test_m2m_2', n=0) await a_inst.save() b_inst = B(text2='test_m2m_2_bad', n=0) with self.assertRaises(OrmException): a_inst.b_objs.add(b_inst) for i in range(10): b_inst = B(text2='test_m2m_2_' + str(i), n=0) await b_inst.save() a_inst.b_objs.add(b_inst) self.assertEqual(await a_inst.b_objs.count(), 0) await a_inst.b_objs.save() self.assertEqual(await a_inst.b_objs.count(), 10) await a_inst.b_objs.fetch() self.assertEqual(len(a_inst.b_objs), 10) self.assertEqual(await a_inst.b_objs.count(), 10)
async def test_m2m_delete(self): a_inst = A(text='test_m2m_delete', n=0) await a_inst.save() await a_inst.b_objs.fetch() for i in range(10): b_inst = B(text2='test_m2m_delete_' + str(i), n=0) await b_inst.save() a_inst.b_objs.add(b_inst) a_inst.b_objs.add(b_inst) await a_inst.b_objs.save() self.assertEqual(len(a_inst.b_objs), 10) b_inst = await B.select(B.c.text2 == 'test_m2m_delete_3') a_inst.b_objs.delete(b_inst) a_inst.b_objs.delete(b_inst) self.assertNotIn(b_inst, a_inst.b_objs) a_inst_2 = await A.select(A.c.text == 'test_m2m_delete') await a_inst_2.b_objs.fetch() self.assertIn(b_inst, a_inst_2.b_objs) await a_inst.b_objs.save() await a_inst_2.b_objs.fetch() self.assertNotIn(b_inst, a_inst_2.b_objs)
async def index(): a_inst = A(text='test_fastapi', n=0) await a_inst.save() return a_inst
async def test_select(self): inst = A(text='test_select', n=0) await inst.save() inst = await A.select(A.c.text == 'test_select') self.assertIsNotNone(inst) self.assertEqual(inst.text, 'test_select')
async def test_empty_update(self): inst = A(text='test_empty_update', n=0) await inst.save() await inst.save()
async def test_recursive_serialization(self): inst = A(text='test_recursive_serialization', n=1) await inst.save() inst.recursive = RecursiveTest(a=[RecursiveTest2(a='123')]) await inst.save()
async def test_delete_inst(self): inst = A(text='test_delete_inst', n=0) await inst.save() await inst.delete() res = await A.exists(A.c.text == 'test_delete_inst') self.assertFalse(res)
async def test_count(self): for i in range(10): inst = A(text='test_count', n=1875) await inst.save() res = await A.count(A.c.n == 1875) self.assertEqual(res, 10)
async def test_exists(self): a_inst = A(text='test_exists', n=0) await a_inst.save() res = await A.exists(A.c.text == 'test_exists') self.assertTrue(res)