Coder Social home page Coder Social logo

aiohttp-test's Introduction

aiohttp and peewee-async testing examples

The project is focused on testing aiohttp apps with pytest. Main points are: testing an asynchronous code which is using database; pytest fixtures for a database.

As the example of an aiohttp application I have made a simple REST API for a pool application from the standard Django tutorial.

Examples of tasks to be solved

  • Working with a database within tests
  • Creating pytest fixtures for Peewee models
  • Run database migrations automatically before tests
  • Testing an aiohttp code which is works with database using Peewee-async
  • A fixture which is helping to get the uri path of an appropriate resource (route_path())

Built With

  • aiohttp - asynchronous HTTP Client/Server for asyncio and Python
  • Postgresql - powerful, open source object-relational database system
  • Async-peewee - a library providing asynchronous interface powered by asyncio for peewee ORM
  • peewee-migrate - a simple migration engine for Peewee

Prerequisites

  • Python >= 3.5.3
  • Postgresql or MySQL

It really doesn't matter which relational database you'll use. MySQL or Postgresql will meet the requirements. Nevertheless the example uses Postgresql.

Getting Started

  • Clone the repository
  • Create a virtual environment with the appropriate version of Python
  • Activate the virtual environment and install packages from requirements.txt
  • Create a .env file in the project's root directory (.env.example file is provided as the example)
  • Create a database for the application. Example: pool
  • Create a test database with the postfix _test. Example: pool_test
  • Run the tests: pytest

Run tests

Run tests:

pytest

The tests are contain print() instructions to support a verbose mode so pytest may be running with -s flag:

pytest -s

Database Migrations

Migrations are run automatically on the application start up.

Examples of usage

Create

pw_migrate create --database "postgresql://[user]:[password]@[host]/[db_name]" --auto api.models [migration_name]

Run

All
pw_migrate migrate --database "postgresql://[user]:[password]@[host]/[db_name]"
Single
pw_migrate migrate --database "postgresql://[user]:[password]@[host]/[db_name]" --name [migration_name]

Rollback

pw_migrate rollback --database "postgresql://[user]:[password]@[host]/[db_name]" migration_name

Only one migration from the end would be cancelled

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Code style

  • PEP-8 (lines length - 80)

License

This project is licensed under the MIT License

aiohttp-test's People

Contributors

idzhalalov avatar islomsobirov avatar asia-coder avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.