Skip to content

Instantly share code, notes, and snippets.

@alecxe alecxe/test_parse_arguments.py Secret
Created Mar 14, 2017

Embed
What would you like to do?
Support testing code for the Code Review question (http://codereview.stackexchange.com/questions/157747)
import argparse
from unittest import TestCase
import mock
from libs.general import parse_arguments
DEFAULT_ARGS = ['--host=127.0.0.1', '--port=3319', '--database=ap', '--user=root']
@mock.patch('getpass.getpass', return_value='Password1')
class PasswordStubTestCase(TestCase):
def test_validate_mode_with_minimum_args(self, _):
args = parse_arguments(DEFAULT_ARGS + ['--validate'])
self.assertEqual(args, argparse.Namespace(apply=False, batch_size=5000, name=None,
id_begin=1, id_end=None,
database='ap', host='127.0.0.1', password='Password1',
port=3319, user='root', validate=True, verbose=False))
def test_validate_mode_with_name(self, _):
args = parse_arguments(DEFAULT_ARGS + ['--validate', '--name=test'])
self.assertEqual(args, argparse.Namespace(apply=False, batch_size=5000, name='test',
id_begin=None, id_end=None,
database='ap', host='127.0.0.1', password='Password1',
port=3319, user='root', validate=True, verbose=False))
def test_validate_mode_with_id(self, _):
args = parse_arguments(DEFAULT_ARGS + ['--validate', '--id-begin=1'])
self.assertEqual(args, argparse.Namespace(apply=False, batch_size=5000, name=None,
id_begin=1, id_end=None,
database='ap', host='127.0.0.1', password='Password1',
port=3319, user='root', validate=True, verbose=False))
args = parse_arguments(DEFAULT_ARGS + ['--validate', '--id-begin=1', '--id-end=10'])
self.assertEqual(args, argparse.Namespace(apply=False, batch_size=5000, name=None,
id_begin=1, id_end=10,
database='ap', host='127.0.0.1', password='Password1',
port=3319, user='root', validate=True, verbose=False))
def test_default_id_begin_set(self, _):
args = parse_arguments(DEFAULT_ARGS + ['--validate', '--id-end=10'])
self.assertEqual(args.id_begin, 1)
self.assertEqual(args.id_end, 10)
def test_default_id_begin_set_no_end(self, _):
args = parse_arguments(DEFAULT_ARGS + ['--validate'])
self.assertEqual(args.id_begin, 1)
self.assertTrue(args.id_end is None)
class ParseArgumentsTestCase(TestCase):
def test_empty_arguments(self):
self.assertRaises(SystemExit, parse_arguments, [])
def test_no_mode_specified(self):
self.assertRaises(SystemExit, parse_arguments, DEFAULT_ARGS)
def test_both_modes_specified(self):
self.assertRaises(SystemExit, parse_arguments, DEFAULT_ARGS + ['--validate', '--apply'])
def test_valid_id_range(self):
self.assertRaises(SystemExit, parse_arguments,
DEFAULT_ARGS + ['--validate', '--id-begin=10', '--id-end=1'])
def test_name_and_id(self):
self.assertRaises(SystemExit, parse_arguments,
DEFAULT_ARGS + ['--validate', '--name=test', '--id-begin=1'])
self.assertRaises(SystemExit, parse_arguments,
DEFAULT_ARGS + ['--validate', '--name=test', '--id-end=1'])
self.assertRaises(SystemExit, parse_arguments,
DEFAULT_ARGS + ['--validate', '--name=test', '--id-begin=1', '--id-end=20'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.