def test_delete_deep(self): # test views: man = TogetherManager.getManager(Resource.VIEW) tracker = SessionResourcesTracker() tracker.add_view(self.res_1, datetime.datetime(2012, 7, 16, 18, 0, 0)) tracker.add_view(self.res_2, datetime.datetime(2012, 7, 16, 18, 0, 1)) # both resources have been viewed 'together' self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) # deep delete resource self.res_1.delete_deep() self.assertEquals(0, len(man.getTogetherList(self.res_2, 0))) # test downloads: man = TogetherManager.getManager(Resource.DOWNLOAD) tracker.add_download(self.res_3, datetime.datetime(2012, 7, 16, 18, 0, 0)) tracker.add_download(self.res_4, datetime.datetime(2012, 7, 16, 18, 0, 1)) # both resources have been downloaded 'together' self.assertEquals(1, len(man.getTogetherList(self.res_3, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_4, 0))) self.assertEquals(1, man.getTogetherCount(self.res_3, self.res_4)) # deep delete resource self.res_3.delete_deep() self.assertEquals(0, len(man.getTogetherList(self.res_4, 0)))
def setUp(self): """ Import test fixtures and add resource pairs to TogetherManager """ test_utils.setup_test_storage() self.res_1 = _import_resource('elra112.xml') self.res_2 = _import_resource('elra135.xml') self.res_3 = _import_resource('elra260.xml') self.res_4 = _import_resource('elra295.xml') man = TogetherManager.getManager(Resource.VIEW) count = 0 while (count < 5): man.addResourcePair(self.res_1, self.res_2) count += 1 count = 0 while (count < 10): man.addResourcePair(self.res_1, self.res_3) count += 1 count = 0 while (count < 3): man.addResourcePair(self.res_1, self.res_4) count += 1 man = TogetherManager.getManager(Resource.DOWNLOAD) count = 0 while (count < 15): man.addResourcePair(self.res_1, self.res_2) count += 1 count = 0 while (count < 10): man.addResourcePair(self.res_1, self.res_3) count += 1 count = 0 while (count < 5): man.addResourcePair(self.res_1, self.res_4) count += 1
def test_usage(self): # test views: man = TogetherManager.getManager(Resource.VIEW) tracker = SessionResourcesTracker() tracker.add_view(self.res_1, datetime.datetime(2012, 7, 16, 18, 0, 0)) tracker.add_view(self.res_2, datetime.datetime(2012, 7, 16, 18, 0, 1)) # both resources have been viewed 'together' self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) # viewing the same resource again does NOT increase the count tracker.add_view(self.res_2, datetime.datetime(2012, 7, 16, 18, 0, 2)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) # viewing a resource after the max view interval changes nothing tracker.add_view(self.res_3, datetime.datetime(2012, 7, 16, 18, 0, 10)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(0, len(man.getTogetherList(self.res_3, 0))) # add another resource tracker.add_view(self.res_4, datetime.datetime(2012, 7, 16, 18, 0, 12)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(1, len(man.getTogetherList(self.res_3, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_4, 0))) self.assertEquals(1, man.getTogetherCount(self.res_3, self.res_4)) # test downloads: man = TogetherManager.getManager(Resource.DOWNLOAD) tracker.add_download(self.res_1, datetime.datetime(2012, 7, 16, 18, 0, 0)) tracker.add_download(self.res_2, datetime.datetime(2012, 7, 16, 18, 0, 1)) # both resources have been downloaded 'together' self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) # downloading the same resource again does NOT increase the count tracker.add_download(self.res_2, datetime.datetime(2012, 7, 16, 18, 0, 2)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) # downloading a resource after the max download interval changes nothing tracker.add_download(self.res_3, datetime.datetime(2012, 7, 16, 18, 0, 20)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(0, len(man.getTogetherList(self.res_3, 0))) # add another resource tracker.add_download(self.res_4, datetime.datetime(2012, 7, 16, 18, 0, 22)) self.assertEquals(1, len(man.getTogetherList(self.res_1, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_2, 0))) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(1, len(man.getTogetherList(self.res_3, 0))) self.assertEquals(1, len(man.getTogetherList(self.res_4, 0))) self.assertEquals(1, man.getTogetherCount(self.res_3, self.res_4))
def test_sorting(self): man = TogetherManager.getManager(Resource.VIEW) sorted_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(sorted_res)) self.assertEquals(self.res_3, sorted_res[0]) self.assertEquals(self.res_2, sorted_res[1]) self.assertEquals(self.res_4, sorted_res[2])
def get_download_recommendations(resource): """ Returns a list of ranked download recommendations for the given resource. """ # TODO: decide what threshold to use; may restrict recommendation to top X resources of the list return TogetherManager.getManager(Resource.DOWNLOAD)\ .getTogetherList(resource, 0)
def test_delete_deep(self): man = TogetherManager.getManager(Resource.VIEW) sorted_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(sorted_res)) self.res_1.delete_deep() sorted_res = man.getTogetherList(self.res_2, 0) self.assertEqual(0, len(sorted_res))
def test_deleted_filter(self): self.res_2.storage_object.deleted = True self.res_2.storage_object.save() self.res_2.storage_object.update_storage() man = TogetherManager.getManager(Resource.VIEW) sorted_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(sorted_res)) self.assertEquals(self.res_3, sorted_res[0]) self.assertEquals(self.res_4, sorted_res[1])
def test_publication_status_filter(self): self.res_3.storage_object.publication_status = INGESTED self.res_3.storage_object.save() self.res_3.storage_object.update_storage() man = TogetherManager.getManager(Resource.VIEW) sorted_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(sorted_res)) self.assertEquals(self.res_2, sorted_res[0]) self.assertEquals(self.res_4, sorted_res[1])
def _add_resource_to_set(self, res_set, res, res_type): """ Adds the given resource to the given resource set; resource is of the given resource type, either Resource.VIEW or Resource.DOWNLOAD. """ if not res in res_set: # update TogetherManager with new pairs man = TogetherManager.getManager(res_type) for _res in res_set: man.addResourcePair(_res, res) res_set.add(res)
def test_unique_together_constraint(self): man = TogetherManager.getManager(Resource.VIEW) man.addResourcePair(self.res_1, self.res_2) res_count_dict = man.resourcecountdict_set.get( lrid=self.res_1.storage_object.identifier) try: res_count_dict.resourcecountpair_set.create( lrid = self.res_2.storage_object.identifier) self.fail("Should have raised an exception") except IntegrityError: # reset database connection; required for PostgreSQL from django import db db.close_connection()
def _add_resource_to_set(self, res_set, res, res_type): """ Adds the given resource to the given resource set; resource is of the given resource type, either Resource.VIEW or Resource.DOWNLOAD. """ if not res in res_set: # update TogetherManager with new pairs but make sure that only one # thread updates the TogetherManager at a time with SessionResourcesTracker.lock: man = TogetherManager.getManager(res_type) for _res in res_set: man.addResourcePair(_res, res) res_set.add(res)
def test_delete_deep_with_keeping_recommendations(self): """ tests that deep deleting a resource keeps the recommendations if requested """ man = TogetherManager.getManager(Resource.VIEW) self.assertEquals(0, man.getTogetherCount(self.res_1, self.res_2)) man.addResourcePair(self.res_1, self.res_2) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(2, len(ResourceCountPair.objects.all())) self.assertEquals(2, len(ResourceCountDict.objects.all())) self.res_1.delete_deep(keep_stats=True) # recommendations stay the same self.assertEquals(2, len(ResourceCountPair.objects.all())) self.assertEquals(2, len(ResourceCountDict.objects.all()))
def test_delete_deep(self): """ tests that deep deleting a resource removes it from the counts """ man = TogetherManager.getManager(Resource.VIEW) self.assertEquals(0, man.getTogetherCount(self.res_1, self.res_2)) man.addResourcePair(self.res_1, self.res_2) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(2, len(ResourceCountPair.objects.all())) self.assertEquals(2, len(ResourceCountDict.objects.all())) self.res_1.delete_deep() # after deep deletion, only one instance remains: # the (empty) resource count dictionary of res_2 self.assertEquals(0, len(ResourceCountPair.objects.all())) self.assertEquals(1, len(ResourceCountDict.objects.all()))
def test_counts(self): """ tests the addResourcePair and getTogetherCount methods of TogetherManager """ man = TogetherManager.getManager(Resource.VIEW) self.assertEquals(0, man.getTogetherCount(self.res_1, self.res_2)) man.addResourcePair(self.res_1, self.res_2) self.assertEquals(0, man.getTogetherCount(self.res_1, self.res_3)) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(1, man.getTogetherCount(self.res_2, self.res_1)) man.addResourcePair(self.res_1, self.res_3) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_3)) self.assertEquals(1, man.getTogetherCount(self.res_3, self.res_1)) man.addResourcePair(self.res_1, self.res_2) self.assertEquals(2, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(2, man.getTogetherCount(self.res_2, self.res_1))
def test_delete_deep_with_keeping_recommendations(self): """ tests that deep deleting a resource keeps the recommendations if requested """ man = TogetherManager.getManager(Resource.VIEW) self.assertEquals(0, man.getTogetherCount(self.res_1, self.res_2)) man.addResourcePair(self.res_1, self.res_2) self.assertEquals(1, man.getTogetherCount(self.res_1, self.res_2)) self.assertEquals(2, len(ResourceCountPair.objects.all())) self.assertEquals(2, len(ResourceCountDict.objects.all())) self.assertEquals(3, len(resourceInfoType_model.objects.all())) self.assertEquals(3, len(StorageObject.objects.all())) self.res_1.storage_object.delete() self.res_1.delete_deep(keep_stats=True) self.assertEquals(2, len(resourceInfoType_model.objects.all())) self.assertEquals(2, len(StorageObject.objects.all())) # recommendations stay the same self.assertEquals(2, len(ResourceCountPair.objects.all())) self.assertEquals(2, len(ResourceCountDict.objects.all())) # recommendations are deleted after repairing them call_command('repair_recommendations', interactive=False) self.assertEquals(0, len(ResourceCountPair.objects.all())) self.assertEquals(1, len(ResourceCountDict.objects.all()))
def get_view_recommendations(resource): """ Returns a list of ranked view recommendations for the given resource. """ # TODO: decide what threshold to use; may restrict recommendation to top X resources of the list return TogetherManager.getManager(Resource.VIEW).getTogetherList(resource, 0)
def test_threshold_filter(self): man = TogetherManager.getManager(Resource.VIEW) sorted_res = man.getTogetherList(self.res_1, 5) self.assertEqual(2, len(sorted_res)) self.assertEquals(self.res_3, sorted_res[0]) self.assertEquals(self.res_2, sorted_res[1])
def test_views(self): # client 1 views all 4 resources client_1 = Client() man = TogetherManager.getManager(Resource.VIEW) response = client_1.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(view_res)) response = client_1.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(1, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(view_res)) self.assertEqual(1, man.getTogetherCount(self.res_1, self.res_2)) response = client_1.get(self.res_3.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(2, len(view_res)) response = client_1.get(self.res_4.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_4, 0) self.assertEqual(3, len(view_res)) # another client views 2 of the resources, counts are increased client_2 = Client() response = client_2.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) response = client_2.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) # counts of res_1 and res_2 appearing together is increased self.assertEqual(2, man.getTogetherCount(self.res_1, self.res_2)) # make sure that downloads are no touched man = TogetherManager.getManager(Resource.DOWNLOAD) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(download_res)) # make sure that statistics are updated when a resource is # completely removed saveLRStats(self.res_1, UPDATE_STAT) saveLRStats(self.res_2, UPDATE_STAT) saveLRStats(self.res_3, UPDATE_STAT) saveLRStats(self.res_4, UPDATE_STAT) self.assertEquals(9, len(LRStats.objects.all())) self.assertEquals(228, len(UsageStats.objects.all())) remove_resource(self.res_1.storage_object) self.assertEquals(7, len(LRStats.objects.all())) self.assertEquals(181, len(UsageStats.objects.all()))
def test_downloads(self): # client 1 downloads all 4 resources client_1 = Client() client_1.login(username='******', password='******') man = TogetherManager.getManager(Resource.DOWNLOAD) response = _download_resource(client_1, self.res_1) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(download_res)) response = _download_resource(client_1, self.res_2) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(1, len(download_res)) download_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(download_res)) self.assertEqual(1, man.getTogetherCount(self.res_1, self.res_2)) response = _download_resource(client_1, self.res_3) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(download_res)) download_res = man.getTogetherList(self.res_2, 0) self.assertEqual(2, len(download_res)) download_res = man.getTogetherList(self.res_3, 0) self.assertEqual(2, len(download_res)) response = _download_resource(client_1, self.res_4) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(download_res)) download_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(download_res)) download_res = man.getTogetherList(self.res_3, 0) self.assertEqual(3, len(download_res)) download_res = man.getTogetherList(self.res_4, 0) self.assertEqual(3, len(download_res)) # another client downloads 2 of the resources, counts are increased client_2 = Client() client_2.login(username='******', password='******') response = _download_resource(client_2, self.res_1) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(download_res)) response = _download_resource(client_2, self.res_2) self.assertEquals(200, response.status_code) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(download_res)) download_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(download_res)) # counts of res_1 and res_2 appearing together is increased self.assertEqual(2, man.getTogetherCount(self.res_1, self.res_2)) # make sure that views are no touched; # in the web interface, the resource has of course to be viewed first # before it could be downloaded man = TogetherManager.getManager(Resource.VIEW) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(view_res))
def test_views(self): # client 1 views all 4 resources client_1 = Client() man = TogetherManager.getManager(Resource.VIEW) response = client_1.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(view_res)) response = client_1.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(1, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(view_res)) self.assertEqual(1, man.getTogetherCount(self.res_1, self.res_2)) response = client_1.get(self.res_3.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(2, len(view_res)) response = client_1.get(self.res_4.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_4, 0) self.assertEqual(3, len(view_res)) # another client views 2 of the resources, counts are increased client_2 = Client() response = client_2.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) response = client_2.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) # counts of res_1 and res_2 appearing together is increased self.assertEqual(2, man.getTogetherCount(self.res_1, self.res_2)) # make sure that downloads are no touched man = TogetherManager.getManager(Resource.DOWNLOAD) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(download_res)) # make sure that statistics are updated when a resource is # completely removed saveLRStats(self.res_1, UPDATE_STAT) saveLRStats(self.res_2, UPDATE_STAT) saveLRStats(self.res_3, UPDATE_STAT) saveLRStats(self.res_4, UPDATE_STAT) self.assertEquals(9, len(LRStats.objects.all())) # TODO: checka again self.assertEquals(256, len(UsageStats.objects.all())) remove_resource(self.res_1.storage_object) self.assertEquals(7, len(LRStats.objects.all())) # TODO: check again self.assertEquals(192, len(UsageStats.objects.all()))