<s> from datetime import date , datetime , time , timedelta <EOL> from django . contrib . auth . models import User <EOL> from django . test import TestCase <EOL> from mox3 import mox <EOL> from django_lean . lean_retention . models import DailyActivity <EOL> from django_lean . lean_retention . reports import ( sort_retention_periods , <EOL> Cohort , Period ) <EOL> from django_lean . utils import get_current_site <EOL> class TestReportUtils ( TestCase ) : <EOL> def test_sort_retention_periods ( self ) : <EOL> self . assertEqual ( sort_retention_periods ( [ ] ) , [ ] ) <EOL> self . assertEqual ( sort_retention_periods ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( sort_retention_periods ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) , [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( sort_retention_periods ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ] ) , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertRaises ( TypeError , sort_retention_periods , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] ) <EOL> self . assertRaises ( TypeError , sort_retention_periods , [ '<STR_LIT:1>' , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertRaises ( ValueError , sort_retention_periods , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertRaises ( ValueError , sort_retention_periods , [ - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> class TestPeriod ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mox = mox . Mox ( ) <EOL> self . user = User . objects . create_user ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> self . activity , _ = DailyActivity . objects . stamp ( user = self . user , <EOL> site = get_current_site ( ) , <EOL> medium = '<STR_LIT>' ) <EOL> self . activity . days = <NUM_LIT> <EOL> self . activity . save ( ) <EOL> def test_create ( self ) : <EOL> self . assertRaises ( ValueError , Period , <EOL> cohort = None , start_day = <NUM_LIT:1> , end_day = <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , Period , <EOL> cohort = None , start_day = <NUM_LIT:30> , end_day = <NUM_LIT:1> ) <EOL> def test_length ( self ) : <EOL> period = Period ( cohort = None , start_day = <NUM_LIT:1> , end_day = <NUM_LIT:30> ) <EOL> self . assertEqual ( period . length ( ) , <NUM_LIT> ) <EOL> def test_activities ( self ) : <EOL> cohort = self . mox . CreateMockAnything ( ) <EOL> cohort . users = User . objects . all ( ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( <EOL> list ( Period ( cohort , start_day = <NUM_LIT:1> , end_day = <NUM_LIT:30> ) . activities ) , <EOL> [ self . activity ] <EOL> ) <EOL> self . assertEqual ( <EOL> list ( Period ( cohort , start_day = <NUM_LIT:30> , end_day = <NUM_LIT> ) . activities ) , <EOL> [ ] <EOL> ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_users ( self ) : <EOL> cohort = self . mox . CreateMockAnything ( ) <EOL> cohort . users = User . objects . all ( ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( list ( Period ( cohort , start_day = <NUM_LIT:1> , end_day = <NUM_LIT:30> ) . users ) , <EOL> [ self . user ] ) <EOL> self . assertEqual ( list ( Period ( cohort , start_day = <NUM_LIT:30> , end_day = <NUM_LIT> ) . users ) , <EOL> [ ] ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_periods ( self ) : <EOL> cohort = self . mox . CreateMockAnything ( ) <EOL> cohort . users = User . objects . all ( ) <EOL> self . mox . ReplayAll ( ) <EOL> periods = list ( Period . periods ( cohort , [ <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT> ] ) ) <EOL> for period in periods : <EOL> self . assertEqual ( period . cohort , cohort ) <EOL> self . assertEqual ( periods [ <NUM_LIT:0> ] . start_day , <NUM_LIT:1> ) <EOL> self . assertEqual ( periods [ <NUM_LIT:0> ] . end_day , <NUM_LIT:30> ) <EOL> self . assertEqual ( periods [ <NUM_LIT:1> ] . start_day , <NUM_LIT:30> ) <EOL> self . assertEqual ( periods [ <NUM_LIT:1> ] . end_day , <NUM_LIT> ) <EOL> self . assertEqual ( periods [ <NUM_LIT:2> ] . start_day , <NUM_LIT> ) <EOL> self . assertEqual ( periods [ <NUM_LIT:2> ] . end_day , <NUM_LIT> ) <EOL> self . mox . VerifyAll ( ) <EOL> class TestCohort ( TestCase ) : <EOL> def setUp ( self ) : <EOL> midnight = time ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> yesterday = datetime . combine ( date . today ( ) - timedelta ( days = <NUM_LIT:1> ) , midnight ) <EOL> day_before = datetime . combine ( yesterday - timedelta ( days = <NUM_LIT:1> ) , midnight ) <EOL> self . user1 = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . user1 . date_joined = day_before <EOL> self . user1 . save ( ) <EOL> self . user2 = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . user2 . date_joined = yesterday <EOL> self . user2 . save ( ) <EOL> def test_create ( self ) : <EOL> today = date . today ( ) <EOL> yesterday = today - timedelta ( days = <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , <EOL> Cohort , start_date = today , end_date = yesterday , <EOL> retention_periods = [ ] ) <EOL> cohort = Cohort ( start_date = yesterday , end_date = today , <EOL> retention_periods = [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( cohort . start_date , yesterday ) <EOL> self . assertEqual ( cohort . end_date , today ) <EOL> self . assertEqual ( cohort . retention_periods , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_periods ( self ) : <EOL> yesterday = date . today ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> day_before = yesterday - timedelta ( days = <NUM_LIT:1> ) <EOL> cohort = Cohort ( start_date = day_before , end_date = yesterday , <EOL> retention_periods = [ <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> ] ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:0> ] . start_day , <NUM_LIT:1> ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:0> ] . end_day , <NUM_LIT:20> ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:1> ] . start_day , <NUM_LIT:20> ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:1> ] . end_day , <NUM_LIT> ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:2> ] . start_day , <NUM_LIT> ) <EOL> self . assertEqual ( cohort . periods [ <NUM_LIT:2> ] . end_day , <NUM_LIT> ) <EOL> def test_users ( self ) : <EOL> yesterday = date . today ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> day_before = yesterday - timedelta ( days = <NUM_LIT:1> ) <EOL> cohort = Cohort ( start_date = day_before , end_date = yesterday , <EOL> retention_periods = [ ] ) <EOL> self . assertEqual ( set ( cohort . users ) , set ( [ self . user1 , self . user2 ] ) ) <EOL> cohort = Cohort ( start_date = yesterday , end_date = yesterday , <EOL> retention_periods = [ ] ) <EOL> self . assertEqual ( set ( cohort . users ) , set ( [ self . user2 ] ) ) <EOL> def test_cohorts ( self ) : <EOL> yesterday = date . today ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> retention_periods = [ <NUM_LIT:1> ] <EOL> cohorts = Cohort . cohorts ( end_date = date . today ( ) , length = <NUM_LIT:2> , <EOL> retention_periods = retention_periods ) <EOL> cohort = cohorts . next ( ) <EOL> self . assertEqual ( set ( cohort . users ) , set ( [ self . user1 , self . user2 ] ) ) <EOL> self . assertEqual ( cohort . start_date , yesterday - timedelta ( days = <NUM_LIT:1> ) ) <EOL> self . assertEqual ( cohort . end_date , yesterday ) <EOL> self . assertEqual ( cohort . retention_periods , retention_periods ) <EOL> cohort = cohorts . next ( ) <EOL> self . assertEqual ( set ( cohort . users ) , set ( [ self . user1 ] ) ) <EOL> self . assertEqual ( cohort . start_date , yesterday - timedelta ( days = <NUM_LIT:2> ) ) <EOL> self . assertEqual ( cohort . end_date , yesterday - timedelta ( days = <NUM_LIT:1> ) ) <EOL> self . assertEqual ( cohort . retention_periods , retention_periods ) <EOL> cohort = cohorts . next ( ) <EOL> self . assertEqual ( set ( cohort . users ) , set ( ) ) <EOL> self . assertEqual ( cohort . start_date , yesterday - timedelta ( days = <NUM_LIT:3> ) ) <EOL> self . assertEqual ( cohort . end_date , yesterday - timedelta ( days = <NUM_LIT:2> ) ) <EOL> self . assertEqual ( cohort . retention_periods , retention_periods ) </s>
