{"id":18507614,"url":"https://github.com/hspandher/django-test-addons","last_synced_at":"2025-04-09T03:31:18.917Z","repository":{"id":35431356,"uuid":"39697239","full_name":"hspandher/django-test-addons","owner":"hspandher","description":"Testing support for different database system like Mongo, Redis, Neo4j, Memcache, Django Rest Framework for django","archived":false,"fork":false,"pushed_at":"2019-03-25T14:37:06.000Z","size":822,"stargazers_count":20,"open_issues_count":3,"forks_count":12,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-03T12:48:28.058Z","etag":null,"topics":["django","django-rest-framework","memcached","mongodb","neo4j","redis","tdd","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hspandher.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-25T18:18:09.000Z","updated_at":"2020-07-16T09:27:45.000Z","dependencies_parsed_at":"2022-08-17T18:15:18.365Z","dependency_job_id":null,"html_url":"https://github.com/hspandher/django-test-addons","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hspandher%2Fdjango-test-addons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hspandher%2Fdjango-test-addons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hspandher%2Fdjango-test-addons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hspandher%2Fdjango-test-addons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hspandher","download_url":"https://codeload.github.com/hspandher/django-test-addons/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223291770,"owners_count":17120964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-rest-framework","memcached","mongodb","neo4j","redis","tdd","testing"],"created_at":"2024-11-06T15:09:31.720Z","updated_at":"2024-11-06T15:12:04.781Z","avatar_url":"https://github.com/hspandher.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"====================\nFull Documentation:\n====================\n    * `Read the docs \u003chttp://django-test-addons.readthedocs.org/en/latest/\u003e`_\n    * `Python Hosted \u003chttps://pythonhosted.org/django-test-addons/\u003e`_\n\n=========\nTutorial\n=========\n\nThis tutorial provides a step-by-step description on how to use django test addons for\ntesting different database systems.\n\nGetting Started\n================\nIt is recommended to have local installation of respective databases just for testing.\nStaging or shared database or any database with critical data should never be used in\ntesting, as database is cleaned after each test is ran. It is recommended to use a separate\nsettings file for testing.\n\n.. warning:: **Be Careful** to use correct settings for test databases. Using staging or any other database may result in cleaning of the entire database.\n\nIf you haven't installed django test addons already, use\n\n.. code-block:: console\n\n    pip install django-test-addons\n\nTesting Mongodb\n================\n\nDefining test settings\n----------------------\n\nMake sure you have running installation of mongodb and have mongoengine installed.\nJust specify the settings for connection to mongodb instance in the settings file.\nDefine *TEST_MONGO_DATABASE* dict in your test file containing connection information.\n\n**Example**:\n\nAdd this code to test settings file -\n\n.. code-block:: python\n\n    TEST_MONGO_DATABASE = {\n        'db': 'test',\n        'host': ['localhost'],\n        'port': 27017,\n    }\n\nMake sure to use same test database for all mongo database aliases. To clarify,\nsay you have following mongo connection settings in your development/production\nsettings containing two mongodb aliases.\n\n.. code-block:: python\n\n    MONGO_DATABASES = {\n        'default': {\n            'db': 'main',\n            'host': ['193.34.32.11'], # random development server\n            'port': 27017,\n        },\n        'miscellaneous': {\n            'DB_NAME': 'misc',\n            'HOST': ['193.34.32.11'],\n            'PORT': 27017,\n        }\n    }\n\nIn your test settings, make sure to disconnect all existing connections and connect\nall mongodb aliases to test db.\n\n.. code-block:: python\n\n    # import MONGO_DATABASES variable from development settings file or just use the\n    # variable if you are using single file for testing with some environment settings.\n\n    import mongoengine\n\n    TEST_MONGO_DATABASE = {\n        'db': 'test',\n        'host': ['localhost'],\n        'port': 27017,\n    }\n\n    map(lambda connection: mongoengine.connection.disconnect(connection), MONGO_DATABASES.keys())\n\n    MONGO_DATABASES = {connection: TEST_MONGO_DATABASE for connection in MONGO_DATABASES.keys()}\n\n    for connection_name, attrs in MONGO_DATABASES.items():\n        mongoengine.connect(**dict(zip(['alias'] + attrs.keys(), [connection_name] + attrs.values())))\n\nWriting Tests\n--------------\n\nJust import *MongoTestCase* from test_addons, and inherit test class from it.\n\n**Example**\n\n.. code-block:: python\n\n    import test_addons\n\n    class TestSomething(test_addons.MongoTestCase):\n\n        def test_instantiation(self):\n            pass\n\n\nTesting Memcache\n=================\n\nJust 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.\n\n**Example**\n\n.. code-block:: python\n\n    import test_addons\n\n    class TestSomething(test_addons.MongoTestCase):\n\n        CLEAR_CACHE = True\n\n        def test_instantiation(self):\n            pass\n\n\nTesting Redis\n==============\n\nDefining test settings\n-----------------------\n\nMake sure you have redis db installed and a running redis server. Just specify\n*TEST_CACHES* dictionary in your test settings containing redis connection info.\n\n**Example**:\n\n.. code-block:: python\n\n    TEST_CACHES = {\n        'default': {\n            \"BACKEND\": \"django_redis.cache.RedisCache\",\n            \"LOCATION\": \"127.0.0.1:6379:0\",\n            \"OPTIONS\": {\n                \"CLIENT_CLASS\": \"django_redis.client.DefaultClient\",\n            }\n        },\n        'redis1': {\n            \"BACKEND\": \"django_redis.cache.RedisCache\",\n            \"LOCATION\": \"127.0.0.1:6379:1\",\n            \"OPTIONS\": {\n                \"CLIENT_CLASS\": \"django_redis.client.DefaultClient\",\n            }\n        },\n    }\n\n.. 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.\n\nWriting Tests\n--------------\nJust import *RedisTestCase* from test_addons, and inherit test class from it.\n\n**Example**\n\n.. code-block:: python\n\n    import test_addons\n\n    class TestSomething(test_addons.RedisTestCase):\n\n        def test_instantiation(self):\n            pass\n\n\nTesting Neo4j Graph database\n=============================\n\nDefining test settings\n-----------------------\n\nMake sure you have neo4j graph installed and a running neo4j server. Just specify\n*NEO4J_TEST_LINK* pointing to ip address of running neo4j server in your test settings file.\n\n**Example**\n\n.. code-block:: python\n\n    NEO4J_TEST_LINK = 'http://localhost:7474/db/data'\n\n.. 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\n\nWriting Tests\n--------------\nJust import *Neo4jTestCase* from test_addons, and inherit test class from it.\n\n**Example**\n\n.. code-block:: python\n\n    import test_addons\n\n    class TestSomething(test_addons.Neo4jTestCase):\n\n        def test_instantiation(self):\n            pass\n\n\nTesting Django Rest Framework APIs\n===================================\nIt provides support for testing Django rest framework api's along with one or\nmore databases.\n\n.. 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.\n\nWriting Tests\n--------------\n\nJust import APITestCase for the specific database you are using (specify settings accordingly).\n\n*Available options are*:\n\n    * APIRedisTestCase\n    * APIMongoTestCase\n    * APINeo4jTestCase\n    * APIMongoRedisTestCase\n    * APIRedisMongoNeo4jTestCase\n\n**Example**\nSay we want to use test DRF apis along with mongodb.\n\n.. code-block:: python\n\n    import test_addons\n\n    class TestSomething(test_addons.APIMongoTestCase):\n\n        def test_instantiation(self):\n            pass\n\n\nComposite Testing\n==================\n\nOften multiple databases are used simulataneously, thereby creating the need of\ntesting them simulataneously. Just to cater this need, django test addons provide\ndifferent combinations of TestCases for respective database combinations.\n\nComposite Test Cases:\n-------------------------------\n\n    * MongoNeo4jTestCase\n    * MongoRedisTestCase\n    * RedisMongoNeo4jTestCase\n    * APIRedisTestCase\n    * APIMongoTestCase\n    * APINeo4jTestCase\n    * APIMongoRedisTestCase\n    * APIRedisMongoNeo4jTestCase\n\nFacing Issues\n=============\nMake sure you have defined settings exactly as mentioned. If you still can't resolve the issue, you can use `Django test addons mailing list \u003chttps://groups.google.com/forum/#!forum/django-test-addons\u003e`_ or raise an issue on `github \u003chttps://github.com/hspandher/django-test-addons\u003e`_  or just mail me directly at *hspandher@outlook.com*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhspandher%2Fdjango-test-addons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhspandher%2Fdjango-test-addons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhspandher%2Fdjango-test-addons/lists"}