def test_create(self): result, new_result = quiz_model_mutation_create( self.client, graphql_update_or_create_project, 'createProject.project', dict( name='Carre', key='carre', geojson={ 'type': 'FeatureCollection', 'features': [{ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [[49.5294835476, 2.51357303225], [51.4750237087, 2.51357303225], [51.4750237087, 6.15665815596], [49.5294835476, 6.15665815596], [49.5294835476, 2.51357303225]]] } }] }, data=dict(), locations=R.map(R.compose(R.pick(['id']), lambda l: l.__dict__), self.locations), user=R.pick(['id'], R.head(self.users).__dict__), ), dict(key=r'carre.+')) versions = Version.objects.get_for_object(get_project_model().objects.get( id=R.item_str_path('data.createProject.project.id', result) )) assert len(versions) == 1
def test_create(self): values = dict( name='Candy', region=dict(id=R.head(self.regions).id), data=R.merge( sample_settings, dict( material='Candy', raw_data=[ 'Other Global Imports;Shipments, location generalized;51.309933, 3.055030;Source;22,469,843', 'Knauf (Danilith) BE;Waregemseweg 156-142 9790 Wortegem-Petegem, Belgium;50.864762, 3.479308;Conversion;657,245', "MPRO Bruxelles;Avenue du Port 67 1000 Bruxelles, Belgium;50.867486, 4.352543;Distribution;18,632", 'Residential Buildings (all typologies);Everywhere in Brussels;NA;Demand;3,882,735', 'Duplex House Typology;Everywhere in Brussels;NA;Demand;13,544', 'Apartment Building Typology;Everywhere in Brussels;NA;Demand;34,643', 'New West Gypsum Recycling;9130 Beveren, Sint-Jansweg 9 Haven 1602, Kallo, Belgium;51.270229, 4.261048;Reconversion;87,565', 'Residential Buildings (all typologies);Everywhere in Brussels;NA;Sink;120,000', 'RecyPark South;1190 Forest, Belgium;50.810799, 4.314789;Sink;3,130', 'RecyPark Nord;Rue du Rupel, 1000 Bruxelles, Belgium;50.880181, 4.377136;Sink;1,162' ] ) ) ) result = graphql_update_or_create_resource(self.client, values) dump_errors(result) assert not R.has('errors', result), R.dump_json(R.prop('errors', result)) # look at the users added and omit the non-determinant dateJoined result_path_partial = R.item_path(['data', 'createResource', 'resource']) self.assertMatchSnapshot(R.omit(omit_props, result_path_partial(result)))
def test_update(self): result, update_result = quiz_model_mutation_update( self.client, graphql_update_or_create_project, 'createProject.project', 'updateProject.project', dict( name='Carre', key='carre', geojson={ 'type': 'FeatureCollection', 'features': [{ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [[49.4426671413, 5.67405195478], [50.1280516628, 5.67405195478], [50.1280516628, 6.24275109216], [49.4426671413, 6.24275109216], [49.4426671413, 5.67405195478]]] } }] }, data=dict(), locations=R.map(R.compose(R.pick(['id']), lambda l: l.__dict__), self.locations), user=R.pick(['id'], R.head(self.users).__dict__), ), # Update the coords and limit to one location dict( geojson={ 'features': [{ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [[49.5294835476, 2.51357303225], [51.4750237087, 2.51357303225], [51.4750237087, 6.15665815596], [49.5294835476, 6.15665815596], [49.5294835476, 2.51357303225]]] } }] }, locations=R.map(R.compose(R.pick(['id']), lambda l: l.__dict__), [R.head(self.locations)]) ) ) versions = Version.objects.get_for_object(get_project_model().objects.get( id=R.item_str_path('data.updateProject.project.id', update_result) )) assert len(versions) == 2
def test_update(self): # First add a new User margay = dict(username="******", first_name='Upa', last_name='Tree', password=make_password("merowgir", salt='not_random')) user = create_sample_user(margay) # Now assign regions and persist the UserState sample_user_state_data = dict( user=dict(id=user.id), data=form_sample_user_state_data( self.regions, self.projects, dict( userRegions=[ dict( # Assign the first region region=dict( key=R.prop('key', R.head(self.regions))), mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7))) ], userProjects=[ dict( # Assign the first prjoect project=dict( key=R.prop('key', R.head(self.projects))), mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7))) ]))) # Update the zoom of the first userRegion update_data = deepcopy(R.pick(['data'], sample_user_state_data)) R.item_str_path( 'mapbox.viewport', R.head(R.item_str_path('data.userRegions', (update_data))))['zoom'] = 15 result, update_result = quiz_model_mutation_update( self.client, R.prop('graphql_mutation', self.user_state_schema), 'createUserState.userState', 'updateUserState.userState', sample_user_state_data, update_data) versions = Version.objects.get_for_object( UserState.objects.get(id=R.item_str_path( 'data.updateUserState.userState.id', update_result))) assert len(versions) == 2
def setUp(self): users = create_sample_users() self.client = client_for_testing(schema, users[0]) delete_sample_resources() self.region = R.head(create_sample_regions(Region)) self.resources = create_sample_resources([self.region]) # Create a graph for all resources # This modifies each node to have a node_index self.graph = create_sankey_graph_from_resources(self.resources)
def fetch_and_merge(modified_user_state_data, props): existing = UserState.objects.filter(**props) # If the user doesn't have a user state yet if not R.length(existing): return modified_user_state_data return merge_data_fields_on_update( ['data'], R.head(existing), # Merge existing's id in case it wasn't in user_state_data R.merge(modified_user_state_data, R.pick(['id'], existing)) )
def get_versioner(single_object_qs, versions_type, **kwargs): """ Cre First we create a little helper function, becase we will potentially have many PaginatedTypes and we will potentially want to turn many querysets into paginated results: :param single_object_qs: The queryset that must return exactly one instance :param versions_type Class created by create_versions_type to hold all the versions of one model instance :param kwargs: Addition kwargs to versioned_type, usually not needed :return: """ instance = R.head(single_object_qs) versions = Version.objects.get_for_object(instance) return versions_type( objects=list( versions ), # R.map(lambda version: version._object_version.object, list(versions)), **kwargs)
def resolve_version_instance(model_versioned_type, resolver, **kwargs): """ Queries for the version instance by for the given model type using the given resolver The kwargs must contain objects: [{id: the id}] :param model_versioned_type: Graphene model class created by create_version_container_type :param resolver: Resolver for the model :param kwargs: Must contain objects: [{id: the id}] to resolve the versions of the instance given by id :return: """ # We technically receive an array but never accept more than the first item obj = R.head(R.prop('objects', kwargs)) if not R.item_str_path_or(None, 'instance.id', obj): raise Exception( f"id required in kwargs.objects.instance for revisions query, but got: {kwargs}" ) # Create the filter that only returns 1 location objs = resolver('filter', **R.prop_or({}, 'instance', obj)).order_by('id') return get_versioner( objs, model_versioned_type, )
def map_value(value): """ value is either a ListValue with values that are ListValues or a ListValue with values that are FloatValues :param value: :return: """ def handle_floats(v): if hasattr(v, 'values'): # Multiple floats:w return R.map(lambda fv: float(fv.value), v.values) else: # Single float return float(v.value) return R.if_else( lambda v: R.isinstance( ListValue, R.head(v.values) if hasattr(v, 'values') else v), # ListValues lambda v: [reduce(v.values)], # FloatValues or single FloatValue lambda v: [handle_floats(v)])(value)
def create_sample_resources(): regions = create_sample_regions() # Convert all sample resource dicts to persisted Resource instances resources = R.map(create_sample_resource(R.head(regions)), sample_resources) return resources
def latest_version(self): # The latest version object return R.head(list(Version.objects.get_for_object(self)))
def test_create(self): # First add a new User margay = dict(username="******", first_name='Upa', last_name='Tree', password=make_password("merowgir", salt='not_random')) user = create_sample_user(margay) # Now assign regions and persist the UserState sample_user_state_data = dict( user=dict(id=user.id), data=form_sample_user_state_data( self.regions, self.projects, dict( userGlobal=dict(mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7))), userRegions=[ dict( # Assign the first region region=dict( key=R.prop('key', R.head(self.regions))), mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7)), userSearch=dict(userSearchLocations=R.concat( R.map( lambda search_location: dict( searchLocation=R.pick(['id'], search_location), activity=dict(isActive=True)), self.search_locations), # Search locations can be created on the fly [ dict(searchLocation=dict( name="I am a new search"), activity=dict(isActive=True)) ])), **self.additional_user_scope_data) ], userProjects=[ dict( # Assign the first project project=dict( key=R.prop('key', R.head(self.projects))), mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7)), userSearch=dict(userSearchLocations=R.map( lambda search_location: dict( searchLocation=R.pick(['id'], search_location), activity=dict(isActive=True)), self.search_locations)), **self.additional_user_scope_data), dict( # Create a new project when creating the userProject project=dict(user=R.pick(['id'], user), region=R.pick(['id'], R.head(self.regions)), key='newProject', name='New Project', locations=R.map( R.pick(['id']), self.locations)), mapbox=dict(viewport=dict( latitude=50.5915, longitude=2.0165, zoom=7)), userSearch=dict(userSearchLocations=R.map( lambda search_location: dict( searchLocation=R.pick(['id'], search_location), activity=dict(isActive=True)), self.search_locations)), **self.additional_user_scope_data) ]))) result, _ = quiz_model_mutation_create( self.client, R.prop('graphql_mutation', self.user_state_schema), 'createUserState.userState', sample_user_state_data, # The second create should update, since we can only have one userState per user dict(), True) versions = Version.objects.get_for_object( UserState.objects.get(id=R.item_str_path( 'data.createUserState.userState.id', result))) assert len(versions) == 1