|
| 1 | +# Copyright (c) 2018 Uber Technologies, Inc. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +from builtins import object |
| 22 | +import random |
| 23 | + |
| 24 | +import psycopg2 as psycopg2_client |
| 25 | + |
| 26 | +from basictracer import BasicTracer |
| 27 | +from basictracer.recorder import InMemoryRecorder |
| 28 | +import opentracing |
| 29 | +from opentracing.ext import tags |
| 30 | + |
| 31 | +from opentracing_instrumentation.client_hooks import _dbapi2 |
| 32 | +from opentracing_instrumentation.client_hooks import psycopg2 |
| 33 | + |
| 34 | +from sqlalchemy import ( |
| 35 | + Column, |
| 36 | + ForeignKey, |
| 37 | + Integer, |
| 38 | + MetaData, |
| 39 | + String, |
| 40 | + Table, |
| 41 | + create_engine, |
| 42 | +) |
| 43 | +from sqlalchemy.orm import mapper, sessionmaker |
| 44 | + |
| 45 | +import pytest |
| 46 | + |
| 47 | + |
| 48 | +POSTGRES_CONNECTION_STRING = 'postgresql://localhost/travis_ci_test' |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def engine(): |
| 53 | + try: |
| 54 | + yield create_engine(POSTGRES_CONNECTION_STRING) |
| 55 | + except: |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def session(): |
| 61 | + Session = sessionmaker() |
| 62 | + Session.configure(bind=engine) |
| 63 | + try: |
| 64 | + yield Session() |
| 65 | + except: |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture(autouse=True) |
| 70 | +def patch_postgres(): |
| 71 | + psycopg2.install_patches() |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture() |
| 75 | +def tracer(): |
| 76 | + t = BasicTracer(recorder=InMemoryRecorder()) |
| 77 | + old_tracer = opentracing.tracer |
| 78 | + opentracing.tracer = t |
| 79 | + |
| 80 | + try: |
| 81 | + yield t |
| 82 | + except: |
| 83 | + opentracing.tracer = old_tracer |
| 84 | + |
| 85 | + |
| 86 | +metadata = MetaData() |
| 87 | +user = Table('user', metadata, |
| 88 | + Column('id', Integer, primary_key=True), |
| 89 | + Column('name', String(50)), |
| 90 | + Column('fullname', String(50)), |
| 91 | + Column('password', String(12))) |
| 92 | + |
| 93 | + |
| 94 | +class User(object): |
| 95 | + |
| 96 | + def __init__(self, name, fullname, password): |
| 97 | + self.name = name |
| 98 | + self.fullname = fullname |
| 99 | + self.password = password |
| 100 | + |
| 101 | + |
| 102 | +mapper(User, user) |
| 103 | + |
| 104 | + |
| 105 | +def is_postgres_running(): |
| 106 | + try: |
| 107 | + with psycopg2_client.connect(POSTGRES_CONNECTION_STRING) as conn: |
| 108 | + pass |
| 109 | + return True |
| 110 | + except: |
| 111 | + return False |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.skipif(not is_postgres_running(), reason='Postgres is not running or cannot connect') |
| 115 | +def test_db(tracer, engine, session): |
| 116 | + metadata.create_all(engine) |
| 117 | + user1 = User(name='user1', fullname='User 1', password='password') |
| 118 | + user2 = User(name='user2', fullname='User 2', password='password') |
| 119 | + session.add(user1) |
| 120 | + session.add(user2) |
| 121 | + # If the test does not raised an error, it is passing |
0 commit comments