def test__with_options_pymongo(self): self.database.with_options(codec_options=codec_options.CodecOptions()) self.database.with_options() with self.assertRaises(NotImplementedError): self.database.with_options( codec_options=codec_options.CodecOptions(tz_aware=True))
def with_options(self, codec_options=None, read_preference=None, write_concern=None, read_concern=None): if codec_options: if not bson_codec_options: raise NotImplementedError( 'The codec options are not implemented in mongomock alone, you need to import ' 'the pymongo library as well.') if codec_options != bson_codec_options.CodecOptions(): raise NotImplementedError( 'The codec options are not implemented yet') if write_concern: raise NotImplementedError( 'write_concern is a valid parameter for with_options but is not implemented yet in ' 'mongomock') if read_concern: raise NotImplementedError( 'read_concern is a valid parameter for with_options but is not implemented yet in' 'mongomock') return Database(self._client, self.name, self._store, read_preference=self.read_preference)
def test__codec_options(self): client = mongomock.MongoClient() self.assertEqual(codec_options.CodecOptions(), client.codec_options) self.assertFalse(client.codec_options.tz_aware) client = mongomock.MongoClient(tz_aware=True) self.assertTrue(client.codec_options.tz_aware) self.assertTrue(client.db.collection.codec_options.tz_aware)
def connect_to_mongo(config): client = MongoClient(**config["@"]) db = getattr(client, config["db"]) people = getattr(db, config["collections"]["people"]) people = people.with_options( codec_options=codec_options.CodecOptions( unicode_decode_error_handler="ignore")) return people
def __init__(self, fh=sys.stdin, unicode_errors='strict', fast_string_prematch="", decode=True): self.fh = fh self.unicode_errors = unicode_errors self.fast_string_prematch = fast_string_prematch self.eof = False self.decode = decode self.codec = codec_options.CodecOptions(tz_aware=True)
def test__with_options_pymongo(self): other = self.database.with_options( read_preference=self.database.NEAREST) self.assertNotEqual(other, self.database) self.database.coll.insert_one({'_id': 42}) self.assertEqual({'_id': 42}, other.coll.find_one()) self.database.with_options(codec_options=codec_options.CodecOptions()) self.database.with_options() with self.assertRaises(NotImplementedError): self.database.with_options( codec_options=codec_options.CodecOptions(tz_aware=True)) tz_aware_db = mongomock.MongoClient(tz_aware=True).somedb self.assertIs( tz_aware_db, tz_aware_db.with_options(codec_options=codec_options.CodecOptions( tz_aware=True)))
def test__with_options_type_registry(self): class _CustomTypeCodec(codec_options.TypeCodec): @property def python_type(self): return _CustomTypeCodec def transform_python(self, unused_value): pass @property def bson_type(self): return int def transform_bson(self, unused_value): pass custom_type_registry = codec_options.CodecOptions( type_registry=codec_options.TypeRegistry([_CustomTypeCodec()])) with self.assertRaises(NotImplementedError): self.database.with_options(custom_type_registry)
def __init__(self, host=None, port=None, document_class=dict, tz_aware=False, connect=True, _store=None, read_preference=None, **kwargs): if host: self.host = host[0] if isinstance(host, (list, tuple)) else host else: self.host = self.HOST self.port = port or self.PORT self._tz_aware = tz_aware if bson_codec_options: self._codec_options = bson_codec_options.CodecOptions( tz_aware=tz_aware) else: self._codec_options = None self._database_accesses = {} self._store = _store or ServerStore() self._id = next(self._CONNECTION_ID) self._document_class = document_class if read_preference is not None: read_preferences.ensure_read_preference_type( 'read_preference', read_preference) self._read_preference = read_preference or _READ_PREFERENCE_PRIMARY dbase = None if '://' in self.host: res = parse_uri(self.host, default_port=self.port, warn=True) self.host, self.port = res['nodelist'][0] dbase = res['database'] else: self.host, self.port = split_hosts(self.host, default_port=self.port)[0] self.__default_datebase_name = dbase
def test__with_options_pymongo(self): other = self.database.with_options( read_preference=self.database.NEAREST) self.assertFalse(other is self.database) self.database.coll.insert_one({'_id': 42}) self.assertEqual({'_id': 42}, other.coll.find_one()) self.database.with_options(codec_options=codec_options.CodecOptions()) self.database.with_options() self.database.with_options(codec_options=codec_options.CodecOptions( tz_aware=True)) tz_aware_db = mongomock.MongoClient(tz_aware=True).somedb self.assertIs( tz_aware_db, tz_aware_db.with_options(codec_options=codec_options.CodecOptions( tz_aware=True))) custom_document_class = codec_options.CodecOptions( document_class=collections.OrderedDict) with self.assertRaises(NotImplementedError): self.database.with_options(custom_document_class) custom_uuid_representation = codec_options.CodecOptions( uuid_representation=4) with self.assertRaises(NotImplementedError): self.database.with_options(custom_uuid_representation) custom_unicode_error_hander = codec_options.CodecOptions( unicode_decode_error_handler='ignore') with self.assertRaises(NotImplementedError): self.database.with_options(custom_unicode_error_hander) custom_tzinfo = codec_options.CodecOptions(tz_aware=True, tzinfo=UTCPlus2()) with self.assertRaises(NotImplementedError): self.database.with_options(custom_tzinfo)
UINT = '<I' LONG = '<q' def unpack(format, buffer, start=0): end = start + struct.calcsize(format) return struct.unpack(format, buffer[start:end])[0], end def get_utf8_string(buffer, start=0): end = buffer.index(b"\x00", start) s = buffer[start:end].decode('utf8') return s, end + 1 CODEC_OPTIONS = codec_options.CodecOptions(document_class=OrderedDict) def decode_documents(buffer, start, content_size): docs = bson.decode_all(buffer[start:start + content_size], CODEC_OPTIONS) return docs, start + content_size class OperationResponder(): def __init__(self, responders): self.responders = responders @abstractmethod def handle(self, query_bytes): pass
def test__codec_options(self): client = mongomock.MongoClient() self.assertEqual(codec_options.CodecOptions(), client.codec_options)
def test__codec_options(self): self.assertEqual(codec_options.CodecOptions(), self.database.codec_options)
def test__get_collection_different_codec_options(self): database = mongomock.MongoClient().somedb a = database.get_collection( 'a', codec_options=codec_options.CodecOptions(tz_aware=True)) self.assertTrue(a.codec_options.tz_aware)
def test__codec_options_with_pymongo(self): client = mongomock.MongoClient() self.assertEqual(codec_options.CodecOptions(), client.codec_options) self.assertFalse(client.codec_options.tz_aware)
def codec_options(self): if not bson_codec_options: raise NotImplementedError( 'The codec options are not implemented in mongomock alone, you need to import ' 'the pymongo library as well.') return bson_codec_options.CodecOptions()
def test__get_collection_different_codec_options(self): database = mongomock.MongoClient().somedb with self.assertRaises(NotImplementedError): database.get_collection( 'a', codec_options=codec_options.CodecOptions(tz_aware=True))