Skip to content

Commit

Permalink
Add .travis.yml config
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Dec 6, 2015
1 parent d1cdf9f commit bb5e53c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python
python:
- "3.4"
- "3.5"
- "3.5-dev" # 3.5 development branch
- "nightly" # currently points to 3.6-dev
addons:
postgresql: "9.3"
before_script:
- psql -c 'create database test;' -U postgres
# command to install dependencies
install: "pip install -e ."
# command to run tests
script: "python setup.py test"
7 changes: 4 additions & 3 deletions tests.ini.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[tests]
db = test
pool_size = 4
;ext = 1
database = test
max_connections = 4
port = 5432
use_ext =
98 changes: 65 additions & 33 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@
Create tests.ini file to configure tests.
"""
import os
import asyncio
import configparser
import sys
import urllib.parse
import unittest
import uuid
import peewee

# Testing module
import peewee_async
import peewee_asyncext


class ProxyDatabase(object):
"""Proxy database for deferred initialization.
"""
def __init__(self):
self.conn = None

def __getattr__(self, attr):
if self.conn is None:
raise AttributeError('Cannot use uninitialized Proxy.')
return getattr(self.conn, attr)

def __setattr__(self, attr, value):
if attr == 'conn':
return super(ProxyDatabase, self).__setattr__(attr, value)
elif (self.conn is None) and (attr != 'conn'):
raise AttributeError('Cannot use uninitialized Proxy.')
else:
return setattr(self.conn, attr, value)

# Shortcuts
execute = peewee_async.execute
count = peewee_async.count
Expand All @@ -26,45 +46,57 @@
update_object = peewee_async.update_object
sync_unwanted = peewee_async.sync_unwanted

#
# Configure tests
#
# Globals
config = {}
database = ProxyDatabase()

ini_config = configparser.ConfigParser()
ini_config.read(['tests.ini'])

try:
config = dict(**ini_config['tests'])
except KeyError:
config = {}
def setUpModule():
global config
global database

config.setdefault('db', 'test')
config.setdefault('user', 'postgres')
config.setdefault('password', '')
ini_config = configparser.ConfigParser()
ini_config.read(['tests.ini'])

if 'pool_size' in config:
if config.get('ext', None):
db_cls = peewee_asyncext.PooledPostgresqlExtDatabase
try:
config = dict(**ini_config['tests'])
except KeyError:
pass

config.setdefault('database', 'test')
config.setdefault('host', '127.0.0.1')
config.setdefault('port', None)
config.setdefault('user', 'postgres')
config.setdefault('password', '')

if 'DATABASE_URL' in os.environ:
url = urllib.parse.urlparse(os.environ['DATABASE_URL'])
config['user'] = url.username or config['user']
config['host'] = url.host or config['host']
config['port'] = url.port or config['port']

db_cfg = config.copy()
use_ext = db_cfg.pop('use_ext', False)

if 'max_connections' in db_cfg:
db_cfg['max_connections'] = int(db_cfg['max_connections'])
use_pool = db_cfg['max_connections'] > 1
else:
db_cls = peewee_async.PooledPostgresqlDatabase

database = db_cls(config['db'],
user=config['user'],
password=config['password'],
max_connections=int(config['pool_size']))
else:
if config.get('ext', None):
db_cls = peewee_asyncext.PostgresqlExtDatabase
use_pool = False

if use_pool:
if use_ext:
db_cls = peewee_asyncext.PooledPostgresqlExtDatabase
else:
db_cls = peewee_async.PooledPostgresqlDatabase
else:
db_cls = peewee_async.PostgresqlDatabase
if use_ext:
db_cls = peewee_asyncext.PostgresqlExtDatabase
else:
db_cls = peewee_async.PostgresqlDatabase

database = db_cls(config['db'],
user=config['user'],
password=config['password'])
database.conn = db_cls(**db_cfg)

#
# Tests
#

class TestModel(peewee.Model):
text = peewee.CharField()
Expand Down

0 comments on commit bb5e53c

Please sign in to comment.