Ejemplo n.º 1
0
def _normalize_options(options):
    """Renames keys in the options dictionary to their internally-used
    names."""
    normalized_options = {}
    for key, value in iteritems(options):
        optname = str(key).lower()
        intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, key)
        normalized_options[intname] = options[key]
    return normalized_options
Ejemplo n.º 2
0
def _normalize_options(options):
    """Renames keys in the options dictionary to their internally-used
    names."""
    normalized_options = {}
    for key, value in iteritems(options):
        optname = str(key).lower()
        intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, key)
        normalized_options[intname] = options[key]
    return normalized_options
Ejemplo n.º 3
0
def _normalize_options(options):
    """Normalizes option names in the options dictionary by converting them to
    their internally-used names. Also handles use of the tlsInsecure option.

    :Parameters:
        - `options`: Instance of _CaseInsensitiveDictionary containing
          MongoDB URI options.
    """
    tlsinsecure = options.get('tlsinsecure')
    if tlsinsecure is not None:
        for opt in _IMPLICIT_TLSINSECURE_OPTS:
            intname = INTERNAL_URI_OPTION_NAME_MAP.get(opt, None)
            # Internal options are logical inverse of public options.
            options[intname] = not tlsinsecure

    for optname in list(options):
        intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, None)
        if intname is not None:
            options[intname] = options.pop(optname)

    return options
Ejemplo n.º 4
0
def _normalize_options(options):
    """Normalizes option names in the options dictionary by converting them to
    their internally-used names.

    :Parameters:
        - `options`: Instance of _CaseInsensitiveDictionary containing
          MongoDB URI options.
    """
    # Expand the tlsInsecure option.
    tlsinsecure = options.get("tlsinsecure")
    if tlsinsecure is not None:
        for opt in _IMPLICIT_TLSINSECURE_OPTS:
            # Implicit options are logically the same as tlsInsecure.
            options[opt] = tlsinsecure

    for optname in list(options):
        intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, None)
        if intname is not None:
            options[intname] = options.pop(optname)

    return options
    def run_scenario(self):
        compressors = (test.get('options') or {}).get('compressors', [])
        if 'snappy' in compressors and not _HAVE_SNAPPY:
            self.skipTest('This test needs the snappy module.')
        if test['uri'].startswith(SRV_SCHEME) and not _HAVE_DNSPYTHON:
            self.skipTest("This test needs dnspython package.")
        valid = True
        warning = False

        with warnings.catch_warnings(record=True) as ctx:
            warnings.simplefilter('always')
            try:
                options = parse_uri(test['uri'], warn=True)
            except Exception:
                valid = False
            else:
                warning = len(ctx) > 0

        expected_valid = test.get('valid', True)
        self.assertEqual(
            valid, expected_valid,
            get_error_message_template(not expected_valid, "error") %
            test['description'])

        if expected_valid:
            expected_warning = test.get('warning', False)
            self.assertEqual(
                warning, expected_warning,
                get_error_message_template(expected_warning, "warning") %
                test['description'])

        # Compare hosts and port.
        if test['hosts'] is not None:
            self.assertEqual(len(test['hosts']), len(options['nodelist']),
                             "Incorrect number of hosts parsed from URI")

            for exp, actual in zip(test['hosts'], options['nodelist']):
                self.assertEqual(
                    exp['host'], actual[0],
                    "Expected host %s but got %s" % (exp['host'], actual[0]))
                if exp['port'] is not None:
                    self.assertEqual(
                        exp['port'], actual[1],
                        "Expected port %s but got %s" % (exp['port'], actual))

        # Compare auth options.
        auth = test['auth']
        if auth is not None:
            auth['database'] = auth.pop('db')  # db == database
            # Special case for PyMongo's collection parsing.
            if options.get('collection') is not None:
                options['database'] += "." + options['collection']
            for elm in auth:
                if auth[elm] is not None:
                    # We have to do this because while the spec requires
                    # "+"->"+", unquote_plus does "+"->" "
                    options[elm] = options[elm].replace(" ", "+")
                    self.assertEqual(
                        auth[elm], options[elm],
                        "Expected %s but got %s" % (auth[elm], options[elm]))

        # Compare URI options.
        err_msg = "For option %s expected %s but got %s"
        if test['options']:
            opts = options['options']
            for opt in test['options']:
                lopt = opt.lower()
                optname = INTERNAL_URI_OPTION_NAME_MAP.get(lopt, lopt)
                if opts.get(optname) is not None:
                    if opts[optname] == test['options'][opt]:
                        expected_value = test['options'][opt]
                    else:
                        expected_value = validate(lopt,
                                                  test['options'][opt])[1]
                    self.assertEqual(
                        opts[optname], expected_value, err_msg % (
                            opt,
                            expected_value,
                            opts[optname],
                        ))
                else:
                    self.fail("Missing expected option %s" % (opt, ))
    def run_scenario(self):
        compressors = (test.get('options') or {}).get('compressors', [])
        if 'snappy' in compressors and not _HAVE_SNAPPY:
            self.skipTest('This test needs the snappy module.')

        valid = True
        warning = False

        with warnings.catch_warnings(record=True) as ctx:
            warnings.simplefilter('always')
            try:
                options = parse_uri(test['uri'], warn=True)
            except Exception:
                valid = False
            else:
                warning = len(ctx) > 0

        expected_valid = test.get('valid', True)
        self.assertEqual(
            valid, expected_valid, get_error_message_template(
                not expected_valid, "error") % test['description'])

        if expected_valid:
            expected_warning = test.get('warning', False)
            self.assertEqual(
                warning, expected_warning, get_error_message_template(
                    expected_warning, "warning") % test['description'])

        # Compare hosts and port.
        if test['hosts'] is not None:
            self.assertEqual(
                len(test['hosts']), len(options['nodelist']),
                "Incorrect number of hosts parsed from URI")

            for exp, actual in zip(test['hosts'],
                                   options['nodelist']):
                self.assertEqual(exp['host'], actual[0],
                                 "Expected host %s but got %s"
                                 % (exp['host'], actual[0]))
                if exp['port'] is not None:
                    self.assertEqual(exp['port'], actual[1],
                                     "Expected port %s but got %s"
                                     % (exp['port'], actual))

        # Compare auth options.
        auth = test['auth']
        if auth is not None:
            auth['database'] = auth.pop('db')  # db == database
            # Special case for PyMongo's collection parsing.
            if options.get('collection') is not None:
                options['database'] += "." + options['collection']
            for elm in auth:
                if auth[elm] is not None:
                    self.assertEqual(auth[elm], options[elm],
                                     "Expected %s but got %s"
                                     % (auth[elm], options[elm]))

        # Compare URI options.
        err_msg = "For option %s expected %s but got %s"
        if test['options'] is not None:
            opts = options['options']
            for opt in test['options']:
                lopt = opt.lower()
                optname = INTERNAL_URI_OPTION_NAME_MAP.get(lopt, lopt)
                if opts.get(optname) is not None:
                    if opts[optname] == test['options'][opt]:
                        expected_value = test['options'][opt]
                    else:
                        expected_value = validate(
                            lopt, test['options'][opt])[1]
                    self.assertEqual(
                        opts[optname], expected_value,
                        err_msg % (opt, expected_value, opts[optname],))
                else:
                    self.fail(
                        "Missing expected option %s" % (opt,))