Best Python code snippet using lisa_python
graphql_queries.py
Source:graphql_queries.py
...4 DEFAULT_SUBSET_FIELDS,5 DEFAULT_VERSION_FIELDS,6)7from .graphql import FIELD_VALUE, GraphQlQuery8def fields_to_dict(fields):9 if not fields:10 return None11 output = {}12 for field in fields:13 hierarchy = field.split(".")14 last = hierarchy.pop(-1)15 value = output16 for part in hierarchy:17 if value is FIELD_VALUE:18 break19 if part not in value:20 value[part] = {}21 value = value[part]22 if value is not FIELD_VALUE:23 value[last] = FIELD_VALUE24 return output25def project_graphql_query(fields):26 query = GraphQlQuery("ProjectQuery")27 project_name_var = query.add_variable("projectName", "String!")28 project_field = query.add_field("project")29 project_field.set_filter("name", project_name_var)30 nested_fields = fields_to_dict(fields)31 query_queue = collections.deque()32 for key, value in nested_fields.items():33 query_queue.append((key, value, project_field))34 while query_queue:35 item = query_queue.popleft()36 key, value, parent = item37 field = parent.add_field(key)38 if value is FIELD_VALUE:39 continue40 for k, v in value.items():41 query_queue.append((k, v, field))42 return query43def projects_graphql_query(fields):44 query = GraphQlQuery("ProjectsQuery")45 projects_field = query.add_field("projects", has_edges=True)46 nested_fields = fields_to_dict(fields)47 query_queue = collections.deque()48 for key, value in nested_fields.items():49 query_queue.append((key, value, projects_field))50 while query_queue:51 item = query_queue.popleft()52 key, value, parent = item53 field = parent.add_field(key)54 if value is FIELD_VALUE:55 continue56 for k, v in value.items():57 query_queue.append((k, v, field))58 return query59def folders_graphql_query(fields):60 query = GraphQlQuery("FoldersQuery")61 project_name_var = query.add_variable("projectName", "String!")62 folder_ids_var = query.add_variable("folderIds", "[String!]")63 parent_folder_ids_var = query.add_variable("parentFolderIds", "[String!]")64 folder_paths_var = query.add_variable("folderPaths", "[String!]")65 folder_names_var = query.add_variable("folderNames", "[String!]")66 has_subsets_var = query.add_variable("folderHasSubsets", "Boolean!")67 project_field = query.add_field("project")68 project_field.set_filter("name", project_name_var)69 folders_field = project_field.add_field("folders", has_edges=True)70 folders_field.set_filter("ids", folder_ids_var)71 folders_field.set_filter("parentIds", parent_folder_ids_var)72 folders_field.set_filter("names", folder_names_var)73 folders_field.set_filter("paths", folder_paths_var)74 folders_field.set_filter("hasSubsets", has_subsets_var)75 nested_fields = fields_to_dict(fields)76 query_queue = collections.deque()77 for key, value in nested_fields.items():78 query_queue.append((key, value, folders_field))79 while query_queue:80 item = query_queue.popleft()81 key, value, parent = item82 field = parent.add_field(key)83 if value is FIELD_VALUE:84 continue85 for k, v in value.items():86 query_queue.append((k, v, field))87 return query88def tasks_graphql_query(fields):89 query = GraphQlQuery("TasksQuery")90 project_name_var = query.add_variable("projectName", "String!")91 task_ids_var = query.add_variable("taskIds", "[String!]")92 task_names_var = query.add_variable("taskNames", "[String!]")93 task_types_var = query.add_variable("taskTypes", "[String!]")94 folder_ids_var = query.add_variable("folderIds", "[String!]")95 project_field = query.add_field("project")96 project_field.set_filter("name", project_name_var)97 tasks_field = project_field.add_field("folders", has_edges=True)98 tasks_field.set_filter("ids", task_ids_var)99 # WARNING: At moment when this been created 'names' filter is not supported100 tasks_field.set_filter("names", task_names_var)101 tasks_field.set_filter("taskTypes", task_types_var)102 tasks_field.set_filter("folderIds", folder_ids_var)103 nested_fields = fields_to_dict(fields)104 query_queue = collections.deque()105 for key, value in nested_fields.items():106 query_queue.append((key, value, tasks_field))107 while query_queue:108 item = query_queue.popleft()109 key, value, parent = item110 field = parent.add_field(key)111 if value is FIELD_VALUE:112 continue113 for k, v in value.items():114 query_queue.append((k, v, field))115 return query116def subsets_graphql_query(fields):117 query = GraphQlQuery("SubsetsQuery")118 project_name_var = query.add_variable("projectName", "String!")119 folder_ids_var = query.add_variable("folderIds", "[String!]")120 subset_ids_var = query.add_variable("subsetIds", "[String!]")121 subset_names_var = query.add_variable("subsetNames", "[String!]")122 project_field = query.add_field("project")123 project_field.set_filter("name", project_name_var)124 subsets_field = project_field.add_field("subsets", has_edges=True)125 subsets_field.set_filter("ids", subset_ids_var)126 subsets_field.set_filter("names", subset_names_var)127 subsets_field.set_filter("folderIds", folder_ids_var)128 nested_fields = fields_to_dict(set(fields))129 query_queue = collections.deque()130 for key, value in nested_fields.items():131 query_queue.append((key, value, subsets_field))132 while query_queue:133 item = query_queue.popleft()134 key, value, parent = item135 field = parent.add_field(key)136 if value is FIELD_VALUE:137 continue138 for k, v in value.items():139 query_queue.append((k, v, field))140 return query141def versions_graphql_query(fields):142 query = GraphQlQuery("VersionsQuery")143 project_name_var = query.add_variable("projectName", "String!")144 subset_ids_var = query.add_variable("subsetIds", "[String!]")145 version_ids_var = query.add_variable("versionIds", "[String!]")146 versions_var = query.add_variable("versions", "[Int]")147 hero_only_var = query.add_variable("heroOnly", "Boolean")148 latest_only_var = query.add_variable("latestOnly", "Boolean")149 hero_or_latest_only_var = query.add_variable(150 "heroOrLatestOnly", "Boolean"151 )152 project_field = query.add_field("project")153 project_field.set_filter("name", project_name_var)154 subsets_field = project_field.add_field("versions", has_edges=True)155 subsets_field.set_filter("ids", version_ids_var)156 subsets_field.set_filter("subsetIds", subset_ids_var)157 subsets_field.set_filter("versions", versions_var)158 subsets_field.set_filter("heroOnly", hero_only_var)159 subsets_field.set_filter("latestOnly", latest_only_var)160 subsets_field.set_filter("heroOrLatestOnly", hero_or_latest_only_var)161 nested_fields = fields_to_dict(set(fields))162 query_queue = collections.deque()163 for key, value in nested_fields.items():164 query_queue.append((key, value, subsets_field))165 while query_queue:166 item = query_queue.popleft()167 key, value, parent = item168 field = parent.add_field(key)169 if value is FIELD_VALUE:170 continue171 for k, v in value.items():172 query_queue.append((k, v, field))173 return query174def representations_graphql_query(fields):175 query = GraphQlQuery("RepresentationsQuery")176 project_name_var = query.add_variable("projectName", "String!")177 repre_ids_var = query.add_variable("representationIds", "[String!]")178 repre_names_var = query.add_variable("representationNames", "[String!]")179 version_ids_var = query.add_variable("versionIds", "[String!]")180 project_field = query.add_field("project")181 project_field.set_filter("name", project_name_var)182 repres_field = project_field.add_field("representations", has_edges=True)183 repres_field.set_filter("ids", repre_ids_var)184 repres_field.set_filter("versionIds", version_ids_var)185 repres_field.set_filter("representationNames", repre_names_var)186 nested_fields = fields_to_dict(set(fields))187 query_queue = collections.deque()188 for key, value in nested_fields.items():189 query_queue.append((key, value, repres_field))190 while query_queue:191 item = query_queue.popleft()192 key, value, parent = item193 field = parent.add_field(key)194 if value is FIELD_VALUE:195 continue196 for k, v in value.items():197 query_queue.append((k, v, field))198 return query199def reprersentations_parents_qraphql_query():200 query = GraphQlQuery("RepresentationsParentsQuery")201 project_name_var = query.add_variable("projectName", "String!")202 repre_ids_var = query.add_variable("representationIds", "[String!]")203 project_field = query.add_field("project")204 project_field.set_filter("name", project_name_var)205 repres_field = project_field.add_field("representations", has_edges=True)206 repres_field.add_field("id")207 repres_field.set_filter("ids", repre_ids_var)208 version_field = repres_field.add_field("version")209 fields_queue = collections.deque()210 for key, value in fields_to_dict(DEFAULT_VERSION_FIELDS).items():211 fields_queue.append((key, value, version_field))212 subset_field = version_field.add_field("subset")213 for key, value in fields_to_dict(DEFAULT_SUBSET_FIELDS).items():214 fields_queue.append((key, value, subset_field))215 folder_field = subset_field.add_field("folder")216 for key, value in fields_to_dict(DEFAULT_FOLDER_FIELDS).items():217 fields_queue.append((key, value, folder_field))218 while fields_queue:219 item = fields_queue.popleft()220 key, value, parent = item221 field = parent.add_field(key)222 if value is FIELD_VALUE:223 continue224 for k, v in value.items():225 fields_queue.append((k, v, field))...
test_schemas.py
Source:test_schemas.py
1from tests.unit import UnitTestCase2from app.api.schemas import Schema3from marshmallow import fields4class SchemasTestCase(UnitTestCase):5 def test_fields_to_dict(self):6 from app.api.schemas import fields_to_dict7 self.assertEqual({}, fields_to_dict(None))8 self.assertEqual({}, fields_to_dict(''))9 _fields = 'id,name'10 result = fields_to_dict(_fields)11 expected = {12 'id': {},13 'name': {}14 }15 self.assertEqual(result, expected)16 _fields = 'id,name,roles{id,name,description}'17 expected = {18 'id': {},19 'name': {},20 'roles': {21 'id': {},22 'name': {},23 'description': {}24 }25 }26 result = fields_to_dict(_fields)27 self.assertEqual(result, expected)28 _fields = 'id,name,roles[0:5]{id,name,description}'29 expected = {30 'id': {},31 'name': {},32 'roles': {33 '__slice': '0:5',34 'id': {},35 'name': {},36 'description': {}37 }38 }39 self.assertEqual(fields_to_dict(_fields), expected)40 _fields = 'id,name,roles[0:5]{id,name,description},' +\41 'A{12{A1,A2},34{A3,A4[300:400]},56{A7{A8{}}}}'42 expected = {43 'id': {},44 'name': {},45 'roles': {46 '__slice': '0:5',47 'id': {},48 'name': {},49 'description': {}50 },51 'A': {52 '12': {53 'A1': {},54 'A2': {}55 },56 '34': {57 'A3': {},58 'A4': {59 '__slice': '300:400',60 }61 },62 '56': {63 'A7': {64 'A8': {}65 }66 }67 }68 }69 self.assertEqual(fields_to_dict(_fields), expected)70 def test_filter_dict(self):71 from app.api.schemas import filter_dict72 origin_dict = {73 'id': 1,74 'email': 'test@example.com',75 'active': True,76 'roles': [{77 'id': 1,78 'name': 'user',79 'description': 'user role'80 }, {81 'id': 2,82 'name': 'admin',83 'description': 'admin role'...
id2xs
Source:id2xs
1#! /usr/bin/env python2from pyAMI import pyAMI3from IPython.Shell import IPShellEmbed as E; sh = E(["-pdb"])4def fields_to_dict(fields):5 return dict((f.attributes["name"].value, getattr(f.firstChild, "data", None))6 for f in fields)7def ami_result_to_dict(ami_result):8 return fields_to_dict(ami_result.getAMIdom().getElementsByTagName("field"))9def get_ds_info(ds_name):10 argument=[]11 argument.append("GetDatasetInfo")12 argument.append("logicalDatasetName=%s" % ds_name)13 amiclient = pyAMI.AMI()14 result = amiclient.execute(argument)15 r = ami_result_to_dict(result)16 assert r["crossSection_unit"] == "nano barn"17 return float(r["crossSection_mean"]), int(r["totalEvents"]), float(r["GenFiltEff_mean"])18def datasets_from_id(ds_number):19 argument=[]20 argument.append("SearchQuery")21 argument.append("entity=dataset")22 argument.append("glite=SELECT logicalDatasetName WHERE "...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!