Skip to content

Commit

Permalink
JSON config for tests better than INI, it is respectful for types
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Apr 19, 2016
1 parent 164e9ec commit fb8be2f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Local config
tests.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ ORM powered by **[asyncio](https://docs.python.org/3/library/asyncio.html)**.
Overview
--------

- Works on Python 3.4+
- Has support for PostgreSQL via `aiopg`
- Has support for MySQL via `aiomysql`
- Single point high level async API
- Drop-in replacement for sync code, sync will remain sync
- Basic operations are supported
- Transactions support is present, yet not heavily tested
* Works on Python 3.4+
* Has support for PostgreSQL via `aiopg`
* Has support for MySQL via `aiomysql`
* Single point high level async API
* Drop-in replacement for sync code, sync will remain sync
* Basic operations are supported
* Transactions support is present, yet not heavily tested

The complete documentation:
http://peewee-async.readthedocs.org
Expand Down
61 changes: 46 additions & 15 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ peewee-async

Current state: **alpha**, yet API seems fine and mostly stable.

* Works on Python 3.3+
* Databases supported: PostgreSQL
* Provides drop-in replacement for synchronous code
In current version (0.5.x) new-high level API is introduced while older low-level API partially marked as deprecated.

* Works on Python 3.4+
* Has support for PostgreSQL via `aiopg`
* Has support for MySQL via `aiomysql`
* Single point high level async API
* Drop-in replacement for sync code, sync will remain sync
* Basic operations are supported
* Transactions support is present, yet not heavily tested

Source code hosted on `GitHub`_.

Expand All @@ -30,40 +36,65 @@ Quickstart
import peewee
import peewee_async
# Nothing special, just define model and database:
database = peewee_async.PostgresqlDatabase('test')
loop = asyncio.get_event_loop()
class TestModel(peewee.Model):
text = peewee.CharField()
class Meta:
database = database
# Create table synchronously!
# Look, sync code is working!
TestModel.create_table(True)
# This is optional: close sync connection
TestModel.create(text="Yo, I can do it sync!")
database.close()
@asyncio.coroutine
def my_handler():
TestModel.create(text="Yo, I can do it sync!")
yield from peewee_async.create_object(TestModel, text="Not bad. Watch this, I'm async!")
all_objects = yield from peewee_async.execute(TestModel.select())
# Create async models manager:
objects = peewee_async.Manager(database)
# No need for sync anymore!
database.allow_sync = False
async def handler():
await objects.create(TestModel, text="Not bad. Watch this, I'm async!")
all_objects = await objects.execute(TestModel.select())
for obj in all_objects:
print(obj.text)
loop.run_until_complete(database.connect_async(loop=loop))
loop.run_until_complete(my_handler())
loop = asyncio.get_event_loop()
loop.run_until_complete(handler())
loop.close()
# Clean up, can do it sync again:
with objects.allow_sync():
TestModel.drop_table(True)
# Expected output:"
# Yo, I can do it sync!
# Not bad. Watch this, I'm async!
Install
-------

Install latest version from PyPI:
Install latest version from PyPI.

For PostgreSQL:

.. code-block:: console
pip install peewee-async aiopg
For MySQL:

.. code-block:: console
pip install peewee-async
pip install peewee-async aiomysql
Install from sources
++++++++++++++++++++
Expand Down
4 changes: 2 additions & 2 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,8 @@ class PooledMySQLDatabase(MySQLDatabase):
http://peewee.readthedocs.org/en/latest/peewee/api.html#MySQLDatabase
"""
def init(self, database, **kwargs):
self.min_connections = self.connect_kwargs.pop('min_connections', 1)
self.max_connections = self.connect_kwargs.pop('max_connections', 10)
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 10)
super().init(database, **kwargs)

@property
Expand Down
5 changes: 0 additions & 5 deletions tests.ini.sample

This file was deleted.

44 changes: 44 additions & 0 deletions tests.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"postgres": {
"database": "test",
"user": "postgres",
"port": 5432,
"host": "127.0.0.1"
},

"postgres-ext": {
"database": "test2",
"user": "postgres",
"port": 5432,
"host": "127.0.0.1"
},

"postgres-pool": {
"database": "test",
"user": "postgres",
"host": "127.0.0.1",
"port": 5432,
"max_connections": 4
},

"postgres-pool-ext": {
"database": "test",
"user": "postgres",
"host": "127.0.0.1",
"port": 5432,
"max_connections": 4
},

"mysql": {
"database": "test",
"user": "root",
"host": "127.0.0.1"
},

"mysql-pool": {
"database": "test",
"user": "root",
"host": "127.0.0.1",
"max_connections": 4
}
}
78 changes: 38 additions & 40 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"""
import os
import json
import logging
import asyncio
import contextlib
import configparser
import sys
import urllib.parse
import unittest
Expand All @@ -23,21 +23,20 @@
##########

# logging.basicConfig(level=logging.DEBUG)
ini = configparser.ConfigParser()

deafults = {
defaults = {
'postgres': {
'database': 'test',
'host': '127.0.0.1',
# 'port': 5432,
'user': 'postgres',
},
'postgres-ext': {
'database': 'test',
'host': '127.0.0.1',
# 'port': 5432,
'user': 'postgres',
},
# 'postgres-ext': {
# 'database': 'test',
# 'host': '127.0.0.1',
# # 'port': 5432,
# 'user': 'postgres',
# },
'postgres-pool': {
'database': 'test',
'host': '127.0.0.1',
Expand Down Expand Up @@ -66,22 +65,24 @@
}
}

overrides = {}

try:
import aiopg
except ImportError:
print("aiopg is not installed, ignoring PostgreSQL tests")
for k in list(deafults.keys()):
for k in list(defaults.keys()):
if k.startswith('postgres'):
deafults.pop(k)
defaults.pop(k)


try:
import aiomysql
except ImportError:
print("aiomysql is not installed, ignoring MySQL tests")
for k in list(deafults.keys()):
for k in list(defaults.keys()):
if k.startswith('mysql'):
deafults.pop(k)
defaults.pop(k)


db_classes = {
Expand All @@ -95,21 +96,19 @@


def setUpModule():
ini.read(['tests.ini'])
try:
with open('tests.json', 'r') as f:
overrides.update(json.load(f))
except:
print("'tests.json' file not found, will use defaults")


def load_managers(*, managers=None, loop=None, only=None):
config = dict(deafults)

config = dict(defaults)
for k in list(config.keys()):
if only and not k in only:
continue

try:
config.update(dict(**ini[k]))
except KeyError:
pass

config[k].update(overrides.get(k, {}))
database = db_classes[k](**config[k])
managers[k] = peewee_async.Manager(database, loop=loop)

Expand Down Expand Up @@ -240,6 +239,23 @@ def test(objects):
################


class DatabaseTestCase(unittest.TestCase):
def test_deferred_init(self):
config = dict(defaults)
for k in list(config.keys()):
config[k].update(overrides.get(k, {}))

database = db_classes[k](None)
self.assertTrue(database.deferred)

database.init(**config[k])
self.assertTrue(not database.deferred)

TestModel._meta.database = database
TestModel.create_table(True)
TestModel.drop_table(True)


class ManagerTestCase(BaseManagerTestCase):
# only = ['postgres', 'postgres-ext', 'postgres-pool', 'postgres-pool-ext']
only = None
Expand Down Expand Up @@ -506,24 +522,6 @@ def test(objects):
self.run_with_managers(test)


class PostgresInitTestCase(unittest.TestCase):
def test_deferred_init(self):
config = dict(deafults)

for k in list(config.keys()):
try:
config.update(dict(**ini[k]))
except KeyError:
pass

db_class = db_classes[k]
database = db_class(None)
self.assertTrue(database.deferred)

database.init(**config[k])
self.assertTrue(not database.deferred)


#####################
# Python 3.5+ tests #
#####################
Expand Down

0 comments on commit fb8be2f

Please sign in to comment.