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_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_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)
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_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_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_o2m_or_and(self): b_inst = B(text2='test_o2m_or_and', n=0) await b_inst.save() d_inst = D() await d_inst.save() c_inst_1 = C() c_inst_1.d_id = d_inst.pkey c_inst_1.b_id = b_inst.pkey await c_inst_1.save() c_inst_2 = C() c_inst_2.d_id = d_inst.pkey await c_inst_2.save() c_inst_3 = C() c_inst_3.b_id = b_inst.pkey await c_inst_3.save() await b_inst.c_objs.fetch() await d_inst.c_objs.fetch() self.assertEqual(len(d_inst.c_objs), 2) self.assertEqual(len(b_inst.c_objs), 2) self.assertEqual(len(b_inst.c_objs | d_inst.c_objs), 3) self.assertEqual(len(b_inst.c_objs & d_inst.c_objs), 1) both_have = (b_inst.c_objs & d_inst.c_objs)[0] self.assertEqual(both_have.pkey, c_inst_1.pkey)
async def test_o2m(self): b_inst = B(text2='test_o2m', n=0) await b_inst.save() c_inst = C() await c_inst.save() await b_inst.c_objs.fetch() self.assertEqual(len(b_inst.c_objs), 0) self.assertEqual(await b_inst.c_objs.count(), 0) c_inst.b_id = b_inst.pkey await c_inst.save() self.assertEqual(await b_inst.c_objs.count(), 1) self.assertEqual(len(b_inst.c_objs), 0) await b_inst.c_objs.fetch() self.assertEqual(len(b_inst.c_objs), 1) c_inst_2 = C() await c_inst_2.save() await b_inst.c_objs.add(c_inst_2) c_inst_2 = await C.get(c_inst_2.pkey) self.assertEqual(c_inst_2.b_id, b_inst.pkey) b_inst = await B.get(b_inst.pkey) await b_inst.c_objs.fetch() self.assertEqual(len(b_inst.c_objs), 2) self.assertEqual(await b_inst.c_objs.count(), 2)
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)