These are chat archives for django/django

20th
Jun 2016
Zeeshan Asgar
@asgarzee
Jun 20 2016 06:04

hi guys,

I m writing test cases in django. I have a separate settings file for the tests and a separate database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': join(PROJECT_ROOT, 'run', 'TEST.sqlite3'),
    },
}

I want to use this database while testing. I am able to override the behaviour of django testcases of creating/destroying the database.
Now how i can use this database in my test cases.

Thanks.

mpachas
@mpachas
Jun 20 2016 06:08
@asgarzee you need to use that settings when running your tests
python manage.py test tests.py --settings=test_settings_file
Zeeshan Asgar
@asgarzee
Jun 20 2016 06:11
okay let me check
Zeeshan Asgar
@asgarzee
Jun 20 2016 06:41
it's not using this database @mpachas
(pdb) connections.__dict__

{'databases': {'default': {'TIME_ZONE': None, 'PORT': '', 'PASSWORD': '', 'HOST': '', 'TEST': {'CHARSET': None, 'MIRROR': None, 'NAME': None, 'COLLATION': None}, 'USER': '', 'NAME': '/run/TEST.sqlite3', 'OPTIONS': {}, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.sqlite3', 'ATOMIC_REQUESTS': False}}, '_databases': {'default': {'TIME_ZONE': None, 'PORT': '', 'PASSWORD': '', 'HOST': '', 'TEST': {'CHARSET': None, 'MIRROR': None, 'NAME': None, 'COLLATION': None}, 'USER': '', 'NAME': '/run/TEST.sqlite3', 'OPTIONS': {}, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.sqlite3', 'ATOMIC_REQUESTS': False}}, '_connections': <_thread._local object at 0x7f6cdf0c5f10>}
I am getting this. @mpachas
Zeeshan Asgar
@asgarzee
Jun 20 2016 06:47

@mpachas
what i m doing is I am hitting an api. This api is saving a record in the db('TEST.sqlite3'). Then in my testcase method I am accessing this created record using

order = self.model.objects.filter(id="45125")

and then I am deleting this record using

order.delete()

and I am using the following code to not create/destroy db:

from django.test.runner import DiscoverRunner


class NoDbTestRunner(DiscoverRunner):
    """ A test runner to test without database creation """

    def setup_databases(self, **kwargs):
        """ Override the database creation defined in parent class """
        pass

    def teardown_databases(self, old_config, **kwargs):
        """ Override the database teardown defined in parent class """
        pass

In pdb I can check that it is deleting the record but It is not reflecting this in the TEST.sqlite3 db.

Robin Papa
@robinpapa
Jun 20 2016 08:15
Hey! I need to pass a lot of filters to a query, what is the best way to do this? Add headers to the request and manage the response in the view? Or add the parameters to the request url and manage the response in the view?
what is best practice? :)
Filip Figiel
@megapctr
Jun 20 2016 09:20
@robinpapa you usually do this stuff via querystring
?foo=bar&bar=baz
check out django-filters, it may help you with building the filters you need
harshit-j
@harshit-j
Jun 20 2016 10:05
Is it necessary to delete modelform instances from modelformsets which I don't save? I am saving only the instances where the text boxes are filled and are not empty.
Gheorghita BUTNARU
@Gheorghita
Jun 20 2016 12:04
hello
anyone worked at / with freeIPA plugins?