|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import MagicMock |
| 16 | + |
| 17 | +import psycopg2.pool |
| 18 | +import sqlalchemy |
| 19 | + |
| 20 | + |
| 21 | +def test_main(): |
| 22 | + import main_mysql |
| 23 | + main_mysql.pymysql = MagicMock() |
| 24 | + fetchall_mock = main_mysql.pymysql.connect().cursor().__enter__().fetchall |
| 25 | + fetchall_mock.return_value = [['0']] |
| 26 | + |
| 27 | + main_mysql.app.testing = True |
| 28 | + client = main_mysql.app.test_client() |
| 29 | + |
| 30 | + r = client.get('/') |
| 31 | + assert r.status_code == 200 |
| 32 | + assert '0' in r.data.decode('utf-8') |
| 33 | + |
| 34 | + |
| 35 | +def test_main_pooling(): |
| 36 | + sqlalchemy.create_engine = MagicMock() |
| 37 | + |
| 38 | + import main_mysql_pooling |
| 39 | + |
| 40 | + cnx_mock = main_mysql_pooling.sqlalchemy.create_engine().connect() |
| 41 | + cnx_mock.execute().fetchall.return_value = [['0']] |
| 42 | + |
| 43 | + main_mysql_pooling.app.testing = True |
| 44 | + client = main_mysql_pooling.app.test_client() |
| 45 | + |
| 46 | + r = client.get('/') |
| 47 | + assert r.status_code == 200 |
| 48 | + assert '0' in r.data.decode('utf-8') |
| 49 | + |
| 50 | + |
| 51 | +def test_main_postgressql(): |
| 52 | + import main_postgres |
| 53 | + main_postgres.psycopg2.connect = MagicMock() |
| 54 | + mock_cursor = main_postgres.psycopg2.connect().cursor() |
| 55 | + mock_cursor.__enter__().fetchall.return_value = [['0']] |
| 56 | + |
| 57 | + main_postgres.app.testing = True |
| 58 | + client = main_postgres.app.test_client() |
| 59 | + |
| 60 | + r = client.get('/') |
| 61 | + assert r.status_code == 200 |
| 62 | + assert '0' in r.data.decode('utf-8') |
| 63 | + |
| 64 | + |
| 65 | +def test_main_postgressql_pooling(): |
| 66 | + psycopg2.pool.ThreadedConnectionPool = MagicMock() |
| 67 | + |
| 68 | + import main_postgres_pooling |
| 69 | + |
| 70 | + mock_pool = main_postgres_pooling.psycopg2.pool.ThreadedConnectionPool() |
| 71 | + mock_pool.getconn().cursor().__enter__().fetchall.return_value = [['0']] |
| 72 | + |
| 73 | + main_postgres_pooling.app.testing = True |
| 74 | + client = main_postgres_pooling.app.test_client() |
| 75 | + |
| 76 | + r = client.get('/') |
| 77 | + assert r.status_code == 200 |
| 78 | + assert '0' in r.data.decode('utf-8') |
0 commit comments