def field2default(self, field: types.BaseType): """Return the dictionary containing the field's default value Args: field (types.BaseType): The field we're looking for a default value on Returns: dict: ie: {'default': False} """ datetypes = (types.DateTimeType, types.DateType, datetime.datetime) ret: dict = {} if hasattr(field, 'default') and field.default is not None: if isinstance(field.default, marshmallow.utils._Missing): return ret if not isinstance(field.default, UndefinedType): if isinstance(field.default, datetypes): try: ret["default"] = arrow.get(field.default).isoformat() except Exception as e: console.warn(e) ret["default"] = "" else: ret["default"] = field.default return ret
def _convert_fk(self, field, value, context): try: service = self._get_service_instance(field) setattr(field.owner_model, field._to_field, service.get(value)) except Exception as e: console.warn(e) return value
def _get_list_backrefs(self): if hasattr(self, 'list_backrefs'): for d in self.list_backrefs: field, service_name, prop = d[0], d[1], d[2] try: service = self._get_service_instance(service_name) query = {field: str(self.id)} related = service.find(**query) setattr(self, prop, related) except Exception as e: console.warn(e)
def _get_backrefs(self): if hasattr(self, 'backrefs'): for d in self.backrefs: field, service_name = d[0], d[1] try: service = self._get_service_instance(service_name) query = {field: str(self.id)} related = service.first(**query) prop = underscore(service.__model__.__name__) # console.log(f'Setting {prop} on {self} to {related}') setattr(self, prop, related) except Exception as e: console.warn(e)
def drop_tables(self): total = len(self._models) count = 0 for name, model in self._models.items(): count += 1 progress = count / total * 100 console.progress('Dropping tables', progress) try: # console.info('Drop {} table'.format(model._table)) q = model._db[model._table] q.delete_many({}) except Exception as e: console.warn(e) raise return True
def setup_teardown(): from smpa.app import db from smpa.db.documentdb.registry import model_registry console.log('ENTER TESTS') # Set up running = True console.warn(f'DB = {db.db}') if db.name.startswith('test_'): model_registry.purge() yield running # Tear down if db.name.startswith('test_'): model_registry.purge() else: console.warn('Refusing to drop non-test database') console.log('EXIT TESTS')
def purge(self): """Purges all records from all tables, without dropping those tables. """ total = len(self._models) count = 0 for name, model in self._models.items(): count += 1 progress = count / total * 100 console.progress('Dropping tables', progress) try: # console.info('Drop {} table'.format(model._table)) q = model._db[model._table] q.delete_many({}) except Exception as e: console.warn(e) raise return True
def __init__(self, config): console.info('CONFIG', config.to_dict()) self._config = config self._hostname = socket.gethostname() self._authenticated = False if config.DOCUMENT_DB_USER is not None: self._production_connect() else: self._staging_connect() try: console.info('CLIENT', self.client) if self.client: self.db = self.client[config.DOCUMENT_DB_DB] console.info('DB', self.db) except Exception: console.warn("Didn't create DB") pass
def json_match(result: dict, body: str) -> bool: """Test to see if the ``result`` from an API call contains all the data sent to it in ``body`` Args: result (str): The API call result body (dict): The data we sent to the API """ diffs = {} body_dict = json.loads(body) for k, v in body_dict.items(): if result[k] != v: diffs[k] = {"wanted": v, "found": result[k]} if len(diffs): console.warn('JSON MATCH ERROR') console.warn(diffs) return False return True
def _production_connect(self): config = self._config self._user = quote_plus(config.DOCUMENT_DB_USER) self._password = quote_plus(config.DOCUMENT_DB_PASSWORD) self._super_user = quote_plus(config.DOCUMENT_DB_SUPERUSER) self._super_password = quote_plus(config.DOCUMENT_DB_SUPERPASS) # self._cert = '/usr/srv/rds-combined-ca-bundle.pem' self._cert = '/tmp/rds-combined-ca-bundle.pem' if not self._ensure_cert_exists(): console.warn('Cert not found') try: connection_string = self.get_connection_str() self.client = MongoClient(connection_string, connect=False) self.init() self.client.admin.command('ismaster') self.db = self.client[config.DOCUMENT_DB_DB] self._test_transaction() except TransactionFailure as e: bugsnag.notify(e, extra_data={ 'connection_type': 'string', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'connection_string': connection_string, 'config': config.to_dict(), }, severity='error') try: self.client = MongoClient( host=self._config.DOCUMENT_DB_HOST, port=int(self._config.DOCUMENT_DB_PORT), ssl=True, ssl_ca_certs=self._cert, connect=True, connectTimeoutMS=self._config.DOCUMENT_DB_CONNECT_TIMEOUT, serverSelectionTimeoutMS=self._config. DOCUMENT_DB_CONNECT_TIMEOUT) self.init() self.client.admin.command('ismaster') self.db = self.client[config.DOCUMENT_DB_DB] self._test_transaction() except TransactionFailure as e: bugsnag.notify(e, extra_data={ 'connection_type': 'object', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'client': self.client, 'config': config.to_dict(), }, severity='error') try: self.client = MongoClient( host=self._config.DOCUMENT_DB_HOST, port=int(self._config.DOCUMENT_DB_PORT), ssl=False, connect=True, connectTimeoutMS=self._config. DOCUMENT_DB_CONNECT_TIMEOUT, serverSelectionTimeoutMS=self._config. DOCUMENT_DB_CONNECT_TIMEOUT) self.init() self.client.admin.command('ismaster') self.db = self.client[config.DOCUMENT_DB_DB] self._test_transaction() except TransactionFailure as e: bugsnag.notify(e, extra_data={ 'connection_type': 'object NO SSL', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'client': self.client, 'config': config.to_dict(), }, severity='error') else: bugsnag.notify(Exception('CONNECTION SUCCEEDED'), extra_data={ 'connection_type': 'object NO SSL', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'client': self.client, 'config': config.to_dict(), }, severity='info') else: bugsnag.notify(Exception('CONNECTION SUCCEEDED'), extra_data={ 'connection_type': 'object', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'client': self.client, 'config': config.to_dict(), }, severity='info') except Exception as e: console.warn('Failed to do full connection') console.warn(e) bugsnag.notify(e, extra_data={ 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'connection_string': connection_string, 'config': config.to_dict(), }, severity='error') else: bugsnag.notify(Exception('CONNECTION SUCCEEDED'), extra_data={ 'connection_type': 'string', 'hostname': self._hostname, 'server_env': os.environ.get('SERVER_ENV'), 'connection_string': connection_string, 'config': config.to_dict(), }, severity='info')