Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hspandher/django-test-addons
Testing support for different database system like Mongo, Redis, Neo4j, Memcache, Django Rest Framework for django
https://github.com/hspandher/django-test-addons
django django-rest-framework memcached mongodb neo4j redis tdd testing
Last synced: about 2 months ago
JSON representation
Testing support for different database system like Mongo, Redis, Neo4j, Memcache, Django Rest Framework for django
- Host: GitHub
- URL: https://github.com/hspandher/django-test-addons
- Owner: hspandher
- License: mit
- Created: 2015-07-25T18:18:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-03-25T14:37:06.000Z (almost 6 years ago)
- Last Synced: 2024-10-03T12:48:28.058Z (3 months ago)
- Topics: django, django-rest-framework, memcached, mongodb, neo4j, redis, tdd, testing
- Language: Python
- Homepage:
- Size: 803 KB
- Stars: 20
- Watchers: 6
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- License: LICENSE.txt
Awesome Lists containing this project
README
====================
Full Documentation:
====================
* `Read the docs `_
* `Python Hosted `_=========
Tutorial
=========This tutorial provides a step-by-step description on how to use django test addons for
testing different database systems.Getting Started
================
It is recommended to have local installation of respective databases just for testing.
Staging or shared database or any database with critical data should never be used in
testing, as database is cleaned after each test is ran. It is recommended to use a separate
settings file for testing... warning:: **Be Careful** to use correct settings for test databases. Using staging or any other database may result in cleaning of the entire database.
If you haven't installed django test addons already, use
.. code-block:: console
pip install django-test-addons
Testing Mongodb
================Defining test settings
----------------------Make sure you have running installation of mongodb and have mongoengine installed.
Just specify the settings for connection to mongodb instance in the settings file.
Define *TEST_MONGO_DATABASE* dict in your test file containing connection information.**Example**:
Add this code to test settings file -
.. code-block:: python
TEST_MONGO_DATABASE = {
'db': 'test',
'host': ['localhost'],
'port': 27017,
}Make sure to use same test database for all mongo database aliases. To clarify,
say you have following mongo connection settings in your development/production
settings containing two mongodb aliases... code-block:: python
MONGO_DATABASES = {
'default': {
'db': 'main',
'host': ['193.34.32.11'], # random development server
'port': 27017,
},
'miscellaneous': {
'DB_NAME': 'misc',
'HOST': ['193.34.32.11'],
'PORT': 27017,
}
}In your test settings, make sure to disconnect all existing connections and connect
all mongodb aliases to test db... code-block:: python
# import MONGO_DATABASES variable from development settings file or just use the
# variable if you are using single file for testing with some environment settings.import mongoengine
TEST_MONGO_DATABASE = {
'db': 'test',
'host': ['localhost'],
'port': 27017,
}map(lambda connection: mongoengine.connection.disconnect(connection), MONGO_DATABASES.keys())
MONGO_DATABASES = {connection: TEST_MONGO_DATABASE for connection in MONGO_DATABASES.keys()}
for connection_name, attrs in MONGO_DATABASES.items():
mongoengine.connect(**dict(zip(['alias'] + attrs.keys(), [connection_name] + attrs.values())))Writing Tests
--------------Just import *MongoTestCase* from test_addons, and inherit test class from it.
**Example**
.. code-block:: python
import test_addons
class TestSomething(test_addons.MongoTestCase):
def test_instantiation(self):
passTesting Memcache
=================Just specify *CLEAR_CACHE=TRUE* in your test class, if you want to clear cache too(it could be Memcache or Redis or any other caching framework that works with django). You must have CACHES configured in your test settings for this to work.
**Example**
.. code-block:: python
import test_addons
class TestSomething(test_addons.MongoTestCase):
CLEAR_CACHE = True
def test_instantiation(self):
passTesting Redis
==============Defining test settings
-----------------------Make sure you have redis db installed and a running redis server. Just specify
*TEST_CACHES* dictionary in your test settings containing redis connection info.**Example**:
.. code-block:: python
TEST_CACHES = {
'default': {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "127.0.0.1:6379:0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
'redis1': {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "127.0.0.1:6379:1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}.. note:: 'django_redis.cache.ShardClient' does not allow flushing all db as of now, so make sure not to use it. Sharding is not required in testing environment anyway.
Writing Tests
--------------
Just import *RedisTestCase* from test_addons, and inherit test class from it.**Example**
.. code-block:: python
import test_addons
class TestSomething(test_addons.RedisTestCase):
def test_instantiation(self):
passTesting Neo4j Graph database
=============================Defining test settings
-----------------------Make sure you have neo4j graph installed and a running neo4j server. Just specify
*NEO4J_TEST_LINK* pointing to ip address of running neo4j server in your test settings file.**Example**
.. code-block:: python
NEO4J_TEST_LINK = 'http://localhost:7474/db/data'
.. note:: Since neo4j 2.0, it requires authentication to connection to your neo4j server. Considering it is unnecessary for testing environment, make sure to set 'dbms.security.auth_enabled=false' in your neo4j-server.properties file
Writing Tests
--------------
Just import *Neo4jTestCase* from test_addons, and inherit test class from it.**Example**
.. code-block:: python
import test_addons
class TestSomething(test_addons.Neo4jTestCase):
def test_instantiation(self):
passTesting Django Rest Framework APIs
===================================
It provides support for testing Django rest framework api's along with one or
more databases... note:: Test cases described above would have worked for apis as well, but they use default Test Client provided by Django, whereas it uses Test Client provided by DRF having some additional facilities like forcing authentication.
Writing Tests
--------------Just import APITestCase for the specific database you are using (specify settings accordingly).
*Available options are*:
* APIRedisTestCase
* APIMongoTestCase
* APINeo4jTestCase
* APIMongoRedisTestCase
* APIRedisMongoNeo4jTestCase**Example**
Say we want to use test DRF apis along with mongodb... code-block:: python
import test_addons
class TestSomething(test_addons.APIMongoTestCase):
def test_instantiation(self):
passComposite Testing
==================Often multiple databases are used simulataneously, thereby creating the need of
testing them simulataneously. Just to cater this need, django test addons provide
different combinations of TestCases for respective database combinations.Composite Test Cases:
-------------------------------* MongoNeo4jTestCase
* MongoRedisTestCase
* RedisMongoNeo4jTestCase
* APIRedisTestCase
* APIMongoTestCase
* APINeo4jTestCase
* APIMongoRedisTestCase
* APIRedisMongoNeo4jTestCaseFacing Issues
=============
Make sure you have defined settings exactly as mentioned. If you still can't resolve the issue, you can use `Django test addons mailing list `_ or raise an issue on `github `_ or just mail me directly at *[email protected]*