def _test_view_with_get_dataset(self, view_name): """Check that a view that uses datasets.get_dataset to retrieve a dataset""" # no such dataset, 404 resp = self.client.get(url_for(view_name, dataset_id=self.test_uuid)) self.assert404(resp) # public dataset + not logged in, OK dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get(url_for(view_name, dataset_id=dataset_id)) self.assert200(resp) self.temporary_login(self.test_user_id) # public dataset + logged in, ok resp = self.client.get(url_for(view_name, dataset_id=dataset_id)) self.assert200(resp) # private dataset + author, ok self.test_data["public"] = False private_dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get(url_for(view_name, dataset_id=private_dataset_id)) self.assert200(resp) # private dataset, not author, 404 another_user_id = user.create("another_tester") user.agree_to_gdpr("another_tester") self.temporary_login(another_user_id) resp = self.client.get(url_for(view_name, dataset_id=private_dataset_id)) self.assert404(resp)
def test_create_from_dict_malformed(self): bad_dict = copy.deepcopy(self.test_data) bad_dict["classes"][0]["name"] = None with self.assertRaises(dataset_validator.ValidationException): dataset.create_from_dict(bad_dict, author_id=self.test_user_id) bad_dict["classes"][0]["name"] = "" with self.assertRaises(dataset_validator.ValidationException): dataset.create_from_dict(bad_dict, author_id=self.test_user_id)
def test_delete_recordings_other_class(self): """ If there are two datasets with the same class names, the correct one is deleted. """ ds1id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) ds2id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) class_name = "Class #1", recordings = ["19e698e7-71df-48a9-930e-d4b1a2026c82"] dataset.delete_recordings(ds2id, class_name, recordings) # Dataset 1 has both initial recordings, but it's been removed from dataset 2 ds1 = dataset.get(ds1id) ds2 = dataset.get(ds2id) self.assertEqual(2, len(ds1["classes"][0]["recordings"])) self.assertEqual(1, len(ds2["classes"][0]["recordings"]))
def test_edit_service(self): dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) # Trying to edit without login resp = self.client.post( url_for("datasets.edit_service", dataset_id=dataset_id), headers={"Content-Type": "application/json"}, data=json.dumps(self.test_data), ) self.assert401(resp) self.assertTrue(resp.json["message"].startswith("The server could not verify that you are authorized")) # Editing using another user another_user_id = user.create("another_tester") user.agree_to_gdpr("another_tester") self.temporary_login(another_user_id) resp = self.client.post( url_for("datasets.edit_service", dataset_id=dataset_id), headers={"Content-Type": "application/json"}, data=json.dumps(self.test_data), ) self.assert401(resp) # Editing properly self.temporary_login(self.test_user_id) resp = self.client.post( url_for("datasets.edit_service", dataset_id=dataset_id), headers={"Content-Type": "application/json"}, data=json.dumps(self.test_data), ) self.assert200(resp)
def test_delete(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) self.assertIsNotNone(dataset.get(id)) dataset.delete(id) with self.assertRaises(db.exceptions.NoDataFoundException): dataset.get(id)
def test_create_from_dict(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) ds = dataset.get(id) self.assertIsNotNone(ds) self.assertEqual(len(ds["classes"][0]["recordings"]), 2) self.assertEqual(len(ds["classes"][1]["recordings"]), 3)
def test_delete(self): # Should redirect to login page even if trying to delete dataset that # doesn't exist. resp = self.client.get(url_for("datasets.delete", dataset_id=self.test_uuid)) self.assertStatus(resp, 302) dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) # Trying to delete without login resp = self.client.get(url_for("datasets.delete", dataset_id=dataset_id)) self.assertStatus(resp, 302) resp = self.client.post(url_for("datasets.delete", dataset_id=dataset_id)) self.assertStatus(resp, 302) self.assertTrue(len(dataset.get_by_user_id(self.test_user_id)) == 1) # Deleting using another user another_user_id = user.create("another_tester") self.temporary_login(another_user_id) resp = self.client.get(url_for("datasets.delete", dataset_id=dataset_id)) self.assert401(resp) resp = self.client.post(url_for("datasets.delete", dataset_id=dataset_id)) self.assert401(resp) self.assertTrue(len(dataset.get_by_user_id(self.test_user_id)) == 1) # Editing properly self.temporary_login(self.test_user_id) resp = self.client.get(url_for("datasets.delete", dataset_id=dataset_id)) self.assert200(resp) resp = self.client.post(url_for("datasets.delete", dataset_id=dataset_id)) self.assertRedirects(resp, url_for("user.profile", musicbrainz_id=self.test_user_mb_name)) self.assertTrue(len(dataset.get_by_user_id(self.test_user_id)) == 0)
def test_view_json(self): resp = self.client.get(url_for("datasets.view_json", id=self.test_uuid)) self.assert404(resp) dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get(url_for("datasets.view_json", id=dataset_id)) self.assert200(resp)
def setUp(self): super(DatasetEvalTestCase, self).setUp() self.test_user_mb_name = "tester" self.test_user_id = user.create(self.test_user_mb_name) self.test_uuid = "123e4567-e89b-12d3-a456-426655440000" self.test_data = { "name": "Test", "description": "", "classes": [ { "name": "Class #1", "description": "This is a description of class #1!", "recordings": [ "0dad432b-16cc-4bf0-8961-fd31d124b01b", "19e698e7-71df-48a9-930e-d4b1a2026c82", ] }, { "name": "Class #2", "description": "", "recordings": [ "fd528ddb-411c-47bc-a383-1f8a222ed213", "96888f9e-c268-4db2-bc13-e29f8b317c20", "ed94c67d-bea8-4741-a3a6-593f20a22eb6", ] }, ], "public": True, } self.test_dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) self.conn = db.engine.connect()
def test_get_pending_jobs_for_user_other_user(self): """ Check that a remote eval job for another user doesn't show """ another_user_id = user.create("another user") another_dataset_id = dataset.create_from_dict(self.test_data, author_id=another_user_id) job_id = dataset_eval._create_job(self.conn, another_dataset_id, True, dataset_eval.EVAL_REMOTE, None) response = dataset_eval.get_remote_pending_jobs_for_user(self.test_user_id) self.assertEqual(response, [])
def test_get_by_user_id(self): dataset.create_from_dict(self.test_data, author_id=self.test_user_id) datasets = dataset.get_by_user_id(self.test_user_id) self.assertEqual(len(datasets), 1) private = copy.deepcopy(self.test_data) private["name"] = "Private Dataset" private["public"] = False dataset.create_from_dict(private, author_id=self.test_user_id) datasets = dataset.get_by_user_id(self.test_user_id) # Not returning private datasets by default. self.assertEqual(len(datasets), 1) self.assertNotEqual(datasets[0]["name"], private["name"]) datasets = dataset.get_by_user_id(self.test_user_id, public_only=False) self.assertEqual(len(datasets), 2)
def test_check_recording_in_dataset(self): dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) recording_mbid = "0dad432b-16cc-4bf0-8961-fd31d124b01b" resp = dataset.check_recording_in_dataset(dataset_id, recording_mbid) self.assertTrue(resp) recording_mbid = "770cc467-8dde-4d22-bc4c-a42f91e7515e" resp = dataset.check_recording_in_dataset(dataset_id, recording_mbid) self.assertFalse(resp)
def test_update_class_no_class(self): """ Return error if the class doesn't exist """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #1", "new_name": "Different class", "description": "different desc" } with self.assertRaises(db.exceptions.NoDataFoundException): dataset.update_class(dsid, "Class #99", test_data)
def test_update_malformed(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) bad_dataset = copy.deepcopy(self.test_data) bad_dataset["classes"][0]["name"] = None self.assertRaises(jsonschema.ValidationError, dataset.update, dataset_id=id, dictionary=bad_dataset, author_id=self.test_user_id) bad_dataset["classes"][0]["name"] = "" self.assertRaises(jsonschema.ValidationError, dataset.update, dataset_id=id, dictionary=bad_dataset, author_id=self.test_user_id)
def test_update_malformed(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) bad_dataset = copy.deepcopy(self.test_data) bad_dataset["classes"][0]["name"] = None with self.assertRaises(dataset_validator.ValidationException): dataset.update(dataset_id=id, dictionary=bad_dataset, author_id=self.test_user_id) bad_dataset["classes"][0]["name"] = "" with self.assertRaises(dataset_validator.ValidationException): dataset.update(dataset_id=id, dictionary=bad_dataset, author_id=self.test_user_id)
def test_view_json(self): resp = self.client.get(url_for("datasets.view_json", id=self.test_uuid)) self.assert404(resp) dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get(url_for("datasets.view_json", id=dataset_id)) self.assert200(resp) dataset_eval.evaluate_dataset(dataset_id, False, dataset_eval.EVAL_LOCAL) self.temporary_login(self.test_user_id)
def test_create_from_dict_duplicates(self): bad_dict = copy.deepcopy(self.test_data) bad_dict["classes"][0]["recordings"] = [ "0dad432b-16cc-4bf0-8961-fd31d124b01b", "19e698e7-71df-48a9-930e-d4b1a2026c82", "19e698e7-71df-48a9-930e-d4b1a2026c82", ] id = dataset.create_from_dict(bad_dict, author_id=self.test_user_id) ds = dataset.get(id) self.assertEqual(len(ds["classes"][0]["recordings"]), 2) self.assertIn("19e698e7-71df-48a9-930e-d4b1a2026c82", ds["classes"][0]["recordings"])
def test_last_edited(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) ds = dataset.get(id) self.assertEqual(ds['created'], ds['last_edited']) with db.engine.begin() as connection: connection.execute("""UPDATE dataset SET last_edited = now() - interval %s where id = %s""", ('1 hour', id)) ds = dataset.get(id) self.assertTrue(ds['created'] > ds['last_edited']) dataset.update(id, self.test_data, author_id=self.test_user_id) ds_updated = dataset.get(id) self.assertTrue(ds_updated['last_edited'] > ds['last_edited'])
def test_delete_nonexistent_class(self): """ Delete a class which doesn't exist in the dataset """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #100" } originaldataset = dataset.get(dsid) dataset.delete_class(dsid, test_data) updateddataset = dataset.get(dsid) self.assertEqual(2, len(originaldataset["classes"])) self.assertEqual(2, len(updateddataset["classes"]))
def test_evaluate_location_options(self): self.temporary_login(self.test_user_id) dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get(url_for("datasets.evaluate", dataset_id=dataset_id)) self.assertStatus(resp, 200) evaluate_form = forms.DatasetEvaluationForm() evaluate_form.filter_type.data = forms.DATASET_EVAL_NO_FILTER evaluate_form.evaluation_location.data = forms.DATASET_EVAL_REMOTE evaluate_form.normalize.data = True resp = self.client.post(url_for("datasets.evaluate", dataset_id=dataset_id, form=evaluate_form)) self.assertStatus(resp, 200)
def test_delete_class(self): """ Delete a class from the dataset """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #2" } originaldataset = dataset.get(dsid) dataset.delete_class(dsid, test_data) updateddataset = dataset.get(dsid) self.assertEqual(2, len(originaldataset["classes"])) self.assertEqual(1, len(updateddataset["classes"]))
def test_add_recordings_duplicate(self): """ A recording id which already exists is skipped """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) class_name = "Class #1", recordings = ["0dad432b-16cc-4bf0-8961-fd31d124b01b", "1c085555-3805-428a-982f-e14e0a2b18e6"] dataset.add_recordings(dsid, class_name, recordings) updateddataset = dataset.get(dsid) expected_recordings = ["0dad432b-16cc-4bf0-8961-fd31d124b01b", "19e698e7-71df-48a9-930e-d4b1a2026c82", "1c085555-3805-428a-982f-e14e0a2b18e6"] self.assertEqual(set(expected_recordings), set(updateddataset["classes"][0]["recordings"]))
def test_delete_shapshot(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) snap_id = dataset.create_snapshot(id) snapshots = dataset.get_snapshots_for_dataset(id) self.assertEqual(len(snapshots), 1) with db.engine.connect() as connection: dataset._delete_snapshots_for_dataset(connection, id) snapshots = dataset.get_snapshots_for_dataset(id) self.assertEqual(len(snapshots), 0) with self.assertRaises(db.exceptions.NoDataFoundException): dataset.get_snapshot(snap_id)
def test_update_metadata(self): dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) existing_dataset = dataset.get(dsid) dataset.update_dataset_meta(dsid, {"name": "new name", "description": "new desc", "public": False}) new_dataset = dataset.get(dsid) self.assertNotEqual(existing_dataset["name"], new_dataset["name"]) self.assertEqual(new_dataset["name"], "new name") self.assertNotEqual(existing_dataset["description"], new_dataset["description"]) self.assertEqual(new_dataset["description"], "new desc") self.assertNotEqual(existing_dataset["public"], new_dataset["public"]) self.assertEqual(new_dataset["public"], False)
def test_update(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) updated_dict = copy.deepcopy(self.test_data) updated_dict["classes"][0]["recordings"] = [] # Removing recordings from first class dataset.update( dataset_id=id, dictionary=updated_dict, author_id=self.test_user_id, ) ds = dataset.get(id) # First class shouldn't have any recordings self.assertEqual(len(ds["classes"][0]["recordings"]), 0) self.assertEqual(len(ds["classes"][1]["recordings"]), 3)
def test_create_snapshot(self): id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) snapshots = dataset.get_snapshots_for_dataset(id) self.assertEqual(len(snapshots), 0) snap_id = dataset.create_snapshot(id) snapshots = dataset.get_snapshots_for_dataset(id) self.assertEqual(len(snapshots), 1) self.assertEqual(snapshots[0]["id"], snap_id) snap = dataset.get_snapshot(snap_id) dataset_snap = copy.deepcopy(self.test_data) del dataset_snap["public"] self.assertDictEqual(snap["data"], dataset_snap)
def test_delete_recordings(self): """ Delete recordings from a class """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) originaldataset = dataset.get(dsid) class_name = "Class #1", recordings = ["19e698e7-71df-48a9-930e-d4b1a2026c82"] dataset.delete_recordings(dsid, class_name, recordings) updateddataset = dataset.get(dsid) # Original dataset has the 2 recordings in Class #1, but the updated one only has 1 remaining self.assertEqual(2, len(originaldataset["classes"][0]["recordings"])) expected_recordings = ["0dad432b-16cc-4bf0-8961-fd31d124b01b"] self.assertEqual(set(expected_recordings), set(updateddataset["classes"][0]["recordings"]))
def test_add_class_with_recordings(self): """ Add a class to a dataset and also add some recordings to it """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #3", "description": "This is description of class 3", "recordings": ["1c085555-3805-428a-982f-e14e0a2b18e6"] } dataset.add_class(dsid, test_data) updateddataset = dataset.get(dsid) # Check that both the class details and recordings are in the retrieved dataset expected = copy.deepcopy(test_data) expected["id"] = mock.ANY self.assertDictEqual(expected, updateddataset["classes"][2])
def test_update_class(self): dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #1", "new_name": "Different class", "description": "different desc" } originaldataset = dataset.get(dsid) dataset.update_class(dsid, "Class #1", test_data) updateddataset = dataset.get(dsid) self.assertEqual("Class #2", updateddataset["classes"][1]["name"]) self.assertEqual("", updateddataset["classes"][1]["description"]) self.assertEqual("Class #1", originaldataset["classes"][0]["name"]) self.assertEqual("Different class", updateddataset["classes"][0]["name"]) self.assertEqual("different desc", updateddataset["classes"][0]["description"])
def test_add_class_twice(self): """ A class name which already exists is skipped. """ dsid = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) test_data = { "name": "Class #1", "description": "This is description of class 1", "recordings": ["1c085555-3805-428a-982f-e14e0a2b18e6"] } dataset.add_class(dsid, test_data) updateddataset = dataset.get(dsid) # Class #1 is not added again, but the recording is added to it self.assertEqual(2, len(updateddataset["classes"])) expected_recordings = ["0dad432b-16cc-4bf0-8961-fd31d124b01b", "19e698e7-71df-48a9-930e-d4b1a2026c82", "1c085555-3805-428a-982f-e14e0a2b18e6"] self.assertEqual(set(expected_recordings), set(updateddataset["classes"][0]["recordings"]))
def test_eval_job_delete(self): resp = self.client.delete("/datasets/%s/%s" % (self.test_uuid, self.test_uuid)) self.assert404(resp) dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.delete("/datasets/%s/%s" % (dataset_id, self.test_uuid)) self.assert404(resp) job_id = dataset_eval.evaluate_dataset(dataset_id, False, dataset_eval.EVAL_LOCAL) resp = self.client.delete("/datasets/%s/%s" % (dataset_id, job_id)) self.assert401(resp) # As an author self.temporary_login(self.test_user_id) resp = self.client.delete("/datasets/%s/%s" % (dataset_id, job_id)) self.assert200(resp) self.assertIsNone(dataset_eval.get_job(job_id))
def test_recording_info_in_dataset(self, get_recording_by_id): """ Tests views.datasets.recording_info_in_dataset. If the Recording MBID is present in the dataset, the view should return information about the MBID. Otherwise, it should return a 404 response """ # Note: self.test_mbid_1 is in the dataset get_recording_by_id.return_value = { 'title': 'recording_title', 'artist-credit-phrase': 'artist_credit' } dataset_id = dataset.create_from_dict(self.test_data, author_id=self.test_user_id) resp = self.client.get( url_for("datasets.recording_info_in_dataset", dataset_id=dataset_id, mbid=self.test_mbid_1)) self.assert200(resp) expected = { 'recording': { 'title': 'recording_title', 'artist': 'artist_credit' } } self.assertEqual(expected, json.loads(resp.data)) # self.test_uuid is not in the dataset resp = self.client.get( url_for("datasets.recording_info_in_dataset", dataset_id=dataset_id, mbid=self.test_uuid)) self.assert404(resp)