<s> from datetime import date , timedelta <EOL> from django . conf import settings <EOL> from nose . tools import eq_ <EOL> from kitsune . dashboards . cron import ( <EOL> cache_most_unhelpful_kb_articles , _get_old_unhelpful , <EOL> _get_current_unhelpful , update_l10n_coverage_metrics , <EOL> update_l10n_contributor_metrics ) <EOL> from kitsune . dashboards . models import ( <EOL> WikiMetric , L10N_TOP20_CODE , L10N_TOP100_CODE , L10N_ALL_CODE ) <EOL> from kitsune . products . tests import ProductFactory <EOL> from kitsune . sumo . redis_utils import redis_client , RedisError <EOL> from kitsune . sumo . tests import SkipTest , TestCase <EOL> from kitsune . users . tests import UserFactory <EOL> from kitsune . wiki . tests import ( <EOL> RevisionFactory , ApprovedRevisionFactory , DocumentFactory , HelpfulVoteFactory ) <EOL> def _add_vote_in_past ( rev , vote , days_back ) : <EOL> HelpfulVoteFactory ( <EOL> revision = rev , <EOL> helpful = vote , <EOL> created = date . today ( ) - timedelta ( days = days_back ) ) <EOL> def _make_backdated_revision ( backdate ) : <EOL> return RevisionFactory ( created = date . today ( ) - timedelta ( days = backdate ) ) <EOL> class TopUnhelpfulArticlesTests ( TestCase ) : <EOL> def test_no_old_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> result = _get_old_unhelpful ( ) <EOL> eq_ ( <NUM_LIT:0> , len ( result ) ) <EOL> def test_no_current_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> result = _get_current_unhelpful ( { } ) <EOL> eq_ ( <NUM_LIT:0> , len ( result ) ) <EOL> def test_old_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT:10> ) <EOL> for x in range ( <NUM_LIT:4> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> result = _get_old_unhelpful ( ) <EOL> eq_ ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertAlmostEqual ( <NUM_LIT> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> eq_ ( <NUM_LIT:5> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> def test_old_articles_helpful ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> result = _get_old_unhelpful ( ) <EOL> eq_ ( <NUM_LIT:0> , len ( result ) ) <EOL> def test_current_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> old_data = { r . document . id : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } } <EOL> result = _get_current_unhelpful ( old_data ) <EOL> eq_ ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertAlmostEqual ( <NUM_LIT> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> self . assertAlmostEqual ( <NUM_LIT> - old_data [ r . document . id ] [ '<STR_LIT>' ] , <EOL> result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> eq_ ( <NUM_LIT:5> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> def test_current_articles_helpful ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> old_data = { r . document . id : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } } <EOL> result = _get_current_unhelpful ( old_data ) <EOL> eq_ ( <NUM_LIT:0> , len ( result ) ) <EOL> def test_current_articles_not_in_old ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> old_data = { } <EOL> result = _get_current_unhelpful ( old_data ) <EOL> eq_ ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertAlmostEqual ( <NUM_LIT> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> self . assertAlmostEqual ( <NUM_LIT:0> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> eq_ ( <NUM_LIT:5> , result [ r . document . id ] [ '<STR_LIT>' ] ) <EOL> class TopUnhelpfulArticlesCronTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TopUnhelpfulArticlesCronTests , self ) . setUp ( ) <EOL> self . REDIS_KEY = settings . HELPFULVOTES_UNHELPFUL_KEY <EOL> try : <EOL> self . redis = redis_client ( '<STR_LIT>' ) <EOL> self . redis . flushdb ( ) <EOL> except RedisError : <EOL> raise SkipTest <EOL> def tearDown ( self ) : <EOL> try : <EOL> self . redis . flushdb ( ) <EOL> except ( KeyError , AttributeError ) : <EOL> raise SkipTest <EOL> super ( TopUnhelpfulArticlesCronTests , self ) . tearDown ( ) <EOL> def test_no_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> cache_most_unhelpful_kb_articles ( ) <EOL> eq_ ( <NUM_LIT:0> , self . redis . llen ( self . REDIS_KEY ) ) <EOL> def test_caching_unhelpful ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> cache_most_unhelpful_kb_articles ( ) <EOL> eq_ ( <NUM_LIT:1> , self . redis . llen ( self . REDIS_KEY ) ) <EOL> result = self . redis . lrange ( self . REDIS_KEY , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> eq_ ( u'<STR_LIT>' % <EOL> ( r . document . id , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , r . document . slug , <EOL> r . document . title ) , <EOL> result [ <NUM_LIT:0> ] . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_caching_helpful ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> cache_most_unhelpful_kb_articles ( ) <EOL> eq_ ( <NUM_LIT:0> , self . redis . llen ( self . REDIS_KEY ) ) <EOL> def test_caching_changed_helpfulness ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> cache_most_unhelpful_kb_articles ( ) <EOL> eq_ ( <NUM_LIT:1> , self . redis . llen ( self . REDIS_KEY ) ) <EOL> result = self . redis . lrange ( self . REDIS_KEY , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> eq_ ( u'<STR_LIT>' % <EOL> ( r . document . id , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> , r . document . slug , <EOL> r . document . title ) , <EOL> result [ <NUM_LIT:0> ] . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_caching_sorting ( self ) : <EOL> """<STR_LIT>""" <EOL> r = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> r2 = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r2 , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r2 , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> r3 = _make_backdated_revision ( <NUM_LIT> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r3 , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> for x in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _add_vote_in_past ( r3 , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> cache_most_unhelpful_kb_articles ( ) <EOL> eq_ ( <NUM_LIT:3> , self . redis . llen ( self . REDIS_KEY ) ) <EOL> result = self . redis . lrange ( self . REDIS_KEY , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> assert '<STR_LIT>' % ( r2 . document . id , <NUM_LIT> ) in result [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT>' % ( r3 . document . id , <NUM_LIT> ) in result [ <NUM_LIT:1> ] <EOL> assert '<STR_LIT>' % ( r . document . id , <NUM_LIT> ) in result [ <NUM_LIT:2> ] <EOL> class L10nMetricsTests ( TestCase ) : <EOL> def test_update_l10n_coverage_metrics ( self ) : <EOL> """<STR_LIT>""" <EOL> p = ProductFactory ( visible = True ) <EOL> revisions = RevisionFactory . create_batch ( <EOL> <NUM_LIT:20> , <EOL> is_approved = True , <EOL> is_ready_for_localization = True , <EOL> document__products = [ p ] , <EOL> document__locale = '<STR_LIT>' ) <EOL> r1 = revisions [ <NUM_LIT:0> ] <EOL> r2 = revisions [ <NUM_LIT:1> ] <EOL> d = DocumentFactory ( parent = r1 . document , locale = '<STR_LIT>' ) <EOL> ApprovedRevisionFactory ( document = d , based_on = r1 ) <EOL> d = DocumentFactory ( parent = r1 . document , locale = '<STR_LIT>' ) <EOL> ApprovedRevisionFactory ( document = d , based_on = r1 ) <EOL> d = DocumentFactory ( parent = r2 . document , locale = '<STR_LIT>' ) <EOL> ApprovedRevisionFactory ( document = d , based_on = r2 ) <EOL> for r in revisions : <EOL> d = DocumentFactory ( parent = r . document , locale = '<STR_LIT>' ) <EOL> RevisionFactory ( document = d , based_on = r , is_approved = True ) <EOL> update_l10n_coverage_metrics ( ) <EOL> eq_ ( <NUM_LIT:6> , WikiMetric . objects . filter ( locale = '<STR_LIT>' ) . count ( ) ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:6> , WikiMetric . objects . filter ( locale = '<STR_LIT>' ) . count ( ) ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:6> , WikiMetric . objects . filter ( locale = '<STR_LIT>' ) . count ( ) ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:6> , WikiMetric . objects . filter ( locale = '<STR_LIT>' ) . count ( ) ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , code = L10N_ALL_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP20_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_TOP100_CODE ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , code = L10N_ALL_CODE ) . value ) <EOL> def test_update_active_contributor_metrics ( self ) : <EOL> """<STR_LIT>""" <EOL> day = date ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) <EOL> last_month = date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:15> ) <EOL> start_date = date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) <EOL> before_start = date ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) <EOL> d = DocumentFactory ( locale = '<STR_LIT>' ) <EOL> u = UserFactory ( ) <EOL> RevisionFactory ( document = d , created = last_month , is_approved = True , reviewer = u ) <EOL> RevisionFactory ( document = d , creator = u , created = last_month ) <EOL> p = ProductFactory ( visible = True ) <EOL> RevisionFactory ( created = start_date , document__products = [ p ] ) <EOL> RevisionFactory ( document = d , created = before_start ) <EOL> RevisionFactory ( document = d , created = day ) <EOL> d = DocumentFactory ( locale = '<STR_LIT>' ) <EOL> RevisionFactory ( document = d , created = last_month , is_approved = True , reviewer = u ) <EOL> RevisionFactory ( document = d , creator = u , created = last_month , reviewer = UserFactory ( ) ) <EOL> RevisionFactory ( document = d , created = start_date ) <EOL> RevisionFactory ( document = d , created = last_month ) <EOL> RevisionFactory ( document = d , created = before_start ) <EOL> RevisionFactory ( document = d , created = day ) <EOL> update_l10n_contributor_metrics ( day ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , date = start_date ) . value ) <EOL> eq_ ( <NUM_LIT:1.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , date = start_date ) . value ) <EOL> eq_ ( <NUM_LIT> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = None , date = start_date ) . value ) <EOL> eq_ ( <NUM_LIT:0.0> , WikiMetric . objects . get ( locale = '<STR_LIT>' , product = p , date = start_date ) . value ) </s>
