Python django.utils.six.StringIO() Examples
The following are 30
code examples of django.utils.six.StringIO().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.six
, or try the search function
.
Example #1
Source File: base.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def validate(self, app=None, display_num_errors=False): """ Validates the given app, raising CommandError for any errors. If app is None, then this will validate all installed apps. """ from django.core.management.validation import get_validation_errors s = StringIO() num_errors = get_validation_errors(s, app) if num_errors: s.seek(0) error_text = s.read() raise CommandError("One or more models did not validate:\n%s" % error_text) if display_num_errors: self.stdout.write("%s error%s found" % (num_errors, num_errors != 1 and 's' or ''))
Example #2
Source File: tests.py From django-sqlserver with MIT License | 6 votes |
def test_dumpdata(self): "dumpdata honors allow_migrate restrictions on the router" User.objects.create_user('alice', 'alice@example.com') User.objects.db_manager('default').create_user('bob', 'bob@example.com') # dumping the default database doesn't try to include auth because # allow_migrate prohibits auth on default new_io = StringIO() management.call_command('dumpdata', 'auth', format='json', database='default', stdout=new_io) command_output = new_io.getvalue().strip() self.assertEqual(command_output, '[]') # dumping the other database does include auth new_io = StringIO() management.call_command('dumpdata', 'auth', format='json', database='other', stdout=new_io) command_output = new_io.getvalue().strip() self.assertIn('"email": "alice@example.com"', command_output)
Example #3
Source File: test_commands.py From civet with Apache License 2.0 | 6 votes |
def test_dump_latest(self): out = StringIO() management.call_command("dump_latest", stdout=out) self.assertIn("Dumping 0 events", out.getvalue()) ev = utils.create_event() management.call_command("dump_latest", stdout=out) self.assertIn("Dumping 1 events", out.getvalue()) with open("out.json", "r") as f: data = f.read() out = json.loads(data) count = 0 for entry in out: if entry["model"] == "ci.event": self.assertEqual(ev.pk, entry["pk"]) count = 1 self.assertEqual(count, 1)
Example #4
Source File: views.py From waliki with BSD 3-Clause "New" or "Revised" License | 6 votes |
def webhook_pull(request, remote='origin'): if request.method == 'POST': try: log = Git().pull(remote) s = StringIO() call_command('sync_waliki', stdout=s) s.seek(0) r = {'pull': log, 'sync': s.read()} status_code = 200 except Exception as e: r = {'error': text_type(e)} status_code = 500 return HttpResponse(json.dumps(r), status=status_code, content_type="application/json") return HttpResponse("POST to %s" % reverse("waliki_webhook_pull", args=(remote,)))
Example #5
Source File: test_commands.py From ecommerce with GNU Affero General Public License v3.0 | 6 votes |
def test_with_commit(self): """ Verify the command adds a site to baskets without one. """ queryset = Basket.objects.filter(site=self.site) # There should be no baskets associated with the site self.assertEqual(queryset.count(), 0) # Call the command out = StringIO() call_command(self.command, site_id=self.site.id, commit=True, stderr=out) # The baskets should be associated with the site self.assertEqual(queryset.count(), 3) # There should be no unassociated baskets self.assertEqual(Basket.objects.filter(site__isnull=True).count(), 0) # Verify info was output to stderr actual = out.getvalue().strip() self.assertTrue(actual.startswith('Associating [{}] baskets with site [{}]..'.format( len(self.unassociated_baskets), self.site))) self.assertTrue(actual.endswith('Done.'))
Example #6
Source File: test_commands.py From ecommerce with GNU Affero General Public License v3.0 | 6 votes |
def test_without_commit(self): """ Verify the command does not modify any baskets, if the commit flag is not specified. """ queryset = Basket.objects.filter(site__isnull=True) expected = queryset.count() # Call the command with dry-run flag out = StringIO() call_command(self.command, site_id=self.site.id, commit=False, stderr=out) # Verify no baskets affected self.assertEqual(queryset.count(), expected) # Verify the number of baskets expected to be deleted was printed to stderr expected = 'This has been an example operation. If the --commit flag had been included, the command ' \ 'would have associated [{}] baskets with site [{}].'.format(len(self.unassociated_baskets), self.site) self.assertEqual(out.getvalue().strip(), expected)
Example #7
Source File: test_commands.py From civet with Apache License 2.0 | 6 votes |
def test_user_access(self, mock_get): out = StringIO() mock_get.return_value = utils.Response(status_code=404) with self.assertRaises(CommandError): management.call_command("user_access", stdout=out) with self.assertRaises(models.GitUser.DoesNotExist): management.call_command("user_access", "--master", "nobody", stdout=out) with self.assertRaises(CommandError): management.call_command("user_access", "--master", self.owner.name, stdout=out) out = StringIO() management.call_command("user_access", "--master", self.build_user.name, stdout=out) repo1 = {'name': 'repo1', 'owner': {'login': 'owner'} } repo2 = {'name': 'repo2', 'owner': {'login': 'owner'} } mock_get.side_effect = [utils.Response([repo1]), utils.Response([repo2])] out = StringIO() management.call_command("user_access", "--master", self.build_user.name, "--user", "owner", stdout=out)
Example #8
Source File: test_commands.py From ecommerce with GNU Affero General Public License v3.0 | 6 votes |
def test_with_commit(self): """ Verify the command, when called with the commit flag, deletes baskets with orders. """ # Verify we have baskets with orders self.assertEqual(Basket.objects.filter(order__isnull=False).count(), len(self.orders + self.invoiced_orders)) # Verify we have invoiced baskets self.assertEqual(Basket.objects.filter(invoice__isnull=False).count(), len(self.invoiced_baskets)) # Call the command with the commit flag out = StringIO() call_command(self.command, commit=True, stderr=out) # Verify baskets with orders deleted, except for those which are invoiced self.assertEqual(list(Basket.objects.all()), self.unordered_baskets + self.invoiced_baskets) # Verify info was output to stderr actual = out.getvalue().strip() self.assertTrue(actual.startswith('Deleting [{}] baskets.'.format(len(self.orders)))) self.assertTrue(actual.endswith('All baskets deleted.'))
Example #9
Source File: test_delete_archived.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_ref_without_instance(self, mock_delete, mock_ref_delete): """ Verify deletion proceeds when an InstanceReference does not point to an OpenEdxInstance. """ # Create instanceless InstanceReference ref = InstanceReference.objects.create( name='Instanceless', instance_id=999, instance_type_id=13, is_archived=True) ref.modified -= timedelta(days=365) ref.save(update_modified=False) # Run command out = StringIO() call_command('delete_archived', '10', stdout=out) # Check only the InstanceReference queryset gets deleted self.assertTrue('Found 1 archived instances older than 10 months' in out.getvalue()) self.assertTrue('Instanceless: No instance associated' in out.getvalue()) self.assertTrue('Deleted 1 archived instances older than 10 months' in out.getvalue()) self.assertEqual(mock_delete.call_count, 0) self.assertEqual(mock_ref_delete.call_count, 1)
Example #10
Source File: test_migrate_ephemeral.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_migrate(self, mock_spawn_appserver): """ Verify that the command correctly migrate an instance to use external databases. """ try: OpenEdXInstance.objects.create( sub_domain='test_migrate', use_ephemeral_databases=True, name='test_migrate instance') with LogCapture() as captured_logs: call_command( 'migrate_ephemeral', stdout=StringIO(), ) # Verify the logs actual = set(l[2] for l in captured_logs.actual()) expected = { 'Found "1" instances using ephemeral databases', 'Migrated and started provisioning a new app server for test_migrate instance', } self.assertTrue(expected <= actual) self.assertTrue(mock_spawn_appserver.called) except TypeError: # Field already removed from database pass
Example #11
Source File: test_update_metadata.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_consul_skip_update(self): """ Tests the management command with skipping metadata update option. :param purge_consul_metadata: purge_consul_metadata method mock """ out = StringIO() # Archive the instances instances = [OpenEdXInstanceFactory.create() for _ in range(10)] for instance in instances: instance.ref.is_archived = True instance.ref.save() # Add some garbage data to consul bad_prefix = settings.CONSUL_PREFIX.format(ocim=settings.OCIM_ID, instance=333) self.client.kv.put(bad_prefix, json.dumps({'key1': 'value1', 'key2': 'value2'}).encode('utf-8')) call_command('update_metadata', skip_update=True, stdout=out) objects_count = OpenEdXInstance.objects.count() self.assertIn('Cleaning metadata for {} archived instances...'.format(objects_count + 1), out.getvalue()) self.assertIn('Successfully cleaned archived instances\' metadata', out.getvalue()) self.assertNotIn('Updating {} instances\' metadata'.format(objects_count + 1), out.getvalue())
Example #12
Source File: test_delete_archived.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_deletion_fails(self): """ Verify '-y' skips confirmation and errors are logged """ out = StringIO() err = StringIO() call_command('delete_archived', '-y', '3', stdout=out, stderr=err) self.assertTrue('Found 1 archived instances older than 3 months' in out.getvalue()) self.assertTrue('Deleting old.example.com' in out.getvalue()) self.assertTrue('Deleting new.example.com' not in out.getvalue()) self.assertTrue('Deleting newer.example.com' not in out.getvalue()) self.assertTrue('Failed to delete Instance' in out.getvalue()) self.assertTrue('Failed to delete Instance' in err.getvalue()) self.assertTrue('Traceback' in out.getvalue()) self.assertTrue('Deleted 0 archived instances older than 3 months' in out.getvalue())
Example #13
Source File: test_archive_instances.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_archiving_instances_by_file(self, filename, expected_archived_count, mock_deprovision_rabbitmq, *mock): """ Test archiving instances from domains listed in a file. """ instances = self.create_test_instances('ABCDEFGH') out = StringIO() fp = get_fixture_filepath(os.path.join('management', filename)) with open(fp, 'r') as f: domains = [line.strip() for line in f.readlines()] call_command('archive_instances', '--file=%s' % fp, stdout=out) self.assertRegex( out.getvalue(), r'.*Archived {archived_count} instances \(from {domains_count} domains\).'.format( archived_count=expected_archived_count, domains_count=len(domains) ) ) for domain in domains: instance = instances[domain]['instance'] instance.refresh_from_db() self.assertTrue(instance.ref.is_archived) self.assertEqual(mock_deprovision_rabbitmq.call_count, expected_archived_count)
Example #14
Source File: test_archive_instances.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def test_archive_exception_handling(self, mock_disable_monitoring, *mock): """ Verify that if an instance fails to be archived, the other instances are still being archived. """ instances = self.create_test_instances('ABCD') domains = instances.keys() # mock instance.disable_monitoring() method to raise exception for the first instance mock_disable_monitoring.side_effect = [Exception('disable_monitoring'), None, None, None] out = StringIO() call_command( 'archive_instances', '--domains=%s' % ','.join(domains), stdout=out ) self.assertRegex(out.getvalue(), r'.*Archived 3 instances \(from 4 domains\).') self.assertRegex(out.getvalue(), r'.*Failed to archive A.example.com.') self.assertEqual( OpenEdXInstance.objects.filter(internal_lms_domain__in=domains, ref_set__is_archived=True).count(), 3 )
Example #15
Source File: helpers.py From janeway with GNU Affero General Public License v3.0 | 6 votes |
def create_journals(): """ Creates a set of dummy journals for testing :return: a 2-tuple of two journals """ update_xsl_files() journal_one = journal_models.Journal(code="TST", domain="testserver") journal_one.save() journal_two = journal_models.Journal(code="TSA", domain="journal2.localhost") journal_two.save() out = StringIO() sys.stdout = out call_command('load_default_settings', stdout=out) journal_one.name = 'Journal One' journal_two.name = 'Journal Two' return journal_one, journal_two
Example #16
Source File: tests.py From django-sqlserver with MIT License | 6 votes |
def test_args(self): pre_migrate_receiver = Receiver(signals.pre_migrate) post_migrate_receiver = Receiver(signals.post_migrate) management.call_command( 'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY, interactive=MIGRATE_INTERACTIVE, stdout=six.StringIO(), ) for receiver in [pre_migrate_receiver, post_migrate_receiver]: args = receiver.call_args self.assertEqual(receiver.call_counter, 1) self.assertEqual(set(args), set(SIGNAL_ARGS)) self.assertEqual(args['app_config'], APP_CONFIG) self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY) self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE) self.assertEqual(args['using'], 'default') self.assertEqual(args['plan'], []) self.assertIsInstance(args['apps'], migrations.state.StateApps)
Example #17
Source File: test_create_demo_data.py From ecommerce with GNU Affero General Public License v3.0 | 5 votes |
def test_handle_with_error_in_publish_to_lms(self): """ The command should log error message if there was an error in publish to LMS. """ err_out = StringIO() sys.stderr = err_out with mock.patch.object(Course, 'publish_to_lms', return_value="Failed to publish"): call_command('create_demo_data', '--partner={}'.format(self.partner.short_code)) output = err_out.getvalue().strip() self.assertIn("An error occurred while attempting to publish", output)
Example #18
Source File: utils.py From openhgsenti with Apache License 2.0 | 5 votes |
def captured_output(stream_name): """Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO. Note: This function and the following ``captured_std*`` are copied from CPython's ``test.support`` module.""" orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, six.StringIO()) try: yield getattr(sys, stream_name) finally: setattr(sys, stream_name, orig_stdout)
Example #19
Source File: creation.py From openhgsenti with Apache License 2.0 | 5 votes |
def deserialize_db_from_string(self, data): """ Reloads the database with data from a string generated by the serialize_db_to_string method. """ data = StringIO(data) for obj in serializers.deserialize("json", data, using=self.connection.alias): obj.save()
Example #20
Source File: test_helpers.py From django-cloudinary-storage with MIT License | 5 votes |
def execute_command(*args): out = StringIO() call_command(*args, stdout=out) return out.getvalue()
Example #21
Source File: creation.py From openhgsenti with Apache License 2.0 | 5 votes |
def serialize_db_to_string(self): """ Serializes all data in the database into a JSON string. Designed only for test runner usage; will not handle large amounts of data. """ # Build list of all apps to serialize from django.db.migrations.loader import MigrationLoader loader = MigrationLoader(self.connection) app_list = [] for app_config in apps.get_app_configs(): if ( app_config.models_module is not None and app_config.label in loader.migrated_apps and app_config.name not in settings.TEST_NON_SERIALIZED_APPS ): app_list.append((app_config, None)) # Make a function to iteratively return every object def get_objects(): for model in serializers.sort_dependencies(app_list): if (model._meta.can_migrate(self.connection) and router.allow_migrate_model(self.connection.alias, model)): queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name) for obj in queryset.iterator(): yield obj # Serialize to a string out = StringIO() serializers.serialize("json", get_objects(), indent=None, stream=out) return out.getvalue()
Example #22
Source File: feedgenerator.py From openhgsenti with Apache License 2.0 | 5 votes |
def writeString(self, encoding): """ Returns the feed in the given encoding as a string. """ s = StringIO() self.write(s, encoding) return s.getvalue()
Example #23
Source File: base.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def serialize(self, queryset, **options): """ Serialize a queryset. """ self.options = options self.stream = options.pop("stream", six.StringIO()) self.selected_fields = options.pop("fields", None) self.use_natural_keys = options.pop("use_natural_keys", False) self.start_serialization() self.first = True for obj in queryset: self.start_object(obj) # Use the concrete parent class' _meta instead of the object's _meta # This is to avoid local_fields problems for proxy models. Refs #17717. concrete_model = obj._meta.concrete_model for field in concrete_model._meta.local_fields: if field.serialize: if field.rel is None: if self.selected_fields is None or field.attname in self.selected_fields: self.handle_field(obj, field) else: if self.selected_fields is None or field.attname[:-3] in self.selected_fields: self.handle_fk_field(obj, field) for field in concrete_model._meta.many_to_many: if field.serialize: if self.selected_fields is None or field.attname in self.selected_fields: self.handle_m2m_field(obj, field) self.end_object(obj) if self.first: self.first = False self.end_serialization() return self.getvalue()
Example #24
Source File: tests.py From django-sqlserver with MIT License | 5 votes |
def test_pseudo_empty_fixtures(self): """ A fixture can contain entries, but lead to nothing in the database; this shouldn't raise an error (#14068). """ new_io = StringIO() management.call_command('loaddata', 'pets', stdout=new_io, stderr=new_io) command_output = new_io.getvalue().strip() # No objects will actually be loaded self.assertEqual(command_output, "Installed 0 object(s) (of 2) from 1 fixture(s)")
Example #25
Source File: tests.py From longclaw with MIT License | 5 votes |
def test_remove_baskets(self): """Removing stale baskets. Expect that nothiing is removed and the command exits cleanly """ out = StringIO() call_command('remove_stale_baskets', '1', stdout=out) self.assertIn('Deleted 0 basket items', out.getvalue())
Example #26
Source File: message.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def as_string(self, unixfrom=False): """Return the entire formatted message as a string. Optional `unixfrom' when True, means include the Unix From_ envelope header. This overrides the default as_string() implementation to not mangle lines that begin with 'From '. See bug #13433 for details. """ fp = six.StringIO() g = Generator(fp, mangle_from_ = False) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #27
Source File: message.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def as_string(self, unixfrom=False): """Return the entire formatted message as a string. Optional `unixfrom' when True, means include the Unix From_ envelope header. This overrides the default as_string() implementation to not mangle lines that begin with 'From '. See bug #13433 for details. """ fp = six.StringIO() g = Generator(fp, mangle_from_ = False) if sys.version_info < (2, 6, 6) and isinstance(self._payload, six.text_type): # Workaround for http://bugs.python.org/issue1368247 self._payload = self._payload.encode(self._charset.output_charset) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #28
Source File: feedgenerator.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def writeString(self, encoding): """ Returns the feed in the given encoding as a string. """ s = StringIO() self.write(s, encoding) return s.getvalue()
Example #29
Source File: test_commands.py From algoliasearch-django with MIT License | 5 votes |
def setUp(self): # Create some records User.objects.create(name='James Bond', username="jb") User.objects.create(name='Captain America', username="captain") User.objects.create(name='John Snow', username="john_snow", _lat=120.2, _lng=42.1) User.objects.create(name='Steve Jobs', username="genius", followers_count=331213) self.out = StringIO()
Example #30
Source File: output.py From yournextrepresentative with GNU Affero General Public License v3.0 | 5 votes |
def capture_output(): # Suggested here: http://stackoverflow.com/a/17981937/223092 new_out, new_err = six.StringIO(), six.StringIO() old_out, old_err = sys.stdout, sys.stderr try: sys.stdout, sys.stderr = new_out, new_err yield new_out, new_err finally: sys.stdout, sys.stderr = old_out, old_err