[py3] Used six.StringIO to simulate stdout buffer in tests

This commit is contained in:
Claude Paroz 2012-08-11 18:56:14 +02:00
parent 09c589810d
commit 3eb28d0119
4 changed files with 13 additions and 15 deletions

View File

@ -1,11 +1,11 @@
import copy import copy
import sys import sys
from io import BytesIO
from django.core.management.validation import get_validation_errors from django.core.management.validation import get_validation_errors
from django.db.models.loading import cache, load_app from django.db.models.loading import cache, load_app
from django.utils import unittest from django.utils import unittest
from django.utils.six import StringIO
class InvalidModelTestCase(unittest.TestCase): class InvalidModelTestCase(unittest.TestCase):
@ -16,7 +16,7 @@ class InvalidModelTestCase(unittest.TestCase):
# coloring attached (makes matching the results easier). We restore # coloring attached (makes matching the results easier). We restore
# sys.stderr afterwards. # sys.stderr afterwards.
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
self.stdout = BytesIO() self.stdout = StringIO()
sys.stdout = self.stdout sys.stdout = self.stdout
# This test adds dummy applications to the app cache. These # This test adds dummy applications to the app cache. These

View File

@ -4,7 +4,6 @@ from __future__ import absolute_import, unicode_literals
import os import os
import re import re
from io import BytesIO
from django.core import management from django.core import management
from django.core.management.base import CommandError from django.core.management.base import CommandError
@ -14,6 +13,7 @@ from django.db.models import signals
from django.test import (TestCase, TransactionTestCase, skipIfDBFeature, from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
skipUnlessDBFeature) skipUnlessDBFeature)
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.six import StringIO
from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3, Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3,
@ -276,7 +276,7 @@ class TestFixtures(TestCase):
) )
animal.save() animal.save()
stdout = BytesIO() stdout = StringIO()
management.call_command( management.call_command(
'dumpdata', 'dumpdata',
'fixtures_regress.animal', 'fixtures_regress.animal',
@ -305,7 +305,7 @@ class TestFixtures(TestCase):
""" """
Regression for #11428 - Proxy models aren't included when you dumpdata Regression for #11428 - Proxy models aren't included when you dumpdata
""" """
stdout = BytesIO() stdout = StringIO()
# Create an instance of the concrete class # Create an instance of the concrete class
widget = Widget.objects.create(name='grommet') widget = Widget.objects.create(name='grommet')
management.call_command( management.call_command(
@ -380,7 +380,7 @@ class TestFixtures(TestCase):
) )
def test_loaddata_not_existant_fixture_file(self): def test_loaddata_not_existant_fixture_file(self):
stdout_output = BytesIO() stdout_output = StringIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'this_fixture_doesnt_exist', 'this_fixture_doesnt_exist',
@ -465,7 +465,7 @@ class NaturalKeyFixtureTests(TestCase):
commit=False commit=False
) )
stdout = BytesIO() stdout = StringIO()
management.call_command( management.call_command(
'dumpdata', 'dumpdata',
'fixtures_regress.book', 'fixtures_regress.book',

View File

@ -1,10 +1,9 @@
from __future__ import absolute_import from __future__ import absolute_import
from io import BytesIO
from django.core import management from django.core import management
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from django.utils.six import StringIO
from .models import (Person, Group, Membership, UserMembership, Car, Driver, from .models import (Person, Group, Membership, UserMembership, Car, Driver,
CarDriver) CarDriver)
@ -70,11 +69,11 @@ class M2MThroughTestCase(TestCase):
pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk} pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk}
out = BytesIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks) self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)
out = BytesIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="xml", management.call_command("dumpdata", "m2m_through_regress", format="xml",
indent=2, stdout=out) indent=2, stdout=out)
self.assertEqual(out.getvalue().strip(), """ self.assertEqual(out.getvalue().strip(), """
@ -142,6 +141,6 @@ class ThroughLoadDataTestCase(TestCase):
def test_sequence_creation(self): def test_sequence_creation(self):
"Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107" "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
out = BytesIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""") self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")

View File

@ -7,7 +7,6 @@ import posixpath
import shutil import shutil
import sys import sys
import tempfile import tempfile
from io import BytesIO
from django.template import loader, Context from django.template import loader, Context
from django.conf import settings from django.conf import settings
@ -194,7 +193,7 @@ class TestFindStatic(CollectionTestCase, TestDefaults):
Test ``findstatic`` management command. Test ``findstatic`` management command.
""" """
def _get_file(self, filepath): def _get_file(self, filepath):
out = BytesIO() out = six.StringIO()
call_command('findstatic', filepath, all=False, verbosity=0, stdout=out) call_command('findstatic', filepath, all=False, verbosity=0, stdout=out)
out.seek(0) out.seek(0)
lines = [l.strip() for l in out.readlines()] lines = [l.strip() for l in out.readlines()]
@ -206,7 +205,7 @@ class TestFindStatic(CollectionTestCase, TestDefaults):
""" """
Test that findstatic returns all candidate files if run without --first. Test that findstatic returns all candidate files if run without --first.
""" """
out = BytesIO() out = six.StringIO()
call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out) call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out)
out.seek(0) out.seek(0)
lines = [l.strip() for l in out.readlines()] lines = [l.strip() for l in out.readlines()]