def _collect_relation_info(self, relation_name: str, schema_collection_name: str): catalog_name, collection_name = resolve_schema_collection_name( schema_collection_name) if not (catalog_name and collection_name): raise InvalidQueryException( f"{schema_collection_name} is not a valid entity") collection = self.model.get_collection(catalog_name, collection_name) abbr = collection['abbreviation'].lower() abbr_cnt = len([ item for item in self.relation_info.values() if item['abbr'] == abbr ]) self.relation_info[relation_name] = { 'abbr': abbr, 'collection_name': collection_name, 'catalog_name': catalog_name, 'tablename': self.model.get_table_name(catalog_name, collection_name), 'alias': f'{abbr}_{abbr_cnt}', 'has_states': collection.get('has_states', False), 'collection': collection, 'attributes': collection['attributes'], 'all_fields': collection['all_fields'], } return self.relation_info[relation_name]
def test_resolve_schema_collection_name(self, mock_model_class): mock_model = mock.MagicMock() mock_model_class.return_value = mock_model mock_model.get_catalog = lambda cat: "catalog" if cat == "catalog" else None mock_model.get_collection = lambda cat, col: "collection" if col == "collection" else None result = resolve_schema_collection_name("catalogCollection") self.assertEqual(result, ('catalog', 'collection')) mock_model.get_catalog = lambda cat: None result = resolve_schema_collection_name("catalogCollection") self.assertEqual(result, (None, None)) mock_model.get_catalog = lambda cat: "catalog" if cat == "catalog_ext" else None mock_model.get_collection = lambda cat, col: "collection" if col == "collection" else None result = resolve_schema_collection_name("catalogExtCollection") self.assertEqual(result, ('catalog_ext', 'collection')) mock_model.get_catalog = lambda cat: "catalog" if cat == "catalog" else None mock_model.get_collection = lambda cat, col: "collection" if col == "ext_collection" else None result = resolve_schema_collection_name("catalogExtCollection") self.assertEqual(result, ('catalog', 'ext_collection'))
def _collect_relation_info(self, relation_name: str, schema_collection_name: str): catalog_name, collection_name = resolve_schema_collection_name(schema_collection_name) assert catalog_name and collection_name, f"{schema_collection_name} error" collection = self.model.get_collection(catalog_name, collection_name) abbr = collection['abbreviation'].lower() abbr_cnt = len([item for item in self.relation_info.values() if item['abbr'] == abbr]) self.relation_info[relation_name] = { 'abbr': abbr, 'collection_name': collection_name, 'catalog_name': catalog_name, 'tablename': self.model.get_table_name(catalog_name, collection_name), 'alias': f'{abbr}_{abbr_cnt}', 'has_states': collection.get('has_states', False), 'collection': collection, 'attributes': collection['attributes'], } return self.relation_info[relation_name]
def _build_query(cls, query, model, **kwargs): """Build a query to filter a model on the contents of kwargs :param query: the query to start with :param model: the model to filter :param kwargs: the filter arguments :return: the query to filter model on the filter arguments """ # Assure that query results are authorised catalog, collection = resolve_schema_collection_name(model.__tablename__) query.set_catalog_collection(catalog, collection) # Skip the default GraphQL filters RELAY_ARGS = ['first', 'last', 'before', 'after', 'sort', 'active'] for field, value in kwargs.items(): if field not in RELAY_ARGS: # null is defined as a special string value because Python None or JSON null does not work if value == FILTER_ON_NULL_VALUE: query = query.filter(getattr(model, field) == None) # noqa: E711 else: query = query.filter(getattr(model, field) == value) return query
def _build_query(self): relation_model = self._get_relation_model() query = getattr(self.dst_model, 'query') # Assure that query results are authorised catalog, collection = resolve_schema_collection_name(self.dst_model.__tablename__) query.set_catalog_collection(catalog, collection) query = query.select_from(relation_model) query = self._add_relation_table_filters(query, relation_model) query = self._add_dst_table_join(query, relation_model) if self.add_relation_table_columns: query = query.add_columns( getattr(relation_model, FIELD.SOURCE_VALUE), getattr(relation_model, FIELD.START_VALIDITY).label(API_FIELD.START_VALIDITY_RELATION), getattr(relation_model, FIELD.END_VALIDITY).label(API_FIELD.END_VALIDITY_RELATION), ) if self.kwargs.get('sort'): query = self._add_sort(query, self.kwargs['sort']) return query