def test_datasets_recommendations_from_config(self, rmock, mock_response, datasets, reuses): ds1, ds2, ds3 = datasets r1, r2 = reuses ds4 = DatasetFactory() rmock.get(MOCK_URL, json=mock_response) ds2.extras['recommendations:sources'] = ['existing'] ds2.extras['recommendations'] = [ {'id': str(ds4.id), 'source': 'existing', 'score': 50}, ] ds2.save() recommendations_add({'fake_source': MOCK_URL}, should_clean=False) # Recommendations have been merged, new source has been added ds2.reload() assert set(ds2.extras['recommendations:sources']) == set(['existing', 'fake_source']) assert ds2.extras['recommendations'] == [ {'id': str(ds4.id), 'source': 'existing', 'score': 50}, {'id': str(ds1.id), 'source': 'fake_source', 'score': 2}, {'id': str(ds3.id), 'source': 'fake_source', 'score': 1}, ] assert ds2.extras['recommendations-reuses'] == [ {'id': str(r2.id), 'source': 'fake_source', 'score': 100}, {'id': str(r1.id), 'source': 'fake_source', 'score': 50}, ]
def test_datasets_recommendations_from_config_clean( self, mock_response, rmock, datasets): ds1, ds2, ds3 = datasets rmock.get(MOCK_URL, json=mock_response) ds1.extras['recommendations:sources'] = ['fake_source'] ds1.extras['recommendations'] = [{ 'id': str(ds2.id), 'source': 'fake_source', 'score': 100 }] ds1.save() recommendations_add({'fake_source': MOCK_URL}, should_clean=True) # Correct recommendations have been filled ds2.reload() assert ds2.extras['recommendations:sources'] == ['fake_source'] assert ds2.extras['recommendations'] == [ { 'id': str(ds1.id), 'source': 'fake_source', 'score': 2 }, { 'id': str(ds3.id), 'source': 'fake_source', 'score': 1 }, ] # Previous recommendations have been cleaned ds1.reload() assert ds1.extras == {}
def test_datasets_recommendations_from_config_empty_db( self, rmock, mock_response, datasets): ds1, ds2, ds3 = datasets rmock.get(MOCK_URL, json=mock_response) recommendations_add({'fake_source': MOCK_URL}, should_clean=False) # Correct recommendations have been filled ds2.reload() assert ds2.extras['recommendations:sources'] == ['fake_source'] assert ds2.extras['recommendations'] == [ { 'id': str(ds1.id), 'source': 'fake_source', 'score': 2 }, { 'id': str(ds3.id), 'source': 'fake_source', 'score': 1 }, ] # Invalid recommendations have not been filled ds1.reload() ds3.reload() assert ds1.extras == {} assert ds3.extras == {}
def test_datasets_recommendations_ignore_self_recommendation(self, rmock, datasets): ds1, _, _ = datasets rmock.get(MOCK_URL, json=[{ "id": str(ds1.id), "recommendations": [{ "id": str(ds1.id), "score": 50 }] }]) recommendations_add({'fake_source': MOCK_URL}, should_clean=True) ds1.reload() assert ds1.extras == {}
def test_datasets_recommendations_invalid_data_in_config(self, mock_invalid_response, rmock): rmock.get(MOCK_URL, json=mock_invalid_response) with pytest.raises(jsonschema.exceptions.ValidationError): recommendations_add({'fake_source': MOCK_URL}, should_clean=False)