diff options
author | Pavel Kulikov <kulikovpavel@gmail.com> | 2017-03-26 22:29:05 +0300 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2017-05-26 19:54:21 -0400 |
commit | af1fa5e7da21c57a4037e67f93493af4e78d454a (patch) | |
tree | 459e2342f0431a83f9915dffee09cf89fa54bc16 /tests/fixtures | |
parent | c930c241f8f1ccf3a7848b843628eacdb983d70a (diff) | |
download | django-af1fa5e7da21c57a4037e67f93493af4e78d454a.tar.gz |
Fixed #27978 -- Allowed loaddata to read data from stdin.
Thanks Squareweave for the django-loaddata-stdin project from which this
is adapted.
Diffstat (limited to 'tests/fixtures')
-rw-r--r-- | tests/fixtures/tests.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index fe047e9838..fb9ba91d3d 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -680,6 +680,35 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): with self.assertRaisesMessage(management.CommandError, msg): management.call_command('loaddata', 'fixture1', exclude=['fixtures.FooModel'], verbosity=0) + def test_stdin_without_format(self): + """Reading from stdin raises an error if format isn't specified.""" + msg = '--format must be specified when reading from stdin.' + with self.assertRaisesMessage(management.CommandError, msg): + management.call_command('loaddata', '-', verbosity=0) + + def test_loading_stdin(self): + """Loading fixtures from stdin with json and xml.""" + tests_dir = os.path.dirname(__file__) + fixture_json = os.path.join(tests_dir, 'fixtures', 'fixture1.json') + fixture_xml = os.path.join(tests_dir, 'fixtures', 'fixture3.xml') + + with mock.patch('django.core.management.commands.loaddata.sys.stdin', open(fixture_json, 'r')): + management.call_command('loaddata', '--format=json', '-', verbosity=0) + self.assertEqual(Article.objects.count(), 2) + self.assertQuerysetEqual(Article.objects.all(), [ + '<Article: Time to reform copyright>', + '<Article: Poker has no place on ESPN>', + ]) + + with mock.patch('django.core.management.commands.loaddata.sys.stdin', open(fixture_xml, 'r')): + management.call_command('loaddata', '--format=xml', '-', verbosity=0) + self.assertEqual(Article.objects.count(), 3) + self.assertQuerysetEqual(Article.objects.all(), [ + '<Article: XML identified as leading cause of cancer>', + '<Article: Time to reform copyright>', + '<Article: Poker on TV is great!>', + ]) + class NonexistentFixtureTests(TestCase): """ |