def test_limit(self): data = { 'query': { 'limit': [0, 1] } } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) # assert returns 1/3 matched posts self.assertEqual(len(data['posts']), 1) data = { 'query': { 'limit': [0, 2] } } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) # assert returns 2/3 matched posts self.assertEqual(len(data['posts']), 2)
def test_order_by(self): data = { 'query': { 'order_by': '-title' } } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) posts = data['posts'] self.assertEqual(posts[0]['title'], 'c') self.assertEqual(posts[1]['title'], 'b') self.assertEqual(posts[2]['title'], 'a') data = { 'query': { 'order_by': 'title' } } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) posts = data['posts'] self.assertEqual(posts[0]['title'], 'a') self.assertEqual(posts[1]['title'], 'b') self.assertEqual(posts[2]['title'], 'c')
def handle_response(self, response_body): result = json.loads(response_body) # errors? if type(result) == dict: if result.has_key('errors'): for error in json.loads(response_body)['errors']: if error['description'].startswith('has already been taken'): raise SpringnoteError.HasAlreadyBeenTaken elif type(result) == list: if len(result) == 0: #찾을 수 없음 return None for r in result: if r.has_key('error') and r['error']['error_type'].startswith("InvalidOauthRequest"): raise SpringnoteError.InvalidOauthRequest elif r.has_key('error') and r['error']['error_type'].startswith("NotFound"): raise SpringnoteError.PageNotFound # multi ? if type(result) == list: pages = [] for r in result: pages.append(Page.from_json(r)) return pages # generate Page instance from json return Page.from_jsons(response_body)
def test_all(self): uri = reverse('apis:posts') res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) self.assertEqual(len(data.keys()), 1) self.assertEqual(data.keys()[0], 'posts') self.assertEqual(len(data['posts']), 3)
def test_has_many_fields_are_added_to_json_outputs(self): user = User.objects.get(pk=1) uri = reverse('apis:user', kwargs={ 'pk': user.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) user_data = data['user'] self.assertEqual(user_data['id'], user.pk) user = User.objects.get(pk=2) uri = reverse('apis:user', kwargs={ 'pk': user.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) user_data = data['user'] self.assertEqual(user_data['id'], user.pk)
def test_foreign(self): post = Post.objects.all()[0] uri = reverse('apis:post', kwargs={ 'pk': post.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) post_data = data['post'] self.assertEqual(post_data['id'], post.pk) self.assertEqual(post_data['user_id'], post.user.pk)
def test_image(self): post = Post.objects.get(pk=1) uri = reverse('apis:post', kwargs={ 'pk': post.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) post_data = data['post'] self.assertEqual(post_data['id'], post.pk) self.assertEqual(post_data['image'], '/media/posts/image.png') post = Post.objects.get(pk=2) uri = reverse('apis:post', kwargs={ 'pk': post.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) post_data = data['post'] self.assertEqual(post_data['id'], post.pk) self.assertEqual(post_data['image'], '')
def update_from_jsons(self,str): """ update parsing json string """ data = json.loads(str) if type(data) == list: data = data[0] data = data["page"] # type conversion for attr_name, attr_type in self.typeset.iteritems(): if data.has_key(attr_name): data[attr_name] = attr_type( data[attr_name] ) for key,value in data.iteritems(): setattr(self, key, value) return data
def test_one(self): post = Post.objects.all()[0] uri = reverse('apis:post', kwargs={ 'pk': post.pk }) res = self.client.get(uri) self.assertEqual(res.status_code, 200) data = json.loads(res.content) self.assertEqual(len(data.keys()), 1) self.assertEqual(data.keys()[0], 'post') post_data = data['post'] self.assertEqual(post_data['id'], post.pk) self.assertEqual(post_data['title'], post.title) self.assertEqual(post_data['content'], post.content) self.assertEqual(post_data['user_id'], post.user.pk)
def test_create(self): data = {'post': { 'title': 'New Book', 'content': '', 'user_id': '1' }} uri = reverse('apis:posts') res = self.client.post(uri, json.dumps(data), 'application/json') self.assertEqual(res.status_code, 200) data = json.loads(res.content) post = Post.objects.latest('pk') self.assertEqual(post.pk, data['post']['id']) self.assertEqual(post.title, data['post']['title']) self.assertEqual(post.content, data['post']['content']) self.assertEqual(post.user.pk, data['post']['user_id'])
def test_exclude(self): data = { 'query': { 'exclude': { 'user__pk': 1 } } } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) # assert returns 1/3 matched posts self.assertEqual(len(data['posts']), 1)
def add_backend(event, context): try: params = add_backend_schema_request.validate(json.loads(event['body'])) except json.JSONDecodeError as e: return error_response(400, e, error_type='parse_error') except SchemaError as e: return error_response(400, e, error_type='validation_error') params.update({ 'is_healthy': False, 'when_added': datetime.utcnow().isoformat() }) table = get_table() try: response = table.put_item(Item=params) except Exception as e: return error_response(500, e) return {'statusCode': 201, 'body': json.dumps(params)}
def test_update(self): post = Post.objects.all()[0] data = {'post': { 'title': 'New Book', 'content': '', 'user_id': '1' }} uri = reverse('apis:post', kwargs={ 'pk': post.pk }) res = self.client.put(uri, json.dumps(data), 'application/json') self.assertEqual(res.status_code, 200) data = json.loads(res.content) post = Post.objects.get(pk=post.pk) self.assertEqual(post.pk, data['post']['id']) self.assertEqual(post.title, data['post']['title']) self.assertEqual(post.content, data['post']['content']) self.assertEqual(post.user.pk, data['post']['user_id'])
def test_is_sql(self): data = { 'query': 'SELECT * WHERE ...' } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 400) data = { 'query': 'WHERE user__pk' } res = self.client.post( self.uri, json.dumps(data), 'application/json' ) self.assertEqual(res.status_code, 200) data = json.loads(res.content) # assert returns 1/3 matched posts self.assertEqual(len(data['posts']), 1)
def from_jsons(body): """ generate a Page instance from json string """ data = json.loads(body) return Page.update_from_dict(data)
def unpack(self, fp): return loads(fp.read())
def loads(self, s): l = loads(s) return unpack(l)
def load_index_file(self): if os.path.exists(DEFAULT_INDEX_FILE): f = open(DEFAULT_INDEX_FILE) self.ihash = json.loads(f.read()) else: self.ihash = {}