Exemple #1
0
 def test_bad_index(self):
     for bad in [1, None, object()]:
         with pytest.raises(TypeError) as exc:
             parse_index(1)
         assert exc.value.args[0] == (
             'Index type must be <str>, <list>, <dict> or <pymongo.IndexModel>'
         )
Exemple #2
0
 def test_parse_index(self):
     for value, expected in (
         ('my_index', IndexModel([('my_index', ASCENDING)])),
         ('+my_index', IndexModel([('my_index', ASCENDING)])),
         ('-my_index', IndexModel([('my_index', DESCENDING)])),
         ('$my_index', IndexModel([('my_index', TEXT)])),
         ('#my_index', IndexModel([('my_index', HASHED)])),
             # Compound indexes
         (('index1', '-index2'),
          IndexModel([('index1', ASCENDING), ('index2', DESCENDING)])),
             # No changes if not needed
         (IndexModel([('my_index', ASCENDING)]),
          IndexModel([('my_index', ASCENDING)])),
             # Custom index
         ({
             'name': 'my-custom-index',
             'key': ['+index1', '-index2'],
             'sparse': True,
             'unique': True,
             'expireAfterSeconds': 42
         },
          IndexModel([('index1', ASCENDING), ('index2', DESCENDING)],
                     name='my-custom-index',
                     sparse=True,
                     unique=True,
                     expireAfterSeconds=42)),
     ):
         assert_indexes(parse_index(value), expected)
Exemple #3
0
 def test_parse_index(self):
     for value, expected in (
         ('my_index', IndexModel([('my_index', ASCENDING)])),
         ('+my_index', IndexModel([('my_index', ASCENDING)])),
         ('-my_index', IndexModel([('my_index', DESCENDING)])),
         ('$my_index', IndexModel([('my_index', TEXT)])),
         ('#my_index', IndexModel([('my_index', HASHED)])),
         # Compound indexes
         (('index1', '-index2'), IndexModel([('index1', ASCENDING), ('index2', DESCENDING)])),
         # No changes if not needed
         (IndexModel([('my_index', ASCENDING)]), IndexModel([('my_index', ASCENDING)])),
         # Custom index
         (
             {
                 'name': 'my-custom-index',
                 'key': ['+index1', '-index2'],
                 'sparse': True,
                 'unique': True,
                 'expireAfterSeconds': 42
             },
             IndexModel([('index1', ASCENDING), ('index2', DESCENDING)],
                        name='my-custom-index', sparse=True,
                        unique=True, expireAfterSeconds=42)
         ),
     ):
         assert_indexes(parse_index(value), expected)
Exemple #4
0
 def test_bad_index(self):
     for bad in [1, None, object()]:
         with pytest.raises(TypeError) as exc:
             parse_index(1)
         assert exc.value.args[0] == (
             'Index type must be <str>, <list>, <dict> or <pymongo.IndexModel>')