|
52 | 52 | AffiliationRecommendation,
|
53 | 53 | MergeRecommendation,
|
54 | 54 | GenderRecommendation)
|
| 55 | +from sortinghat.core.recommendations import RecommendationEngine |
55 | 56 |
|
56 | 57 | JOB_NOT_FOUND_ERROR = "DEF not found in the registry"
|
57 | 58 |
|
@@ -1243,6 +1244,60 @@ def test_not_found_uuid_error(self, mock_find_indv):
|
1243 | 1244 |
|
1244 | 1245 | self.assertDictEqual(result, expected)
|
1245 | 1246 |
|
| 1247 | + @unittest.mock.patch('sortinghat.core.jobs.RecommendationEngine') |
| 1248 | + def test_recommend_matches_with_concurrent_removal(self, mock_recommendation_engine): |
| 1249 | + """Check if recommendations are obtained when an identity is removed while the job is running""" |
| 1250 | + |
| 1251 | + ctx = SortingHatContext(self.user) |
| 1252 | + |
| 1253 | + # Mock RecommendationEngine class to return a non-existing key and an existing one |
| 1254 | + def mock_recommend_matches(*args, **kwargs): |
| 1255 | + yield (self.john_smith.individual.mk, |
| 1256 | + self.john_smith.individual.mk, |
| 1257 | + ['non_existing_mk', self.jsmith.individual.mk]) |
| 1258 | + |
| 1259 | + class MockRecommendationEngine(RecommendationEngine): |
| 1260 | + RECOMMENDATION_TYPES = { |
| 1261 | + 'matches': mock_recommend_matches, |
| 1262 | + } |
| 1263 | + |
| 1264 | + mock_recommendation_engine.return_value = MockRecommendationEngine() |
| 1265 | + |
| 1266 | + # Test |
| 1267 | + expected = { |
| 1268 | + 'results': { |
| 1269 | + self.john_smith.uuid: sorted(['non_existing_mk', self.jsm3.individual.mk]) |
| 1270 | + } |
| 1271 | + } |
| 1272 | + recommendations_expected = [ |
| 1273 | + sorted([self.jsmith.individual.mk, self.john_smith.individual.mk]) |
| 1274 | + ] |
| 1275 | + |
| 1276 | + source_uuids = [self.john_smith.uuid] |
| 1277 | + target_uuids = [self.john_smith.uuid, |
| 1278 | + self.jsmith.uuid] |
| 1279 | + |
| 1280 | + criteria = ['email', 'name', 'username'] |
| 1281 | + |
| 1282 | + job = recommend_matches.delay(ctx, |
| 1283 | + source_uuids, |
| 1284 | + target_uuids, |
| 1285 | + criteria) |
| 1286 | + result = job.result |
| 1287 | + |
| 1288 | + # Preserve job results order for the comparison against the expected results |
| 1289 | + for key in result['results']: |
| 1290 | + result['results'][key] = sorted(result['results'][key]) |
| 1291 | + |
| 1292 | + self.assertDictEqual(result, expected) |
| 1293 | + |
| 1294 | + self.assertEqual(MergeRecommendation.objects.count(), 1) |
| 1295 | + |
| 1296 | + for rec in recommendations_expected: |
| 1297 | + self.assertTrue( |
| 1298 | + MergeRecommendation.objects.filter(individual1=rec[0], |
| 1299 | + individual2=rec[1]).exists()) |
| 1300 | + |
1246 | 1301 | def test_transactions(self):
|
1247 | 1302 | """Check if the right transactions were created"""
|
1248 | 1303 |
|
|
0 commit comments