Django Tests use the production database when making queries on the static elements of the class, why does...
up vote
0
down vote
favorite
I have two Databases, one for use with Django (app_db) and another read-only one that is not stained with Django (remote_db).
I have my custom router defined (db_for_read, db_for_write, allow_relation) and selects the right one when used with an app (module) created to manipulate the data, inclusive in the tests. In addition, remote_db to their own migrations.
Distributions Apps and Database:
- app news_module with model class News, NewsCategoryClassifier, use app_db.
- app categories_module with model class Category, use remote_db.
My Setting:
DATABASE_ROUTERS = ['src.core.router.MyCustomRouter']
DATABASES = {
'default': env.db('APP_DB'),
'remote_db': env.db('REMOTE_DB')
}
my test:
class CategoriesClassifierTest(TestCase):
def test_instance_have(self):
Category.object.create(name='Sports')
Category.object.create(name='Financials')
cl = NewsCategoryClassifier()
self.assertIsInstance(cl, NewsCategoryClassifier)
self.assertEquals(['Sports', 'Financials'], cl.choices)
This works as expected.
class NewsCategoryClassifier:
def __init__(self):
self.choices = Categories.objects.all()
But when I run the test that involves a query on a class attribute I get data from the production database.
class NewsCategoryClassifier:
choices = Categories.objects.all()
I would like to get how I can test the data in NewsCategoryClassifier from test_remote_db. I don't know if by creating a custom TestRunner I can come up with my solution.
django testing
add a comment |
up vote
0
down vote
favorite
I have two Databases, one for use with Django (app_db) and another read-only one that is not stained with Django (remote_db).
I have my custom router defined (db_for_read, db_for_write, allow_relation) and selects the right one when used with an app (module) created to manipulate the data, inclusive in the tests. In addition, remote_db to their own migrations.
Distributions Apps and Database:
- app news_module with model class News, NewsCategoryClassifier, use app_db.
- app categories_module with model class Category, use remote_db.
My Setting:
DATABASE_ROUTERS = ['src.core.router.MyCustomRouter']
DATABASES = {
'default': env.db('APP_DB'),
'remote_db': env.db('REMOTE_DB')
}
my test:
class CategoriesClassifierTest(TestCase):
def test_instance_have(self):
Category.object.create(name='Sports')
Category.object.create(name='Financials')
cl = NewsCategoryClassifier()
self.assertIsInstance(cl, NewsCategoryClassifier)
self.assertEquals(['Sports', 'Financials'], cl.choices)
This works as expected.
class NewsCategoryClassifier:
def __init__(self):
self.choices = Categories.objects.all()
But when I run the test that involves a query on a class attribute I get data from the production database.
class NewsCategoryClassifier:
choices = Categories.objects.all()
I would like to get how I can test the data in NewsCategoryClassifier from test_remote_db. I don't know if by creating a custom TestRunner I can come up with my solution.
django testing
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have two Databases, one for use with Django (app_db) and another read-only one that is not stained with Django (remote_db).
I have my custom router defined (db_for_read, db_for_write, allow_relation) and selects the right one when used with an app (module) created to manipulate the data, inclusive in the tests. In addition, remote_db to their own migrations.
Distributions Apps and Database:
- app news_module with model class News, NewsCategoryClassifier, use app_db.
- app categories_module with model class Category, use remote_db.
My Setting:
DATABASE_ROUTERS = ['src.core.router.MyCustomRouter']
DATABASES = {
'default': env.db('APP_DB'),
'remote_db': env.db('REMOTE_DB')
}
my test:
class CategoriesClassifierTest(TestCase):
def test_instance_have(self):
Category.object.create(name='Sports')
Category.object.create(name='Financials')
cl = NewsCategoryClassifier()
self.assertIsInstance(cl, NewsCategoryClassifier)
self.assertEquals(['Sports', 'Financials'], cl.choices)
This works as expected.
class NewsCategoryClassifier:
def __init__(self):
self.choices = Categories.objects.all()
But when I run the test that involves a query on a class attribute I get data from the production database.
class NewsCategoryClassifier:
choices = Categories.objects.all()
I would like to get how I can test the data in NewsCategoryClassifier from test_remote_db. I don't know if by creating a custom TestRunner I can come up with my solution.
django testing
I have two Databases, one for use with Django (app_db) and another read-only one that is not stained with Django (remote_db).
I have my custom router defined (db_for_read, db_for_write, allow_relation) and selects the right one when used with an app (module) created to manipulate the data, inclusive in the tests. In addition, remote_db to their own migrations.
Distributions Apps and Database:
- app news_module with model class News, NewsCategoryClassifier, use app_db.
- app categories_module with model class Category, use remote_db.
My Setting:
DATABASE_ROUTERS = ['src.core.router.MyCustomRouter']
DATABASES = {
'default': env.db('APP_DB'),
'remote_db': env.db('REMOTE_DB')
}
my test:
class CategoriesClassifierTest(TestCase):
def test_instance_have(self):
Category.object.create(name='Sports')
Category.object.create(name='Financials')
cl = NewsCategoryClassifier()
self.assertIsInstance(cl, NewsCategoryClassifier)
self.assertEquals(['Sports', 'Financials'], cl.choices)
This works as expected.
class NewsCategoryClassifier:
def __init__(self):
self.choices = Categories.objects.all()
But when I run the test that involves a query on a class attribute I get data from the production database.
class NewsCategoryClassifier:
choices = Categories.objects.all()
I would like to get how I can test the data in NewsCategoryClassifier from test_remote_db. I don't know if by creating a custom TestRunner I can come up with my solution.
django testing
django testing
edited Nov 9 at 18:11
Kzrystof
1,90531221
1,90531221
asked Nov 9 at 17:31
Henry Palacios
12
12
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53230667%2fdjango-tests-use-the-production-database-when-making-queries-on-the-static-eleme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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%2f53230667%2fdjango-tests-use-the-production-database-when-making-queries-on-the-static-eleme%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