def test_build_count_1(self, extract_columns_mock, build_from_clause_mock, count_mock, select_mock, append_where_clauses_mock): """Verify function structure of build_count(). """ executable_mock = Mock() select_from_mock = Mock() type(executable_mock).select_from = select_from_mock select_from_mock.return_value = executable_mock extract_columns_mock.return_value = self.columns build_from_clause_mock.return_value = self.phage count_mock.return_value = self.count_column select_mock.return_value = executable_mock append_where_clauses_mock.return_value = executable_mock querying.build_count(self.graph, self.columns, where=self.whereclauses) extract_columns_mock.assert_any_call(self.whereclauses) extract_columns_mock.assert_any_call(None, check=Column) total_columns = self.columns + self.columns + self.columns build_from_clause_mock.assert_called_once_with(self.graph, total_columns) for index in range(len(self.columns)): with self.subTest(columns_list_index=index): count_mock.assert_any_call(self.columns[index]) count_param = [self.count_column, self.count_column, self.count_column] select_mock.assert_called_once_with(count_param) select_from_mock.assert_called_once_with(self.phage) append_where_clauses_mock.assert_called_once_with( executable_mock, self.whereclauses)
def review_phams(db_filter, verbose=False): """Finds and stores phams with discrepant function calls in a Filter. """ notes = db_filter.get_column("gene.Notes") if verbose: print("Reviewing phams...") reviewed_phams = [] for index in range(len(db_filter.values)): pham = db_filter.values[index] if verbose: print(f"...Analyzing Pham {pham}...") query = querying.build_count(db_filter.graph, notes.distinct(), where=(db_filter.key == pham)) func_count = mysqldb_basic.scalar(db_filter.engine, query) if func_count <= 1: continue if verbose: print(f"......Detected discrepencies in Pham {pham}") reviewed_phams.append(pham) if verbose: print(f"Detected {len(reviewed_phams)} disrepent phams...") db_filter.values = reviewed_phams
def test_build_count_1(self): """Verify build_count() creates valid SQLAlchemy executable. """ count_query = querying.build_count(self.graph, self.PhageID) dict_list = query_dict_list(self.engine, count_query) count_dict = dict_list[0] self.assertTrue(isinstance(count_dict["count_1"], int))
def update_field(alchemist, update_ticket): """Attempts to update a field using information from an update_ticket. :param alchemist: A connected and fully build AlchemyHandler object. :type alchemist: AlchemyHandler :param update_ticket: Dictionary with instructions to update a field. :type update_ticket: dict """ try: table_map = alchemist.mapper.classes[update_ticket["table"]] except: print(f"\nInvalid table '{update_ticket['table']}'") return 0 table_obj = table_map.__table__ try: field = table_obj.c[update_ticket["field"]] except: print(f"\nInvalid replacement field '{update_ticket['field']}' " f"for table '{table_obj.name}'") return 0 try: key_field = table_obj.c[update_ticket["key_name"]] except: print(f"\nInvalid selection key '{update_ticket['key_name']}' " f"for table '{table_obj.name}'") return 0 primary_keys = list(table_obj.primary_key.columns) if key_field not in primary_keys: print(f"\nInvalid selection key '{update_ticket['key_name']}' " f"for table '{table_obj.name}'") return 0 key_value_clause = (key_field == update_ticket["key_value"]) key_value_query = querying.build_count(alchemist.graph, key_field, where=key_value_clause) key_value_count = mysqldb_basic.scalar(alchemist.engine, key_value_query) if key_value_count != 1: print(f"\nInvalid selection value '{update_ticket['key_value']}' " f"for key '{key_field.name}' in table '{table_obj.name}'") return 0 if update_ticket["value"] == "NULL": update_ticket["value"] = None statement = update(table_obj).where(key_value_clause).values( {field.name: update_ticket["value"]}) alchemist.engine.execute(statement) return 1
def test_build_count_2(self): """Verify build_count() appends WHERE clauses to executable. """ where_clause = (self.PhageID == "Trixie") count_query = querying.build_count(self.graph, self.PhageID, where=where_clause) dict_list = query_dict_list(self.engine, count_query) count_dict = dict_list[0] self.assertEqual(count_dict["count_1"], 1)
def test_build_count_3(self): """Verify build_count() recognizes multiple inputs as expected. """ where_clause = (self.Cluster == "A") count_query = querying.build_count( self.graph, [self.PhageID, self.Cluster.distinct()], where=where_clause) dict_list = query_dict_list(self.engine, count_query) count_dict = dict_list[0] self.assertTrue(count_dict["count_1"] > 1) self.assertEqual(count_dict["count_2"], 1)
def test_build_count_1(self, ExtractWhereClauses, ExtractOrderByClauses, BuildFromClause, Count, Select, AppendWhereClauses, AppendOrderByClauses): ExecutableMock = Mock() SelectFromMock = Mock() type(ExecutableMock).select_from = SelectFromMock SelectFromMock.return_value = ExecutableMock ExtractWhereClauses.return_value = self.columns ExtractOrderByClauses.return_value = self.columns BuildFromClause.return_value = self.phage Count.return_value = self.count_column Select.return_value = ExecutableMock AppendWhereClauses.return_value = ExecutableMock AppendOrderByClauses.return_value = ExecutableMock querying.build_count(self.graph, self.columns, where=self.whereclauses, order_by=self.columns) ExtractWhereClauses.assert_called_once_with(self.whereclauses) ExtractOrderByClauses.assert_called_once_with(self.columns) total_columns = self.columns + self.columns + self.columns BuildFromClause.assert_called_once_with(self.graph, total_columns) for index in range(len(self.columns)): with self.subTest(columns_list_index=index): Count.assert_any_call(self.columns[index]) count_param = [self.count_column, self.count_column, self.count_column] Select.assert_called_once_with(count_param) SelectFromMock.assert_called_once_with(self.phage) AppendWhereClauses.assert_called_once_with(ExecutableMock, self.whereclauses) AppendOrderByClauses.assert_called_once_with(ExecutableMock, self.columns)