def test_list_parser(self, allow_empty_list, user_input, expected):
     parser = utils.ListParser(allow_empty_list)
     self.assertEqual(parser.parse(user_input), expected)
     self.assertEqual('ListParser', str(parser))
     self.assertEqual(
         '<ListParser(allow_empty_list={!r})>'.format(allow_empty_list),
         repr(parser))
     self.assertEqual(utils.ListParser(allow_empty_list), parser)
     self.assertNotEqual(utils.ListParser(not allow_empty_list), parser)
Ejemplo n.º 2
0
class AppConstantsTest(parameterized.TestCase, absltest.TestCase):
    def test_get_default_constants(self):
        constants = app_constants.get_default_constants()
        self.assertLen(constants, 7)

    @flagsaver.flagsaver(app_constants_test_flag='valid')
    def test_get_constants_from_flags(self):
        constants = app_constants.get_constants_from_flags(module=__name__)
        self.assertLen(constants, 1)
        self.assertEqual('valid', constants['app_constants_test_flag'].value)

    def test_get_constants_from_flags__not_parsed(self):
        FLAGS.__dict__['__flags_parsed'] = False
        constants = app_constants.get_constants_from_flags(module=__name__)
        FLAGS.__dict__['__flags_parsed'] = True
        self.assertLen(constants, 1)
        self.assertEqual('', constants['app_constants_test_flag'].value)

    @parameterized.parameters(
        ('DEFAULT', None, 'new', 'new'),
        ('', utils.StringParser(True), '', ''),
        ('yes', utils.YesNoParser(), 'no', False),
        ('', utils.ListParser(True), 'this,one', ['this', 'one']),
        ('', utils.StringParser(False), 'asdf', 'asdf'),
        ('1', flags.IntegerParser(), '10', 10),
        ('*****@*****.**', utils.EmailParser(), '*****@*****.**', '*****@*****.**'),
    )
    def test_constant_constructor(self, default, parser, new_value, expected):
        test_constant = app_constants.Constant('name', 'message', default,
                                               parser)
        self.assertEqual('name: {}'.format(default), str(test_constant))
        self.assertEqual(
            "<Constant('name', 'message', {!r}, {!r})>".format(
                default, parser), repr(test_constant))
        self.assertEqual(
            app_constants.Constant('name', 'message', default, parser),
            test_constant)
        self.assertNotEqual(
            app_constants.Constant('other', 'message', default, parser),
            test_constant)
        test_constant.value = new_value
        self.assertTrue(test_constant.valid)
        self.assertEqual(test_constant.value, expected)

    def test_constant_prompt(self):
        test_constant = app_constants.Constant('name', 'messsage', '',
                                               utils.StringParser(False))
        self.assertFalse(test_constant.valid)
        with mock.patch.object(utils, 'prompt', return_value='VALID!'):
            test_constant.prompt()
            self.assertTrue(test_constant.valid)
 def test_list_parser__invalid_input(self, arg):
     parser = utils.ListParser(False)
     with self.assertRaises(ValueError):
         parser.parse(arg)
    'from the web application. This gives access to all in app data.')

# Not required to be provided either by flag or by prompt.
flags.DEFINE_string(
    CUSTOMER_ID, 'my_customer',
    'The G Suite customer ID.\nIf you are an administrator of the organization '
    'this application is running in leave the default. If you are a reseller '
    'you can get the customer ID by making a get user request: '
    'https://developers.google.com/admin-sdk/directory/v1/guides/manage-users'
    '.html#get_user')

# Dictionary where the flag name is the key and the value is a parser, an object
# that has `parse` as a public instance method. A parser is not required,
# without one any value will be accepted.
_PARSERS = {
    APP_DOMAINS: utils.ListParser(allow_empty_list=False),
    CHROME_CLIENT_ID: utils.ClientIDParser(),
    WEB_CLIENT_ID: utils.ClientIDParser(),
    ADMIN_EMAIL: utils.EmailParser(),
    SEND_EMAIL_AS: utils.EmailParser(),
    SUPERADMINS_GROUP: utils.StringParser(allow_empty_string=False),
    CUSTOMER_ID: utils.StringParser(allow_empty_string=False),
}


def get_constants_from_flags(module=__name__):
    """Returns a dictionary of all constants from flags.

  This should only be used when skipping user validation (e.g. scripting) since
  it does not validate the provided values with the custom parsers until the
  value is requested. If the flag provided does not meet the `Parser`