def select(self, sql: str, params=None): """ Perform generic select statement Args: sql (str): SQL statement params (tuple|dict): Params for binding Returns: Result """ cursor = self.get_cursor() try: if params is None: cursor.execute(sql) else: cursor.execute(sql, params) data = cursor.fetchall() result = Result(True, "", data) if len(data) > 0: item = data[0] result.set_full_count(item["full_count"] if "full_count" in item else -1) return result except Exception as e: self.__connection.rollback() return Result(False, str(e)) finally: cursor.close()
def search(self, **kwargs) -> Result: """ Search super user list Args: **kwargs: username (str) first_name (str) last_name (str) statuses (list) - List of status IDs sorts (dict) - <key> column: <value> 1(ASC), -1(DESC) offset (int) limit (int) Returns: Result """ fetch_result = self.__user_super_data.search(**kwargs) if not fetch_result.get_status(): raise UserSuperSearchError("Could not load super users") data = fetch_result.get_data() users = [] for datum in data: users.append( User(id=datum["id"], uuid=datum["uuid"], username=datum["username"], first_name=datum["first_name"], last_name=datum["last_name"], status=self.__statuses.get_by_id(datum["status_id"]))) result = Result(True, "", users) result.set_full_count(fetch_result.get_full_count()) return result
def test_search_searches_pools(self): data = [{ "id": 1, "uuid": "ERT-123", "status_id": self.active_status.get_id(), "const": "CONST_ONE" }, { "id": 2, "uuid": "ERT-456", "status_id": self.active_status.get_id(), "const": "CONST_TWO" }] result = Result(True, "", data) result.set_full_count(len(data)) self.postgres_conn_manager.paginate = MagicMock(return_value=result) fetch_result = self.pool_manager.search( const="CONST", statuses=[self.active_status.get_id()]) self.assertEqual(len(data), fetch_result.get_full_count()) fetched_pools = fetch_result.get_data() self.assertEqual(len(data), len(fetched_pools)) pool: Pool for i in range(0, len(fetched_pools)): pool = fetched_pools[i] self.assertEqual(data[i]["id"], pool.get_id()) self.assertEqual(data[i]["uuid"], pool.get_uuid()) self.assertEqual(data[i]["status_id"], pool.get_status().get_id()) self.assertEqual(data[i]["const"], pool.get_const())
def search(self, pool_ids: list, **kwargs) -> Result: """ Search user in pools Args: pool_ids (list): List of pool IDs **kwargs: username (str) first_name (str) last_name (str) statuses (list) - List of status IDs sorts (dict) - <key> column: <value> 1(ASC), -1(DESC) offset (int) limit (int) Returns: Result """ if len(pool_ids) == 0: raise UserPoolSearchError("No pools specified for search") fetch_result = self.__user_pool_data.search(pool_ids, **kwargs) if not fetch_result.get_status(): raise UserPoolSearchError("Could not search users in pools") data = fetch_result.get_data() users = [] for datum in data: users.append( User(id=datum["id"], uuid=datum["uuid"], username=datum["username"], first_name=datum["first_name"], last_name=datum["last_name"], status=self.__statuses.get_by_id(datum["status_id"]))) result = Result(True, "", users) result.set_full_count(fetch_result.get_full_count()) return result
def test_search_searches_users_in_pools(self): users = [ { "id": 1, "uuid": "ERT-123", "username": "******", "first_name": "John", "last_name": "Johnson", "status_id": self.status_active.get_id() }, { "id": 2, "uuid": "ERT-456", "username": "******", "first_name": "Jim", "last_name": "Jameson", "status_id": self.status_active.get_id() } ] result = Result(True, "", users) result.set_full_count(2) self.postgres_conn_manager.paginate = MagicMock(return_value=result) result_fetch = self.user_pool_manager.search( [1, 2], username="******", first_name="J", last_name="J", statuses=[self.status_active.get_id()], sorts={"username": 1}, offset=0, limit=2 ) user_objs = result_fetch.get_data() self.assertEqual(len(users), len(user_objs)) self.assertEqual(2, result_fetch.get_full_count()) for i in range(0, len(user_objs)): user: User = user_objs[i] self.assertEqual(users[i]["id"], user.get_id()) self.assertEqual(users[i]["uuid"], user.get_uuid()) self.assertEqual(users[i]["username"], user.get_username()) self.assertEqual(users[i]["first_name"], user.get_first_name()) self.assertEqual(users[i]["last_name"], user.get_last_name()) self.assertEqual(users[i]["status_id"], user.get_status().get_id())
def search(self, **kwargs) -> Result: """ Search pools by params Args: **kwargs: const (str) statuses (list) List of status IDs offset (int) limit (int) Returns: Result """ fetch_result = self.__pool_data.search(**kwargs) if not fetch_result.get_status(): raise PoolSearchError("Could not load pools") data = fetch_result.get_data() pools = [] for datum in data: pools.append(self.__build_pool_obj(datum)) result = Result(True, "", pools) result.set_full_count(fetch_result.get_full_count()) return result
def search(self, **kwargs) -> Result: """ Search users Args: **kwargs: username (str) first_name (str) last_name (str) statuses (list) - List of status IDs sorts (dict) - <key> column: <value> 1(ASC), -1(DESC) offset (int) limit (int) Returns: Result """ fetch_result = self.__user_data.search(**kwargs) if not fetch_result.get_status(): raise UserSearchError("Could not load users") data = fetch_result.get_data() users = [] for datum in data: users.append(self.__build_user_obj(datum)) result = Result(True, "", users) result.set_full_count(fetch_result.get_full_count()) return result