コード例 #1
0
    def test_get(self, client):
        """
        insert data in the database, retrieve it with a GET request,
        and check they are the same
        :param client:
        :return:
        """
        connect = CoreConnect()
        cursor = connect.db.cursor()
        # create data to insert in the db
        date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        tid = uuid.uuid4()
        sid = 3
        rid = 9
        amt = 78
        status = 'OK'
        cursor.execute(
            f"INSERT INTO txs (date,tid,rid,sid,amt,status) VALUES ('{date}','{tid}','{rid}','{sid}',{amt},'{status}') ;")
        connect.db.commit()
        # retrieve the id of the newly inserted row
        cursor.execute('select LAST_INSERT_ID() ;')
        id = cursor.fetchone()
        id = id.get('LAST_INSERT_ID()')

        # use the API to retrieve the same data
        res = client.get(url_for('transactions_get_wallet', id=id))
        assert res.status_code == 200
        assert res.json == {'amt': amt, 'sid': sid, 'rid': rid, 'status': status, 'tid': str(
            tid), 'date': date}
コード例 #2
0
    def setup_method(self, test_method):
        """
            each test should start from a deterministic state
            we just clean the txs the table

            the other way would be to prevent committing,
        """
        connect = CoreConnect()
        cursor = connect.db.cursor()
        cursor.execute('DELETE FROM txs ;')
        connect.db.commit()
コード例 #3
0
def check_row_error(id):
    """
    checks that the row with id 'id' has the status set as ERROR
    :param id:
    :return:
    """
    connect = CoreConnect()
    cursor = connect.db.cursor()
    cursor.execute(f'SELECT status from txs where id = {id};')
    row = cursor.fetchone()
    assert row['status'] == 'ERROR'
コード例 #4
0
def check_row(id, request, status):
    """
    a helper function that checks that the row in the database with id 'id'
    is equal to the dictionary request
    :param id: the id of the tx
    :param request: the data to compare
    :param status: the status to comapre
    :return:
    """
    connect = CoreConnect()
    cursor = connect.db.cursor()
    cursor.execute(f'SELECT tid,rid,sid,amt,status from txs where id = {id};')
    row = cursor.fetchone()
    assert row['amt'] == request['amt']
    assert row['rid'] == request['rid']
    assert row['sid'] == request['sid']
    assert row['tid'] == str(request['tid'])
    assert row['status'] == status
コード例 #5
0
    def get(self, id):
        """
        Get an existing tx or error 404 - Not Found -
            this is regardless of the status of the tx : a tx with status ERROR will be returned
            the same way as a tx with status OK
        ---
        tags:
          - transaction
        parameters:
          - in: body
            name: id
            required: true
            type: int
        responses:
          200:
            description: record found and returned
          404:
            description: no record found for that id
          406:
            description: input data is incorrect
          503:
            description: Unable to get wallet, Status -  Error
        """

        try:
            connect = CoreConnect()
            cursor = connect.db.cursor()
            # query
            query = 'SELECT date,tid,rid,sid,amt,status FROM txs where id = %s;'
            cursor.execute(query, [id])
            # get oner row
            row = cursor.fetchone()
            # at this point, the request is successful. It returns either one or zero row
            if row is None:
                return {'id': id, 'msg': 'no row', 'project': 'db'}, 404
            row['date'] = row['date'].strftime('%Y-%m-%d %H:%M:%S')
            return row, 200
        except Exception as e:
            return {'msg': f'Error: {str(e)}', 'project': 'db'}, 503
コード例 #6
0
            def insert_transaction_in_db(status):
                """
                insert a row in the database,
                :param status:
                tid, sid, rid and amt provided as closure
                :return:
                """
                connect = CoreConnect()
                cursor = connect.db.cursor()
                date = datetime.datetime.now()
                query = 'INSERT INTO txs (date,tid,rid,sid,amt,status) VALUES (%s, %s, %s, %s, %s, %s)'
                cursor.execute(
                    query,
                    [date, tid,
                     str(rid), str(sid),
                     str(amt), status])

                connect.db.commit()
                # we want the POST request to return the id of the created row
                cursor.execute('select LAST_INSERT_ID() ;')
                id = cursor.fetchone()
                id = id.get('LAST_INSERT_ID()')
                return id
コード例 #7
0
 def get(self):
     """
     Check health of the DB Connection
     ---
     tags:
       - core
     responses:
       200:
         description: Health status of the DB Connection. Get total of TXS
       503:
         description: Error + Type of error
     """
     try:
         connect = CoreConnect()
         print('HealthCheck CoreConnect', connect.db)
         cursor = connect.db.cursor()
         cursor.execute("""
             CREATE TABLE IF NOT EXISTS txs (
               id INT AUTO_INCREMENT PRIMARY KEY,
               date datetime ,
               tid varchar(40) NOT NULL,
               rid int NOT NULL,
               sid int NOT NULL,
               amt int NOT NULL,
               status varchar(40)
             );
             """)
         cursor.execute('SELECT count(*) FROM txs LIMIT 1')
         result = cursor.fetchone()
         return {'health': 'ok', 'project': 'db', 'total_txs': result}, 200
     except Exception as e:
         return {
             'health': 'error',
             'msg': 'Error: {}'.format(str(e)),
             'project': 'db'
         }, 503
コード例 #8
0
 def setup_method(self, test_method):
     connect = CoreConnect()
     cursor = connect.db.cursor()
     cursor.execute('DELETE FROM txs ;')
     connect.db.commit()