def test_field_with_two_kwargs(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] field = data['relationships']['two_url_kwargs']['links'] assert_in('/v2/nodes/{}/node_links/{}/'.format(node._id, node._id), field['related']['href'])
def test_field_with_non_attribute(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] field = data['relationships']['not_attribute_on_target']['links'] assert_in('/v2/nodes/{}/children/'.format('12345'), field['related']['href'])
def test_node_serialization(self): parent = ProjectFactory(creator=self.user) node = NodeFactory(creator=self.user, parent=parent) req = make_drf_request() result = NodeSerializer(node, context={"request": req}).data data = result["data"] assert_equal(data["id"], node._id) assert_equal(data["type"], "nodes") # Attributes attributes = data["attributes"] assert_equal(attributes["title"], node.title) assert_equal(attributes["description"], node.description) assert_equal(attributes["public"], node.is_public) assert_equal(attributes["tags"], [str(each) for each in node.tags]) assert_equal(attributes["category"], node.category) assert_equal(attributes["registration"], node.is_registration) assert_equal(attributes["fork"], node.is_fork) assert_equal(attributes["collection"], node.is_folder) # Relationships relationships = data["relationships"] assert_in("children", relationships) assert_in("contributors", relationships) assert_in("files", relationships) assert_in("parent", relationships) parent_link = relationships["parent"]["links"]["related"]["href"] assert_equal(urlparse(parent_link).path, "/{}nodes/{}/".format(API_BASE, parent._id)) assert_in("registrations", relationships) # Not a fork, so forked_from is removed entirely assert_not_in("forked_from", relationships)
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={'request': req}).data data = result['data'] assert_equal(data['id'], reg._id) assert_equal(data['type'], 'registrations') # Attributes attributes = data['attributes'] assert_datetime_equal(parse_date(attributes['date_registered']), reg.registered_date) assert_equal(attributes['retracted'], reg.is_retracted) # Relationships relationships = data['relationships'] assert_in('registered_by', relationships) registered_by = relationships['registered_by']['links']['related'][ 'href'] assert_equal( urlparse(registered_by).path, '/{}users/{}/'.format(API_BASE, user._id)) assert_in('registered_from', relationships) registered_from = relationships['registered_from']['links']['related'][ 'href'] assert_equal( urlparse(registered_from).path, '/{}nodes/{}/'.format(API_BASE, reg.registered_from._id))
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={'request': req}).data data = result['data'] assert_equal(data['id'], reg._id) assert_equal(data['type'], 'registrations') # Attributes attributes = data['attributes'] assert_datetime_equal( parse_date(attributes['date_registered']), reg.registered_date ) assert_equal(attributes['retracted'], reg.is_retracted) # Relationships relationships = data['relationships'] assert_in('registered_by', relationships) registered_by = relationships['registered_by']['links']['related']['href'] assert_equal( urlparse(registered_by).path, '/{}users/{}/'.format(API_BASE, user._id) ) assert_in('registered_from', relationships) registered_from = relationships['registered_from']['links']['related']['href'] assert_equal( urlparse(registered_from).path, '/{}nodes/{}/'.format(API_BASE, reg.registered_from._id) )
def test_collection_serialization(self): collection = CollectionFactory(creator=self.user) req = make_drf_request() result = CollectionSerializer(collection, context={ 'request': req }).data data = result['data'] assert_equal(data['id'], collection._id) assert_equal(data['type'], 'collections') # Attributes attributes = data['attributes'] assert_equal(attributes['title'], collection.title) assert_equal(attributes['date_created'], collection.date_created.isoformat()) assert_equal(attributes['date_modified'], collection.date_modified.isoformat()) assert_equal(attributes['bookmarks'], collection.is_bookmark_collection) # Relationships relationships = data['relationships'] assert_in('node_links', relationships) # Bunch of stuff in Nodes that should not be in Collections assert_not_in('contributors', relationships) assert_not_in('files', relationships) assert_not_in('parent', relationships) assert_not_in('registrations', relationships) assert_not_in('forked_from', relationships)
def test_node_serialization(self): parent = ProjectFactory(creator=self.user) node = NodeFactory(creator=self.user, parent=parent) req = make_drf_request() result = NodeSerializer(node, context={'request': req}).data data = result['data'] assert_equal(data['id'], node._id) assert_equal(data['type'], 'nodes') # Attributes attributes = data['attributes'] assert_equal(attributes['title'], node.title) assert_equal(attributes['description'], node.description) assert_equal(attributes['public'], node.is_public) assert_equal(attributes['tags'], [str(each) for each in node.tags]) assert_equal(attributes['category'], node.category) assert_equal(attributes['registration'], node.is_registration) assert_equal(attributes['fork'], node.is_fork) assert_equal(attributes['collection'], node.is_collection) # Relationships relationships = data['relationships'] assert_in('children', relationships) assert_in('contributors', relationships) assert_in('files', relationships) assert_in('parent', relationships) assert_in('primary_institution', relationships) parent_link = relationships['parent']['links']['related']['href'] assert_equal( urlparse(parent_link).path, '/{}nodes/{}/'.format(API_BASE, parent._id) ) assert_in('registrations', relationships) # Not a fork, so forked_from is removed entirely assert_not_in('forked_from', relationships)
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={"request": req}).data data = result["data"] assert_equal(data["id"], reg._id) assert_equal(data["type"], "registrations") should_not_relate_to_registrations = ["registered_from", "registered_by", "registration_schema"] # Attributes attributes = data["attributes"] assert_datetime_equal(parse_date(attributes["date_registered"]), reg.registered_date) assert_equal(attributes["withdrawn"], reg.is_retracted) # Relationships relationships = data["relationships"] relationship_urls = {} for relationship in relationships: relationship_urls[relationship] = relationships[relationship]["links"]["related"]["href"] assert_in("registered_by", relationships) registered_by = relationships["registered_by"]["links"]["related"]["href"] assert_equal(urlparse(registered_by).path, "/{}users/{}/".format(API_BASE, user._id)) assert_in("registered_from", relationships) registered_from = relationships["registered_from"]["links"]["related"]["href"] assert_equal(urlparse(registered_from).path, "/{}nodes/{}/".format(API_BASE, reg.registered_from._id)) for relationship in relationship_urls: if relationship in should_not_relate_to_registrations: assert_not_in("/{}registrations/".format(API_BASE), relationship_urls[relationship]) else: assert_in( "/{}registrations/".format(API_BASE), relationship_urls[relationship], "For key {}".format(relationship), )
def test_node_serialization(self): parent = ProjectFactory(creator=self.user) node = NodeFactory(creator=self.user, parent=parent) req = make_drf_request() result = NodeSerializer(node, context={'request': req}).data data = result['data'] assert_equal(data['id'], node._id) assert_equal(data['type'], 'nodes') # Attributes attributes = data['attributes'] assert_equal(attributes['title'], node.title) assert_equal(attributes['description'], node.description) assert_equal(attributes['public'], node.is_public) assert_equal(attributes['tags'], [str(each) for each in node.tags]) assert_equal(attributes['category'], node.category) assert_equal(attributes['registration'], node.is_registration) assert_equal(attributes['fork'], node.is_fork) assert_equal(attributes['collection'], node.is_collection) # Relationships relationships = data['relationships'] assert_in('children', relationships) assert_in('contributors', relationships) assert_in('files', relationships) assert_in('parent', relationships) assert_in('affiliated_institutions', relationships) parent_link = relationships['parent']['links']['related']['href'] assert_equal( urlparse(parent_link).path, '/{}nodes/{}/'.format(API_BASE, parent._id)) assert_in('registrations', relationships) # Not a fork, so forked_from is removed entirely assert_not_in('forked_from', relationships)
def test_null_links_are_omitted(self): req = make_drf_request() rep = FakeSerializer(FakeModel, context={'request': req}).data['data'] assert_not_in('null_field', rep['links']) assert_in('valued_field', rep['links']) assert_not_in('null_link_field', rep['relationships']) assert_in('valued_link_field', rep['relationships'])
def test_self_and_related_fields(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] relationship_field = data['relationships']['self_and_related_field']['links'] assert_in('/v2/nodes/{}/contributors/'.format(node._id), relationship_field['self']['href']) assert_in('/v2/nodes/{}/'.format(node._id), relationship_field['related']['href'])
def test_fork_serialization(self): node = NodeFactory(creator=self.user) fork = node.fork_node(auth=Auth(user=node.creator)) result = NodeSerializer(fork, context={"request": make_drf_request()}).data data = result["data"] # Relationships relationships = data["relationships"] forked_from = relationships["forked_from"]["links"]["related"]["href"] assert_equal(urlparse(forked_from).path, "/{}nodes/{}/".format(API_BASE, node._id))
def test_serializing_meta(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] meta = data['relationships']['parent_with_meta']['links']['related']['meta'] assert_not_in('count', meta) assert_in('extra', meta) assert_equal(meta['extra'], 'foo')
def test_fork_serialization(self): node = NodeFactory(creator=self.user) fork = node.fork_node(auth=Auth(user=node.creator)) result = NodeSerializer(fork, context={'request': make_drf_request()}).data data = result['data'] # Relationships relationships = data['relationships'] forked_from = relationships['forked_from']['links']['related']['href'] assert_equal( urlparse(forked_from).path, '/{}nodes/{}/'.format(API_BASE, node._id) )
def test_field_with_callable_related_attrs(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] assert_not_in('registered_from', data['relationships']) registration = factories.RegistrationFactory(project=node) data = self.BasicNodeSerializer(registration, context={'request': req}).data['data'] field = data['relationships']['registered_from']['links'] assert_in('/v2/nodes/{}/'.format(node._id), field['related']['href']) registration_registration = factories.RegistrationFactory(project=registration) data = self.BasicNodeSerializer(registration_registration, context={'request': req}).data['data'] field = data['relationships']['registered_from']['links'] assert_in('/v2/registrations/{}/'.format(registration._id), field['related']['href'])
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={'request': req}).data data = result['data'] assert_equal(data['id'], reg._id) assert_equal(data['type'], 'registrations') should_not_relate_to_registrations = [ 'registered_from', 'registered_by', ] # Attributes attributes = data['attributes'] assert_datetime_equal(parse_date(attributes['date_registered']), reg.registered_date) assert_equal(attributes['retracted'], reg.is_retracted) # Relationships relationships = data['relationships'] relationship_urls = {} for relationship in relationships: relationship_urls[relationship] = relationships[relationship][ 'links']['related']['href'] assert_in('registered_by', relationships) registered_by = relationships['registered_by']['links']['related'][ 'href'] assert_equal( urlparse(registered_by).path, '/{}users/{}/'.format(API_BASE, user._id)) assert_in('registered_from', relationships) registered_from = relationships['registered_from']['links']['related'][ 'href'] assert_equal( urlparse(registered_from).path, '/{}nodes/{}/'.format(API_BASE, reg.registered_from._id)) for relationship in relationship_urls: if relationship in should_not_relate_to_registrations: assert_not_in('/{}registrations/'.format(API_BASE), relationship_urls[relationship]) else: assert_in('/{}registrations/'.format(API_BASE), relationship_urls[relationship], 'For key {}'.format(relationship))
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={'request': req}).data data = result['data'] assert_equal(data['id'], reg._id) assert_equal(data['type'], 'registrations') should_not_relate_to_registrations = [ 'registered_from', 'registered_by', 'registration_schema' ] # Attributes attributes = data['attributes'] assert_datetime_equal( parse_date(attributes['date_registered']), reg.registered_date ) assert_equal(attributes['withdrawn'], reg.is_retracted) # Relationships relationships = data['relationships'] relationship_urls = {} for relationship in relationships: relationship_urls[relationship]=relationships[relationship]['links']['related']['href'] assert_in('registered_by', relationships) registered_by = relationships['registered_by']['links']['related']['href'] assert_equal( urlparse(registered_by).path, '/{}users/{}/'.format(API_BASE, user._id) ) assert_in('registered_from', relationships) registered_from = relationships['registered_from']['links']['related']['href'] assert_equal( urlparse(registered_from).path, '/{}nodes/{}/'.format(API_BASE, reg.registered_from._id) ) for relationship in relationship_urls: if relationship in should_not_relate_to_registrations: assert_not_in('/{}registrations/'.format(API_BASE), relationship_urls[relationship]) else: assert_in('/{}registrations/'.format(API_BASE), relationship_urls[relationship], 'For key {}'.format(relationship))
def test_serialization(self): user = UserFactory() req = make_drf_request() reg = RegistrationFactory(creator=user) result = RegistrationSerializer(reg, context={"request": req}).data data = result["data"] assert_equal(data["id"], reg._id) assert_equal(data["type"], "registrations") # Attributes attributes = data["attributes"] assert_datetime_equal(parse_date(attributes["date_registered"]), reg.registered_date) assert_equal(attributes["retracted"], reg.is_retracted) # Relationships relationships = data["relationships"] assert_in("registered_by", relationships) registered_by = relationships["registered_by"]["links"]["related"]["href"] assert_equal(urlparse(registered_by).path, "/{}users/{}/".format(API_BASE, user._id)) assert_in("registered_from", relationships) registered_from = relationships["registered_from"]["links"]["related"]["href"] assert_equal(urlparse(registered_from).path, "/{}nodes/{}/".format(API_BASE, reg.registered_from._id))
def test_collection_serialization(self): collection = FolderFactory(creator=self.user) req = make_drf_request() result = CollectionSerializer(collection, context={'request': req}).data data = result['data'] assert_equal(data['id'], collection._id) assert_equal(data['type'], 'collections') # Attributes attributes = data['attributes'] assert_equal(attributes['title'], collection.title) assert_equal(attributes['date_created'], collection.date_created.isoformat()) assert_equal(attributes['date_modified'], collection.date_modified.isoformat()) # Relationships relationships = data['relationships'] assert_in('node_links', relationships) # Bunch of stuff in Nodes that should not be in Collections assert_not_in('contributors', relationships) assert_not_in('files', relationships) assert_not_in('parent', relationships) assert_not_in('registrations', relationships) assert_not_in('forked_from', relationships)
def test_search_serializer_mixed_model_user(self): req = make_drf_request() result = SearchSerializer(self.user, context={'request': req}).data assert_equal(result['data']['type'], 'users')