How to Mock a Django model query_set in a unittest without accessing the database?
up vote
0
down vote
favorite
I have the following function:
def build_dict():
app_config = {}
for keypair in models.Applify.objects.all():
app_config.update({keypair.key: keypair.value})
return app_config
What I am trying to do is have a test file where I am faking the Applify queryset. So far what I have that is not working is this:
test_mock_list = [
mock.Mock(id=1, key='something_1', value=False),
mock.Mock(id=2, key='something_2', value=False)
]
with mock.patch('models.Applify.objects.all', return_value=test_mock_list):
# perform assertion test.
What I am getting is an error saying.
Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
I am not trying to actually connect to the database, so I don't want to use the @pytest.mark.django_db
decorator. So how can I fake the queryset successfully?
python django django-models pytest-django
add a comment |
up vote
0
down vote
favorite
I have the following function:
def build_dict():
app_config = {}
for keypair in models.Applify.objects.all():
app_config.update({keypair.key: keypair.value})
return app_config
What I am trying to do is have a test file where I am faking the Applify queryset. So far what I have that is not working is this:
test_mock_list = [
mock.Mock(id=1, key='something_1', value=False),
mock.Mock(id=2, key='something_2', value=False)
]
with mock.patch('models.Applify.objects.all', return_value=test_mock_list):
# perform assertion test.
What I am getting is an error saying.
Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
I am not trying to actually connect to the database, so I don't want to use the @pytest.mark.django_db
decorator. So how can I fake the queryset successfully?
python django django-models pytest-django
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following function:
def build_dict():
app_config = {}
for keypair in models.Applify.objects.all():
app_config.update({keypair.key: keypair.value})
return app_config
What I am trying to do is have a test file where I am faking the Applify queryset. So far what I have that is not working is this:
test_mock_list = [
mock.Mock(id=1, key='something_1', value=False),
mock.Mock(id=2, key='something_2', value=False)
]
with mock.patch('models.Applify.objects.all', return_value=test_mock_list):
# perform assertion test.
What I am getting is an error saying.
Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
I am not trying to actually connect to the database, so I don't want to use the @pytest.mark.django_db
decorator. So how can I fake the queryset successfully?
python django django-models pytest-django
I have the following function:
def build_dict():
app_config = {}
for keypair in models.Applify.objects.all():
app_config.update({keypair.key: keypair.value})
return app_config
What I am trying to do is have a test file where I am faking the Applify queryset. So far what I have that is not working is this:
test_mock_list = [
mock.Mock(id=1, key='something_1', value=False),
mock.Mock(id=2, key='something_2', value=False)
]
with mock.patch('models.Applify.objects.all', return_value=test_mock_list):
# perform assertion test.
What I am getting is an error saying.
Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
I am not trying to actually connect to the database, so I don't want to use the @pytest.mark.django_db
decorator. So how can I fake the queryset successfully?
python django django-models pytest-django
python django django-models pytest-django
edited Nov 19 at 23:47
asked Nov 19 at 23:30
user875139
68121533
68121533
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384142%2fhow-to-mock-a-django-model-query-set-in-a-unittest-without-accessing-the-databas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown