def setUp(self): self.json_dict = { u'name': u'Sally', u'age': 144, u'friends': [ { u'name': u'Sasha', u'age': 121 }, { u'name': u'Sandy', u'age': 100 }, ], u'education': { u'Artsy_University': { u'degree': u'BA in Art', u'years_spent': 3, }, u'High_School': { u'degree': u'Diploma', u'years_spent': 4, } }, } self.dn = DotNotator(self.json_dict)
def test_json_dict_param(self): json_dn = DotNotator(self.json_dict) assert_true(isinstance(json_dn, DotNotator)) assert_true(json_dn) # assert json_dn is not empty assert_equal(json_dn[u'name'], u'Sally') assert_equal(json_dn[u'age'], 144) assert_equal(json_dn.name, u'Sally') assert_equal(json_dn.age, 144)
def get_root(self): """ :return: a DotNotator object of the api root as designated by self.root_url """ response = get_response_or_exception('get', self.root_url) response_json = response.json() return DotNotator(response_json)
def get_user(self, user_id): """ :param user_id: 5-character user id :return: the user identified by user_id """ url = '{}users/{}/'.format(self.root_url, user_id) response = get_response_or_exception('get', url, auth=self.auth) data = response.json()[u'data'] return DotNotator(data)
def get_node(self, node_id=''): """ :param node_id: 5-character id for a node that can be viewed by this session's auth :return: the node identified by node_id """ url = '{}nodes/{}/'.format(self.root_url, node_id) response = get_response_or_exception('get', url, auth=self.auth) data = response.json()[u'data'] return DotNotator(data)
def edit_node(self, node_id, **kwargs): """ :param node_id: 5-character id for a node that can be edited by this session's auth :param kwargs: e.g. title='My Title', description='This is my new description', category='data' :return: DotNotator version of edited node """ # e.g. kwargs: title='', description='', category='' params = {} for key, value in kwargs.items(): params[key] = value edited_node = get_response_or_exception('patch', '{}nodes/{}/'.format( self.root_url, node_id), json=params, auth=self.auth) edited_node_json = edited_node.json()[u'data'] return DotNotator(edited_node_json)
def create_node(self, title, description="", category=""): """ :param title: required, string :param description: optional, string :param category: optional, choice of '', 'project', 'hypothesis', 'methods and measures', 'procedure', 'instrumentation', 'data', 'analysis', 'communication', 'other' :return: DotNotator version of created node """ # TODO once functionality exists to create public nodes w/ API, # add 'public' bool to create_node parameters, and add the # functionality in this method. params = { 'title': title, 'description': description, 'category': category } node = get_response_or_exception('post', '{}nodes/'.format(self.root_url), json=params, auth=self.auth) node_json = node.json()[u'data'] return DotNotator(node_json)
def test_empty_dict_param(self): empty_dict = {} empty_dn = DotNotator(empty_dict) assert_true(isinstance(empty_dn, DotNotator)) assert_false(empty_dn) # assert empty_dn is empty
def test_wrong_type_param(self): not_dict = 4 with assert_raises(TypeError): DotNotator(not_dict)
def test_no_param(self): no_param_dn = DotNotator() # no param --> default: dictionary = None assert_true(isinstance(no_param_dn, DotNotator)) assert_false(no_param_dn) # assert no_param_dn is empty