“SQLALCHEMY KONEKSI MOCK” Kode Jawaban

SQLALCHEMY KONEKSI MOCK

import unittest
import db
from unittest.mock import patch


class TestPosition(unittest.TestCase):

    @patch.object(db, '_create_engine')
    def test_get_all_pos(self, mock_sqlalchemy):
        args = {'DB': 'test'}
        db.get_all_pos(args)
        mock_sqlalchemy.assert_called_once()
        mock_sqlalchemy.assert_called_with({'DB': 'test'})


if __name__ == '__main__':
    unittest.main()
Tough Toucan

SQLALCHEMY KONEKSI MOCK

db.py

from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool


def _create_engine(app):
    impac_engine = create_engine(
        app['DB'],
        poolclass=NullPool  # this setting enables NOT to use Pooling, preventing from timeout issues.
    )
    return impac_engine


def get_all_pos(app):
    engine = _create_engine(app)
    qry = """SELECT DISTINCT id, name FROM p_t ORDER BY name ASC"""
    try:
        cursor = engine.execute(qry)
        rows = cursor.fetchall()
        return rows
    except Exception as re:
        raise re
Tough Toucan

Jawaban yang mirip dengan “SQLALCHEMY KONEKSI MOCK”

Pertanyaan yang mirip dengan “SQLALCHEMY KONEKSI MOCK”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya