def test_manager_create_tables(): manager = DBManager(in_memory=True) with patch('db_manager.db_manager.Base.metadata.create_all' ) as mocked_create_all: manager.create_tables() mocked_create_all.assert_called()
def test_state_responder_constructor(): manager = DBManager(in_memory=True) manager.create_tables() responder = DBResponder(manager, Responder(), "ledger_right") assert responder.manager == manager assert responder.ledgerId == "ledger_right"
async def test_state_initiator_commit_transfer(): manager = DBManager(in_memory=True) manager.create_tables() initiator = DBInitiator(manager, Initiator(), "ledger_left") transfer = Transfer() transfer.data = {} transfer.data["assetId"] = 123 with patch("sofie_asset_transfer.interfaces.Initiator.commit_transfer", return_value=True) as mock_commit_transfer: with patch( "sofie_asset_transfer.state_implementation.DBInitiator.store_commit" ) as mocked_state_commit: await initiator.commit_transfer(transfer) mocked_state_commit.assert_called_with(123)
async def test_state_responder_receive_transfer(): manager = DBManager(in_memory=True) manager.create_tables() responder = DBResponder(manager, Responder(), "ledger_right") transfer = Transfer() transfer.data = {} transfer.data["assetId"] = 123 with patch("sofie_asset_transfer.interfaces.Responder.receive_transfer", return_value=True) as mock_receive_transfer: with patch( "sofie_asset_transfer.state_implementation.DBResponder.store_accept" ) as mocked_state_accept: await responder.receive_transfer(transfer) mocked_state_accept.assert_called_with(123)
async def test_state_initiator_get_transfers(): manager = DBManager(in_memory=True) manager.create_tables() initiator = DBInitiator(manager, Initiator(), "ledger_left") transfer = Transfer() transfer.data = {} transfer.data["assetId"] = 123 with patch("sofie_asset_transfer.interfaces.Initiator.get_transfers", return_value=[transfer]) as mock_get_transfers: with patch("db_manager.db_manager.DBManager.update_rows" ) as mocked_update: await initiator.get_transfers() mocked_update.assert_called_with(initiator.ledgerId, [123], "Transfer Out")
def test_manager_query_by_state(): manager = DBManager(in_memory=True) manager.create_tables() assetIds = [123, 1234, 12345] states_left = ["s1", "s2", "s1"] states_right = ["s2", "s2", "s1"] manager.insert_row(assetIds[0], states_left[0], "o", states_right[0], "o") manager.insert_row(assetIds[1], states_left[1], "o", states_right[1], "o") manager.insert_row(assetIds[2], states_left[2], "o", states_right[2], "o") ids = manager.query_by_state("ledger_left", "s1") assert ids[0].assetId == 123 assert ids[1].assetId == 12345 ids = manager.query_by_state("ledger_right", "s2") assert ids[0].assetId == 123 assert ids[1].assetId == 1234
def test_manager_update_rows(): manager = DBManager(in_memory=True) manager.create_tables() assetIds = [123, 1234] states_left = ["sl1", "sl2"] states_right = ["sr1", "sr2"] owners_left = ["ol1", "ol2"] owners_right = ["or1", "or2"] manager.insert_row(assetIds[0], states_left[0], owners_left[0], states_right[0], owners_right[0]) manager.insert_row(assetIds[1], states_left[1], owners_left[1], states_right[1], owners_right[1]) new_states = "sl_new" manager.update_rows("ledger_left", assetIds, new_states) left = manager.session.query(db_manager.LedgerLeft).all() assert left[0].state == "sl_new" assert left[1].state == "sl_new"
def help_setup_initiator_and_insert_row(assetId): manager = DBManager(in_memory=True) manager.create_tables() initiator = DBInitiator(manager, Initiator(), "ledger_left") manager.insert_row(assetId, "s1", "test_owner", "s2", "test_owner") return initiator
def test_manager_delete_row(): manager = DBManager(in_memory=True) manager.create_tables() assetId = 123 state_left = "state_left" state_right = "state_right" owner_left = "owner_left" owner_right = "owner_right" manager.insert_row(assetId, state_left, owner_left, state_right, owner_right) manager.delete_row(assetId) left = manager.session.query(db_manager.LedgerLeft).first() right = manager.session.query(db_manager.LedgerRight).first() assert left is None assert right is None
def test_responder_store_accept(): manager = DBManager(in_memory=True) manager.create_tables() responder = DBResponder(manager, Responder(), "ledger_right") assetId = 123 manager.insert_row(assetId, "s1", "test_owner", "s2", "test_owner") with patch('db_manager.db_manager.DBManager.update_rows') as mocked_update: responder.store_accept(assetId) mocked_update.assert_called_with(responder.ledgerId, [assetId], "Here")
def get_user_by_login_and_password( login: str, password: str, config: Dict[str, Union[str, int]]) -> Optional[List[Dict[str, Any]]]: with DBManager(config) as cursor: _sql = """ select u.login, u.password, ug.name from joom.user as u join joom.user_group ug on u.group_name = ug.name where u.login='******' and u.password='******' """.format(login, password) cursor.execute(_sql) sql_result = cursor.fetchall() if sql_result: return SQLDataProvider.sql_response_to_list( ['login', 'password', 'group'], sql_result) else: return None
def test_manager_insert_row(): manager = DBManager(in_memory=True) manager.create_tables() assetId = 123 state_left = "state_left" state_right = "state_right" owner_left = "owner_left" owner_right = "owner_right" manager.insert_row(assetId, state_left, owner_left, state_right, owner_right) left = manager.session.query(db_manager.LedgerLeft).first() right = manager.session.query(db_manager.LedgerRight).first() assert left.assetId == assetId assert left.owner == owner_left assert left.state == state_left assert right.assetId == assetId assert right.owner == owner_right assert right.state == state_right
def test_manager_creation(): manager = DBManager(in_memory=True) assert manager.session is not None assert manager.engine is not None
app.config['db_config'] = db_config from blueprints.auth import auth app.register_blueprint(auth, prefix='/') # @app.route('/handle_post', methods=['POST', 'GET']) # def handler(): # data = request.data.decode('utf-8') # делаем строку из байтов согласно кодировке # json_data = json.loads(data) # делаем json из стркои # return json_data @app.route('/') def index(): if 'login' not in session: return redirect(url_for('auth_bp.login')) else: return render_template('index.html') with DBManager(db_config) as cursor: cursor.execute("select version()") sql_result = cursor.fetchall() print(sql_result) if __name__ == '__main__': app.run(host='127.0.0.1', port=5001)
def _current_time_(): time_now = int(time.time()) time_local = time.localtime(time_now) date = time.strftime("%Y-%m-%d %H:%M:%S", time_local) return date def _merge_(param=None): d1 = {'id': '001', 'date': 'mmm'} d2 = {**d1, **param} print("MERGE" + str(d2)) if __name__ == '__main__': print("test_DB") dbManager = DBManager(host='127.0.0.1') # dbManager.remove({Key.vendor.value: 'VendorA'}) issue: #000001 dbManager.remove({Key.vendor.value: 'VendorA', Key.group.value: 'GroupA'}) dbManager.remove({Key.vendor.value: 'VendorA', Key.group.value: 'GroupB'}) dbManager.insert(vid_gid={ Key.vendor.value: 'VendorA', Key.group.value: 'GroupA' }, document={ Key.id.value: "001", Key.nick.value: "Ronald", Key.role.value: 'pusher', Key.date.value: _current_time_() }) dbManager.insert(vid_gid={
def __init__(self, url=config['mongo']['server_url'], port=config['mongo']['server_port']): self._db_manager = DBManager(host=url, port=port)
class OnlineNodes(object): def __init__(self, url=config['mongo']['server_url'], port=config['mongo']['server_port']): self._db_manager = DBManager(host=url, port=port) def reset(self): # clear the data base each time mc bootup dblist = self._db_manager.get_db_list() for d in dblist: #Waring: this host should not name a db start with 'V' if d.startswith('V'): self._db_manager.remove(vid_gid_nid={Key.vendor.value: d}) def find_one(self, source_tag): """ :param source_tag: :return: a dic """ vid, gid, nid = source_tag.split('_') node_info = self._db_manager.query(vid_gid_nid={ Key.vendor.value: vid, Key.group.value: gid, Key.node.value: nid }) if len(node_info) == 0: return None else: return node_info[0] def find_role(self, source_tag, role): vid, gid, nid = source_tag.split('_') role_info = self._db_manager.query(vid_gid_nid={ Key.vendor.value: vid, Key.group.value: gid }, condition={Key.role.value: role}) return role_info #return str(role_info) def insert(self, source_tag, document): vid, gid, nid = source_tag.split('_') self._db_manager.insert(vid_gid={ Key.vendor.value: vid, Key.group.value: gid }, document=document) def remove(self, source_tag): vid, gid, nid = source_tag.split('_') self._db_manager.remove(vid_gid_nid={ Key.vendor.value: vid, Key.group.value: gid, Key.node.value: nid }) def update(self, source_tag, field, value): vid, gid, nid = source_tag.split('_') self._db_manager.update(vid_gid_nid={ Key.vendor.value: vid, Key.group.value: gid, Key.node.value: nid }, key_value={field: value})