def test_json(self): dataset = Dataset(filename=self.filename, id=self.id, client_id=self.client_id) self.assertDictEqual(dataset.json(), { 'id': self.id, 'client': self.client_id, 'filename': self.filename })
def test_get_dataset_successfully(self): dataset = Dataset(client_id=1, filename='foo.txt') dataset.set_userdata({'key': 'value'}) db.session.add(dataset) db.session.commit() response = self.client.get('/datasets/1') assert_success(response) self.assertEqual(response.get_json(), dataset.details_json())
def test_details_json(self): dataset = Dataset(filename=self.filename, id=self.id, client_id=self.client_id) dataset.set_userdata({'key': 'value', 'key2': 'value2'}) self.assertDictEqual( dataset.details_json(), { 'id': self.id, 'client': self.client_id, 'filename': self.filename, 'key2': 'value2', 'key': 'value' })
def test_patch_adding_userdefined_metadata(self): key = 'some_key' value = 'some_value' dataset = Dataset(client_id=1, filename='foo.txt') db.session.add(dataset) db.session.commit() response = self.client.patch('/datasets/' + str(dataset.id), data=json.dumps({key: value}), content_type='application/json') assert_status_code_equal(response, status.HTTP_204_NO_CONTENT) self.assertEqual(dataset.get_value(key), value)
def test_details_json(self): client = Client(name=self.name, ip_address=self.ip_address, id=self.id) client.datasets = [ Dataset(filename='foo.bar', id=1), Dataset(filename='test', id=3) ] self.assertDictEqual( client.details_json(), { 'name': self.name, 'ip_address': self.ip_address, 'id': self.id, 'datasets': [1, 3] })
def test_client_has_dataset_returns_true(self): client = Client(name='Client', ip_address=IP_ADDRESS) dataset = Dataset(filename=FILENAME) client.datasets = [dataset] result = DbHelper.client_has_dataset(client, dataset) self.assertTrue(result)
def test_delete_dataset_successfully(self): db.session.add(Dataset(client_id=1, filename='foo.txt')) db.session.commit() response = self.client.delete('/datasets/1') assert_status_code_equal(response, status.HTTP_204_NO_CONTENT) self.assertEqual(Dataset.query.all(), [])
def test_dataset_not_created_because_already_exists(self): filename = 'test.txt' client = Client(name='Client', ip_address='127.0.0.1') db.session.add(client) client.datasets = [Dataset(filename=filename)] db.session.commit() response = self.client.post('/datasets', data=json.dumps({'client': client.id, 'filename': filename}), content_type='application/json') assert_status_code_equal(response, status.HTTP_409_CONFLICT)
def test_patch_dataset_successfully(self): new_filename = 'bar.txt' dataset = Dataset(client_id=1, filename='foo.txt') db.session.add(dataset) db.session.commit() response = self.client.patch('/datasets/' + str(dataset.id), data=json.dumps( {'filename': new_filename}), content_type='application/json') assert_status_code_equal(response, status.HTTP_204_NO_CONTENT) self.assertEqual(dataset.filename, new_filename)
def test_cannot_patch_changing_owner_because_not_exists(self): client = Client(name='Client1', ip_address='127.0.0.1') dataset = Dataset(filename='test.txt') client.datasets = [dataset] db.session.add(client) db.session.commit() response = self.client.patch('/datasets/' + str(dataset.id), data=json.dumps({'client': 42}), content_type='application/json') assert_status_code_equal(response, status.HTTP_400_BAD_REQUEST) self.assertEqual(client.datasets, [dataset])
def test_patch_changing_owner_of_dataset(self): client1 = Client(name='Client1', ip_address='127.0.0.1') client2 = Client(name='Client2', ip_address='127.0.0.2') dataset = Dataset(filename='test.txt') client1.datasets = [dataset] db.session.add_all([client1, client2]) db.session.commit() response = self.client.patch('/datasets/' + str(dataset.id), data=json.dumps({'client': client2.id}), content_type='application/json') assert_status_code_equal(response, status.HTTP_204_NO_CONTENT) self.assertEqual(client1.datasets, []) self.assertEqual(client2.datasets, [dataset]) self.assertEqual(dataset.client, client2)
def test_get_nonempty_datasets_list(self): client = Client(name='Client', ip_address='127.0.0.1') first_dataset = Dataset(filename='test.mp3') second_dataset = Dataset(filename='movie.mp4') client.datasets = [first_dataset, second_dataset] db.session.add(client) db.session.commit() response = self.client.get('/datasets') response_json = response.get_json() assert_success(response) self.assertEqual(len(response_json), 2) self.assertDictEqual(response_json[0], first_dataset.json()) self.assertDictEqual(response_json[1], second_dataset.json())
def _create_dataset(self, json_data): dataset = Dataset(filename=json_data['filename']) self._insert_metadata(json_data, dataset) return dataset