def test_it_does_not_break(self, cells, method, parameter): self.post['cells'] = cells self.post['method'] = method self.post['parameter'] = parameter response = self.client.post( reverse('honeycomb_api'), make_json(self.post), content_type='application/octet-stream' ) try: self.assertEqual(response.status_code, 200) except AssertionError: self.assertEqual(response.status_code, 400) content = read_json(response.content) self.assertEqual(len(content), 1) self.assertIn('error', content) else: content = read_json(response.content) self.assertEqual(len(content), 1) self.assertIn('cells', content) self.assertEqual(len(content['cells']), len(cells)) for key, cell in enumerate(content['cells']): self.assertEqual(len(cell), 3) self.assertEqual(cell[0], cells[key][0]) self.assertEqual(cell[1], cells[key][1]) self.assertGreaterEqual(cell[2], -1) self.assertLessEqual(cell[2], 1)
def __init__(self, root='/home/hzh/data', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) # self.download_dataset(self.dataset_dir, self.dataset_url) self.zip_path = osp.join(self.dataset_dir, 'CUHK01.zip') self.campus_dir = osp.join(self.dataset_dir, 'campus') self.split_path = osp.join(self.dataset_dir, 'splits.json') self.extract_file() required_files = [ self.dataset_dir, self.campus_dir ] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits) - 1)) split = splits[split_id] train = split['train'] query = split['query'] gallery = split['gallery'] train = [tuple(item) for item in train] query = [tuple(item) for item in query] gallery = [tuple(item) for item in gallery] super(CUHK01, self).__init__(train, query, gallery, **kwargs)
def test_empty_upload(self): response = self.client.post(reverse('file_api')) self.assertEqual(response.status_code, 400) d = read_json(response.content) self.assertEqual(len(d), 1) self.assertIn('error', d)
def test_get(self): response = self.client.get(reverse('file_api')) self.assertEqual(response.status_code, 200) d = read_json(response.content) self.assertEqual(len(d), 3) self.assertEqual(d['name'], 'LanguageGraph') self.assertEqual(len(d['nodes']), 44) self.assertEqual(len(d['edges']), 87 + 17)
def test_bad_upload(self): with open('app/fixtures/globes.json', 'r') as f: response = self.client.post(reverse('file_api'), {'file': f}) self.assertEqual(response.status_code, 400) d = read_json(response.content) self.assertEqual(len(d), 1) self.assertIn('error', d)
def test_good_upload(self): with open('app/fixtures/sample.dot', 'r') as f: response = self.client.post(reverse('file_api'), {'file': f}) self.assertEqual(response.status_code, 200) d = read_json(response.content) self.assertEqual(len(d), 3) self.assertEqual(d['name'], 'LanguageGraph') self.assertEqual(len(d['nodes']), 44) self.assertEqual(len(d['edges']), 87 + 17)
def validate_post(self, request_body): """ Returns validated POST or raises ValueError. """ post = read_json(request_body) try: assert len(post) == 5 except AssertionError: raise ValueError('You cannot pass!') try: assert 'id' in post assert type(post['id']) is str assert len(post['id']) > 0 assert len(post['id']) < 200 except AssertionError: raise ValueError('Invalid id.') try: assert 'latitude' in post post['latitude'] = float(post['latitude']) except (AssertionError, TypeError): raise ValueError('Invalid latitude.') try: assert 'longitude' in post post['longitude'] = float(post['longitude']) except (AssertionError, TypeError): raise ValueError('Invalid longitude.') try: assert 'method' in post assert type(post['method']) is str assert post['method'] in ( 'circle', 'neighbourhood', ) except AssertionError: raise ValueError('Invalid method.') try: assert 'parameter' in post assert type(post['parameter']) is int assert post['parameter'] > 0 except AssertionError: raise ValueError('Invalid parameter.') return post
def test_it_does_not_break(self, latitude, longitude, method, parameter): self.post['latitude'] = latitude self.post['longitude'] = longitude self.post['method'] = method self.post['parameter'] = parameter response = self.client.post(reverse('point_api'), make_json(self.post), content_type='application/octet-stream') try: self.assertEqual(response.status_code, 200) except AssertionError: self.assertEqual(response.status_code, 400) content = read_json(response.content) self.assertEqual(len(content), 1) self.assertIn('error', content)
def test_good_upload(self): with open('app/fixtures/berg.tsv', 'r') as f: response = self.client.post(reverse('file_api'), {'file': f}) self.assertEqual(response.status_code, 200) d = read_json(response.content) self.assertEqual(len(d), 2) self.assertIn('id', d) self.assertGreater(len(d['id']), 0) self.assertIn('name', d) self.assertEqual(d['name'], 'berg.tsv') matrix = WordMatrix() matrix.load(d['id']) self.assertEqual(len(matrix.d), 2346)
def validate_post(self, request_body): """ Returns validated POST or raises ValueError. """ post = read_json(request_body) try: assert len(post) == 4 except AssertionError: raise ValueError('You cannot pass!') try: assert 'id' in post assert type(post['id']) is str assert len(post['id']) > 0 assert len(post['id']) < 200 except AssertionError: raise ValueError('Invalid id.') try: assert 'cells' in post assert type(post['cells']) is list assert len(post['cells']) <= 10000 for cell in post['cells']: assert type(cell) is list assert len(cell) == 2 except AssertionError: raise ValueError('Invalid cells.') try: assert 'method' in post assert type(post['method']) is str assert post['method'] in ('circle', 'neighbourhood',) except AssertionError: raise ValueError('Invalid method.') try: assert 'parameter' in post assert type(post['parameter']) is int assert post['parameter'] > 0 except AssertionError: raise ValueError('Invalid parameter.') return post
def test_good_circle(self): response = self.client.post( reverse('honeycomb_api'), make_json(self.post), content_type='application/octet-stream' ) self.assertEqual(response.status_code, 200) content = read_json(response.content) self.assertEqual(len(content), 1) self.assertIn('cells', content) self.assertEqual(len(content['cells']), len(self.post['cells'])) for key, cell in enumerate(content['cells']): self.assertEqual(len(cell), 3) self.assertEqual(cell[0], self.post['cells'][key][0]) self.assertEqual(cell[1], self.post['cells'][key][1]) self.assertGreaterEqual(cell[2], -1) self.assertLessEqual(cell[2], 1)
def test_good_neighbourhood(self): self.post['method'] = 'neighbourhood' self.post['parameter'] = 4 response = self.client.post(reverse('point_api'), make_json(self.post), content_type='application/octet-stream') self.assertEqual(response.status_code, 200) content = read_json(response.content) self.assertEqual(len(content), 3) self.assertIn('origin', content) self.assertIn('d', content) self.assertIn('p', content) self.assertEqual(content['origin'], 'ab') self.assertEqual(len(content['d']), 4) self.assertIn('os', content['d']) self.assertIn('ka', content['d']) self.assertIn('ady', content['d']) self.assertIn('ddo', content['d'])
def test_good_circle(self): response = self.client.post(reverse('point_api'), make_json(self.post), content_type='application/octet-stream') self.assertEqual(response.status_code, 200) content = read_json(response.content) self.assertEqual(len(content), 2) self.assertIn('d', content) self.assertIn('p', content) lang = ('ab', 'os', 'ka', 'ady', 'ddo', 'ce', 'hy', 'dar') for i in lang: for j in lang: if i == j: continue key = [i, j] key.sort() key = key[0] + ',' + key[1] self.assertIn(key, content['d']) self.assertEqual(len(content['d']), 8 * 7 / 2) self.assertLessEqual(content['p'], 1) self.assertGreaterEqual(content['p'], -1)