How to use _call_command method in testcontainers-python

Best Python code snippet using testcontainers-python_python

tests.py

Source:tests.py Github

copy

Full Screen

...7from ietf.person.models import PersonApiKeyEvent, PersonEvent8from ietf.utils.test_utils import TestCase9class CommandTests(TestCase):10 @staticmethod11 def _call_command(command_name, *args, **options):12 out = StringIO()13 options['stdout'] = out14 call_command(command_name, *args, **options)15 return out.getvalue()16 def _assert_purge_results(self, cmd_output, expected_delete_count, expected_kept_events):17 self.assertNotIn('Dry run requested', cmd_output)18 if expected_delete_count == 0:19 delete_text = 'No events older than'20 else:21 delete_text = 'Deleting {} event'.format(expected_delete_count)22 self.assertIn(delete_text, cmd_output)23 self.assertCountEqual(24 PersonApiKeyEvent.objects.all(),25 expected_kept_events,26 'Wrong events were deleted'27 )28 def _assert_purge_dry_run_results(self, cmd_output, expected_delete_count, expected_kept_events):29 self.assertIn('Dry run requested', cmd_output)30 if expected_delete_count == 0:31 delete_text = 'No events older than'32 else:33 delete_text = 'Would delete {} event'.format(expected_delete_count)34 self.assertIn(delete_text, cmd_output)35 self.assertCountEqual(36 PersonApiKeyEvent.objects.all(),37 expected_kept_events,38 'Events were deleted when dry-run option was used'39 )40 def test_purge_old_personal_api_key_events(self):41 keep_days = 1042 # Remember how many PersonEvents were present so we can verify they're cleaned up properly.43 personevents_before = PersonEvent.objects.count()44 now = datetime.datetime.now()45 # The first of these events will be timestamped a fraction of a second more than keep_days46 # days ago by the time we call the management command, so will just barely chosen for purge.47 old_events = [48 PersonApiKeyEventFactory(time=now - datetime.timedelta(days=n))49 for n in range(keep_days, 2 * keep_days + 1)50 ]51 num_old_events = len(old_events)52 recent_events = [53 PersonApiKeyEventFactory(time=now - datetime.timedelta(days=n))54 for n in range(0, keep_days)55 ]56 # We did not create recent_event timestamped exactly keep_days ago because it would57 # be treated as an old_event by the management command. Create an event a few seconds58 # on the "recent" side of keep_days old to test the threshold.59 recent_events.append(60 PersonApiKeyEventFactory(61 time=now + datetime.timedelta(seconds=3) - datetime.timedelta(days=keep_days)62 )63 )64 num_recent_events = len(recent_events)65 # call with dry run66 output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run')67 self._assert_purge_dry_run_results(output, num_old_events, old_events + recent_events)68 # call for real69 output = self._call_command('purge_old_personal_api_key_events', str(keep_days))70 self._assert_purge_results(output, num_old_events, recent_events)71 self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,72 'PersonEvents were not cleaned up properly')73 # repeat - there should be nothing left to delete74 output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days))75 self._assert_purge_dry_run_results(output, 0, recent_events)76 output = self._call_command('purge_old_personal_api_key_events', str(keep_days))77 self._assert_purge_results(output, 0, recent_events)78 self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,79 'PersonEvents were not cleaned up properly')80 # and now delete the remaining events81 output = self._call_command('purge_old_personal_api_key_events', '0')82 self._assert_purge_results(output, num_recent_events, [])83 self.assertEqual(PersonEvent.objects.count(), personevents_before,84 'PersonEvents were not cleaned up properly')85 def test_purge_old_personal_api_key_events_rejects_invalid_arguments(self):86 """The purge_old_personal_api_key_events command should reject invalid arguments"""87 event = PersonApiKeyEventFactory(time=datetime.datetime.now() - datetime.timedelta(days=30))88 with self.assertRaises(CommandError):89 self._call_command('purge_old_personal_api_key_events')90 with self.assertRaises(CommandError):91 self._call_command('purge_old_personal_api_key_events', '-15')92 with self.assertRaises(CommandError):93 self._call_command('purge_old_personal_api_key_events', '15.3')94 with self.assertRaises(CommandError):95 self._call_command('purge_old_personal_api_key_events', '15', '15')96 with self.assertRaises(CommandError):97 self._call_command('purge_old_personal_api_key_events', 'abc', '15')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run testcontainers-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful