def test_member_add_record_from_dict(): app = create_test_app() update_aux_db_engine_discovery_map(app, force_update=True, aux_db_engine_name_prefix='test_aux_') asm = AuxSessionManager(app=app) seq_num = 1 engine = asm.get_engine(seq_num) session = asm.get(seq_num) session.query(MemberModel).delete() assert session.query(MemberModel).count() == 0 d = { 'ecosystem_id': 1, 'key_id': -123, 'address': '456', 'pub': 'a9823432', 'amount': 13.1, 'maxpay': 10.2, 'multi': 1, 'deleted': 1, 'blocked': 1, } MemberModel.add_record_from_dict(d, session=session) assert session.query(MemberModel).count() == 1 i = session.query(MemberModel).first() for k, v in d.items(): assert hasattr(i, k) val = getattr(i, k) if k in ('amount', 'maxpay'): assert float(v) == float(val) else: assert val == v
def test_add_all_records_for_all_ecosystems(): app = create_test_app() update_aux_db_engine_discovery_map(app, force_update=True, aux_db_engine_name_prefix='test_aux_') asm = AuxSessionManager(app=app) seq_num = 1 engine = asm.get_engine(seq_num) session = asm.get(seq_num) m_session = sm.get(seq_num) session.query(MemberModel).delete() assert session.query(MemberModel).count() == 0 assert m_session.query(EsKeys).count() > 0 MemberModel.add_all_records_for_all_ecosystems(EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) total = 0 for es in m_session.query(Ecosystem).all(): EsKeys.set_ecosystem(es.id) for m_item in m_session.query(EsKeys).all(): item = session.query(MemberModel).filter_by(key_id=m_item.id).one() assert_eskeys_and_member_instances_eq(m_item, item) total += m_session.query(EsKeys).count() assert session.query(MemberModel).count() == total
def test_update_from_main_model_instance_via_manager(): app = create_test_app() mhm = MemberHelperManager(app=app, engine_echo=True) sm = AuxSessionManager(app=app) seq_num = 1 mm_session = sm.get(seq_num) mm_engine = sm.get_engine(seq_num) create_tables([MemberModel], mm_engine, recreate_if_exists=True) len_mm_first = len(MemberModel.query.with_session(mm_session).all()) assert len_mm_first == 0 d = { 'ecosystem_id': 1, 'key_id': -123, 'address': '456', 'pub': 'a9823432', 'amount': 13.1, 'maxpay': 10.2, 'multi': 1, 'deleted': 1, 'blocked': 1, } MemberModel.add_record_from_dict(d, session=mm_session) assert len( MemberModel.query.with_session(mm_session).all()) == len_mm_first + 1 mm_instance = MemberModel.query.with_session(mm_session).all()[0] len_first = len(mhm.session.query(mhm.model).all()) assert len_first == 0 mhm.model.update_from_main_model_instance(mm_instance, session=mhm.session, main_model_session=mm_session, seq_num=seq_num, ecosystem_id=1) num_of_mhm_records = len(MemberModel.__table__.columns) - len( MemberHelperModel.get_drop_fields()) assert len(mhm.session.query( mhm.model).all()) == len_first + num_of_mhm_records print("---------------ALL: %s" % mhm.session.query(mhm.model).filter_by( seq_num=seq_num, id=mm_instance.id).all()) mhm.session.query(mhm.model).filter_by(seq_num=seq_num, id=mm_instance.id).delete() mhm.model.update_from_main_model_instance(mm_instance, session=mhm.session, main_model_session=mm_session, seq_num=seq_num, ecosystem_id=1) print("len of mhm: %s" % len(mhm.session.query(mhm.model).all()))
def test_member_add_record_from_model_instance(): app = create_test_app() update_aux_db_engine_discovery_map(app, force_update=True, aux_db_engine_name_prefix='test_aux_') asm = AuxSessionManager(app=app) seq_num = 1 engine = asm.get_engine(seq_num) session = asm.get(seq_num) m_session = sm.get(seq_num) session.query(MemberModel).delete() assert session.query(MemberModel).count() == 0 assert m_session.query(EsKeys).count() > 0 ecosystem_id = 1 EsKeys.set_ecosystem(ecosystem_id) m_instance = m_session.query(EsKeys).first() assert isinstance(m_instance, EsKeys) MemberModel.add_record_from_model_instance(m_instance, session=session) assert session.query(MemberModel).count() == 1 instance = session.query(MemberModel).first() assert_eskeys_and_member_instances_eq(m_instance, instance)
def test_compare_num_of_all_records_for_all_ecosystems(): app = create_test_app() update_aux_db_engine_discovery_map(app, force_update=True, aux_db_engine_name_prefix='test_aux_') asm = AuxSessionManager(app=app) seq_num = 1 engine = asm.get_engine(seq_num) session = asm.get(seq_num) m_session = sm.get(seq_num) session.query(MemberModel).delete() assert session.query(MemberModel).count() == 0 assert m_session.query(EsKeys).count() > 0 MemberModel.add_all_records_for_all_ecosystems(EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) l = session.query(MemberModel).count() assert l > 0 MemberModel.compare_num_of_all_records_for_all_ecosystems( EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) session.delete(session.query(MemberModel).first()) session.commit() l2 = session.query(MemberModel).count() assert l > l2 compare_num_error_caught = False try: MemberModel.compare_num_of_all_records_for_all_ecosystems( EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) except MemberModel.CompareNumError: compare_num_error_caught = True assert compare_num_error_caught
def nontest_update_all_records_for_all_ecosystems(): app = create_test_app() update_aux_db_engine_discovery_map(app, force_update=True, aux_db_engine_name_prefix='test_aux_') asm = AuxSessionManager(app=app) seq_num = 1 engine = asm.get_engine(seq_num) session = asm.get(seq_num) m_session = sm.get(seq_num) session.query(MemberModel).delete() assert session.query(MemberModel).count() == 0 assert m_session.query(EsKeys).count() > 0 MemberModel.add_all_records_for_all_ecosystems(EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) total = 0 for es in m_session.query(Ecosystem).all(): EsKeys.set_ecosystem(es.id) total += m_session.query(EsKeys).count() assert session.query(MemberModel).count() == total MemberModel.compare_num_of_all_records_for_all_ecosystems( EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) l = session.query(MemberModel).count() assert l > 0 session.delete(session.query(MemberModel).first()) l2 = session.query(MemberModel).count() assert l2 == l - 1 MemberModel.update_all_records_for_all_ecosystems( EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) l3 = session.query(MemberModel).count() assert l3 == l m_l = m_session.query(EsKeys).count() assert l3 == m_l if ALLOW_BACKEND_DISTRUCTIVE_TESTS: m_item = m_session.query(EsKeys).first() m_data = m_item.to_dict() m_item_new = EsKeys(**m_data) m_session.delete(m_item) m_session.commit() m_l2 = m_session.query(EsKeys).count() MemberModel.update_all_records_for_all_ecosystems( EsKeys, ecosystem_model=Ecosystem, session=session, model_session=m_session) l4 = session.query(MemberModel).count() assert l4 == m_l2 m_session.add(m_item_new) m_session.commit() m_l3 = m_session.query(EsKeys).count() assert m_l == m_l3