def test_no_mixup(self): acontr = ArticleController() ccontr = ClusterController() total_clusters = len(list(ccontr.read())) total_articles = len(list(acontr.read())) for cluster in ccontr.read(): self.assertEqual(1, len(cluster.articles)) for article in acontr.read(): acontr.create(entry_id=article.entry_id, feed_id=article.feed_id, title=article.title, content=article.content, link=article.link) for user_id in ArticleController.get_user_id_with_pending_articles(): ClusterController(user_id).clusterize_pending_articles() self.assertEqual(2 * total_articles, len(list(acontr.read()))) self.assertEqual(2 * total_clusters, len(list(ccontr.read()))) for cluster in ccontr.read(): self.assertEqual(1, len(cluster.articles)) self.assertEqual(1, len({a.user_id for a in cluster.articles})) main_article = acontr.read().first() for article in acontr.read(): acontr.create(user_id=main_article.user_id, feed_id=main_article.feed_id, entry_id=article.entry_id, title=article.title, content=article.content, link=article.link) for cluster in ccontr.read(): self.assertEqual(1, len({a.user_id for a in cluster.articles}))
def test_adding_to_cluster_by_link(self): ccontr = ClusterController() cluster = ccontr.read().first() ccontr.update({'id': cluster.id}, { 'read': True, 'read_reason': 'marked' }) cluster = ccontr.get(id=cluster.id) self.assertTrue(cluster.read) article = cluster.articles[0] articles_count = len(cluster.articles) fcontr = FeedController(cluster.user_id) acontr = ArticleController(cluster.user_id) fcontr.update({'id': article.feed_id}, {'cluster_wake_up': True}) feed = fcontr.read(id__ne=article.feed_id).first() update_on_all_objs(articles=[article], feeds=[feed], cluster_enabled=True) self._clone_article(acontr, article, feed) ccontr.clusterize_pending_articles() cluster = ccontr.get(id=cluster.id) self.assertEqual(articles_count + 1, len(cluster.articles)) self.assertFalse(cluster.read)
def _test_unread_on_cluster(self, read_reason): ccontr = ClusterController() fcontr = FeedController() cluster = ccontr.read().first() clusterizer = Clusterizer() self.assertFalse(clusterizer.get_config(cluster, 'cluster_enabled')) self.assertTrue(clusterizer.get_config(cluster, 'cluster_wake_up')) ccontr.update({'id': cluster.id}, { 'read': True, 'read_reason': read_reason }) target_feed = fcontr.read(id__ne=cluster.main_article.feed_id, user_id=cluster.user_id).first() clusterizer = Clusterizer() self.assertFalse(clusterizer.get_config(target_feed, 'cluster_enabled')) fcontr.update( {'id__in': [f.id for f in cluster.feeds] + [target_feed.id]}, { 'cluster_wake_up': True, 'cluster_enabled': True }) clusterizer = Clusterizer() self.assertTrue(clusterizer.get_config(cluster, 'cluster_enabled')) target_feed = fcontr.read(id__ne=cluster.main_article.feed_id, user_id=cluster.user_id).first() article = self._clone_article(ArticleController(), cluster.main_article, target_feed) clusterizer = Clusterizer() self.assertTrue(clusterizer.get_config(article, 'cluster_wake_up')) ClusterController(cluster.user_id).clusterize_pending_articles() self.assertEqual(2, len(article.cluster.articles)) self.assertInCluster(article, cluster) return ccontr.get(id=cluster.id)
def remove_from_cluster(self, article): """Removes article with id == article_id from the cluster it belongs to If it's the only article of the cluster will delete the cluster Return True if the article is deleted at the end or not """ from jarr.controllers.cluster import ClusterController from jarr.controllers.article_clusterizer import Clusterizer if not article.cluster_id: return clu_ctrl = ClusterController(self.user_id) cluster = clu_ctrl.read(id=article.cluster_id).first() if not cluster: return try: new_art = next(new_art for new_art in cluster.articles if new_art.id != article.id) except StopIteration: # only on article in cluster, deleting cluster clu_ctrl.delete(cluster.id, delete_articles=False) else: if cluster.main_article_id == article.id: cluster.main_article_id = None Clusterizer(article.user_id).enrich_cluster( cluster, new_art, cluster.read, cluster.liked, force_article_as_main=True) self.update({'id': article.id}, {'cluster_id': None, 'cluster_reason': None, 'cluster_score': None, 'cluster_tfidf_with': None, 'cluster_tfidf_neighbor_size': None})
def test_delete(self): clu_ctrl = ClusterController() for cluster in clu_ctrl.read(): clu_ctrl.delete(cluster.id) self.assertEqual(0, ClusterController(2).read().count()) self.assertEqual(0, ArticleController(2).read().count())