MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / test_regex

Method test_regex

test/test_json_util.py:348–384  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

346 self.assertEqual(re.U, loaded.flags)
347
348 def test_regex(self):
349 for regex_instance in (re.compile("a*b", re.IGNORECASE), Regex("a*b", re.IGNORECASE)):
350 res = self.round_tripped({"r": regex_instance})["r"]
351
352 self.assertEqual("a*b", res.pattern)
353 res = self.round_tripped({"r": Regex("a*b", re.IGNORECASE)})["r"]
354 self.assertEqual("a*b", res.pattern)
355 self.assertEqual(re.IGNORECASE, res.flags)
356
357 unicode_options = re.I | re.M | re.S | re.U | re.X
358 regex = re.compile("a*b", unicode_options)
359 res = self.round_tripped({"r": regex})["r"]
360 self.assertEqual(unicode_options, res.flags)
361
362 # Some tools may not add $options if no flags are set.
363 res = json_util.loads('{"r": {"$regex": "a*b"}}')["r"]
364 self.assertEqual(0, res.flags)
365
366 self.assertEqual(
367 Regex(".*", "ilm"), json_util.loads('{"r": {"$regex": ".*", "$options": "ilm"}}')["r"]
368 )
369
370 # Check order.
371 self.assertEqual(
372 '{"$regularExpression": {"pattern": ".*", "options": "mx"}}',
373 json_util.dumps(Regex(".*", re.M | re.X)),
374 )
375
376 self.assertEqual(
377 '{"$regularExpression": {"pattern": ".*", "options": "mx"}}',
378 json_util.dumps(re.compile(b".*", re.M | re.X)),
379 )
380
381 self.assertEqual(
382 '{"$regex": ".*", "$options": "mx"}',
383 json_util.dumps(Regex(".*", re.M | re.X), json_options=LEGACY_JSON_OPTIONS),
384 )
385
386 def test_regex_validation(self):
387 non_str_types = [10, {}, []]

Callers

nothing calls this directly

Calls 2

round_trippedMethod · 0.95
RegexClass · 0.90

Tested by

no test coverage detected