- Timestamp:
- Mar 5, 2019, 11:45:32 PM (5 years ago)
- Branches:
- current
- Children:
- aa6dcaf
- Parents:
- 35953eb
- Location:
- ow/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/tests/models/test_user.py
r35953eb r778d53d 81 81 assert list(root['john'].workout_ids()) == ['1', '2', '3'] 82 82 assert root['john'].num_workouts == len(workouts) 83 84 def test_favorite_sport(self, root): 85 assert root['john'].favorite_sport is None 86 # add a cycling workout 87 workout = Workout( 88 sport='cycling', 89 start=datetime.now(timezone.utc), 90 duration=timedelta(minutes=120), 91 distance=66, 92 ) 93 root['john'].add_workout(workout) 94 assert root['john'].favorite_sport == 'cycling' 95 # add a running workout, both sports have same amount of workouts, 96 # favorite is picked up reversed alphabetically 97 workout = Workout( 98 sport='running', 99 start=datetime.now(timezone.utc), 100 duration=timedelta(minutes=45), 101 distance=5, 102 ) 103 root['john'].add_workout(workout) 104 assert root['john'].favorite_sport == 'running' 105 # add another cycling workout, now that is the favorite sport 106 workout = Workout( 107 sport='cycling', 108 start=datetime.now(timezone.utc), 109 duration=timedelta(minutes=60), 110 distance=30, 111 ) 112 root['john'].add_workout(workout) 113 assert root['john'].favorite_sport == 'cycling' 114 115 def test_activity_sports(self, root): 116 assert root['john'].activity_sports == [] 117 workout = Workout( 118 sport='cycling', 119 start=datetime.now(timezone.utc), 120 duration=timedelta(minutes=120), 121 distance=66, 122 ) 123 root['john'].add_workout(workout) 124 assert root['john'].activity_sports == ['cycling'] 125 workout = Workout( 126 sport='running', 127 start=datetime.now(timezone.utc), 128 duration=timedelta(minutes=45), 129 distance=5, 130 ) 131 root['john'].add_workout(workout) 132 assert root['john'].activity_sports == ['cycling', 'running'] 133 134 def test_activity_years(self, root): 135 assert root['john'].activity_years == [] 136 workout = Workout( 137 sport='cycling', 138 start=datetime.now(timezone.utc), 139 duration=timedelta(minutes=120), 140 distance=66, 141 ) 142 root['john'].add_workout(workout) 143 assert root['john'].activity_years == [datetime.now(timezone.utc).year] 144 workout = Workout( 145 sport='running', 146 start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc), 147 duration=timedelta(minutes=45), 148 distance=5, 149 ) 150 root['john'].add_workout(workout) 151 assert root['john'].activity_years == [ 152 datetime.now(timezone.utc).year, 153 2018 154 ] 155 156 def test_activity_months(self, root): 157 # we have to pass a year parameter 158 with pytest.raises(TypeError): 159 root['john'].activity_months() 160 now = datetime.now(timezone.utc) 161 assert root['john'].activity_months(now.year) == [] 162 workout = Workout( 163 sport='cycling', 164 start=datetime.now(timezone.utc), 165 duration=timedelta(minutes=120), 166 distance=66, 167 ) 168 root['john'].add_workout(workout) 169 assert root['john'].activity_months(now.year) == [now.month] 170 assert root['john'].activity_months(now.year-1) == [] 171 assert root['john'].activity_months(now.year+1) == [] 172 workout = Workout( 173 sport='running', 174 start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc), 175 duration=timedelta(minutes=45), 176 distance=5, 177 ) 178 root['john'].add_workout(workout) 179 assert root['john'].activity_months(now.year) == [now.month] 180 assert root['john'].activity_months(2018) == [11] 181 assert root['john'].activity_months(now.year+1) == [] 83 182 84 183 def test_activity_dates_tree(self, root): … … 552 651 else: 553 652 assert stats == expected_no_stats_per_week 653 654 def test_sport_totals(self, root): 655 # user has no workouts, so no totals 656 assert root['john'].sport_totals() == { 657 'workouts': 0, 658 'time': timedelta(0), 659 'distance': Decimal(0), 660 'elevation': Decimal(0), 661 } 662 # add a cycling workout happening now 663 workout = Workout( 664 sport='cycling', 665 start=datetime.now(timezone.utc), 666 duration=timedelta(minutes=120), 667 distance=66, 668 ) 669 root['john'].add_workout(workout) 670 # only one workout, one sport, so the default will show totals 671 # for that sport 672 assert root['john'].sport_totals() == { 673 'workouts': 1, 674 'time': timedelta(minutes=120), 675 'distance': Decimal(66), 676 'elevation': Decimal(0), 677 } 678 # Add a running workout 679 workout = Workout( 680 sport='running', 681 start=datetime(2018, 11, 25, 10, 00, tzinfo=timezone.utc), 682 duration=timedelta(minutes=45), 683 distance=5, 684 ) 685 root['john'].add_workout(workout) 686 # the favorite sport is running now 687 assert root['john'].sport_totals() == { 688 'workouts': 1, 689 'time': timedelta(minutes=45), 690 'distance': Decimal(5), 691 'elevation': Decimal(0), 692 } 693 # but we can get the totals for cycling too 694 assert root['john'].sport_totals('cycling') == { 695 'workouts': 1, 696 'time': timedelta(minutes=120), 697 'distance': Decimal(66), 698 'elevation': Decimal(0), 699 } 700 # adding a new cycling workout, in a different year 701 workout = Workout( 702 sport='cycling', 703 start=datetime(2017, 11, 25, 10, 00, tzinfo=timezone.utc), 704 duration=timedelta(minutes=60), 705 distance=32, 706 ) 707 root['john'].add_workout(workout) 708 # now cycling is the favorite sport 709 assert root['john'].sport_totals() == { 710 'workouts': 2, 711 'time': timedelta(minutes=180), 712 'distance': Decimal(98), 713 'elevation': Decimal(0), 714 } 715 # but we can get running stats too 716 assert root['john'].sport_totals('running') == { 717 'workouts': 1, 718 'time': timedelta(minutes=45), 719 'distance': Decimal(5), 720 'elevation': Decimal(0), 721 } 722 # there are no running activities for 2016 723 assert root['john'].sport_totals('running', 2016) == { 724 'workouts': 0, 725 'time': timedelta(0), 726 'distance': Decimal(0), 727 'elevation': Decimal(0), 728 } 729 # and not activities for cycling in 2016 neither 730 assert root['john'].sport_totals('cycling', 2016) == { 731 'workouts': 0, 732 'time': timedelta(0), 733 'distance': Decimal(0), 734 'elevation': Decimal(0), 735 } 736 # and we can get the separate totals for cycling in different years 737 year = datetime.now(timezone.utc).year 738 assert root['john'].sport_totals('cycling', year) == { 739 'workouts': 1, 740 'time': timedelta(minutes=120), 741 'distance': Decimal(66), 742 'elevation': Decimal(0), 743 } 744 assert root['john'].sport_totals('cycling', 2017) == { 745 'workouts': 1, 746 'time': timedelta(minutes=60), 747 'distance': Decimal(32), 748 'elevation': Decimal(0), 749 } -
ow/tests/views/test_user.py
r35953eb r778d53d 46 46 start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), 47 47 duration=timedelta(minutes=60), 48 distance=30 48 distance=30, 49 sport='cycling' 49 50 ) 50 51 john.add_workout(workout) … … 413 414 # profile page for the current day (no workouts avalable) 414 415 response = user_views.profile(john, request) 415 assert len(response.keys()) == 6416 assert len(response.keys()) == 7 416 417 current_month = datetime.now(timezone.utc).strftime('%Y-%m') 417 418 assert response['user'] == john … … 425 426 'elevation': Decimal(0) 426 427 } 428 assert response['profile_stats'] == { 429 'sports': ['cycling'], 430 'years': [2015], 431 'current_year': datetime.now(timezone.utc).year, 432 'current_sport': 'cycling' 433 } 427 434 # profile page for a previous date, that has workouts 428 435 request.GET['year'] = 2015 429 436 request.GET['month'] = 6 430 437 response = user_views.profile(john, request) 431 assert len(response.keys()) == 6438 assert len(response.keys()) == 7 432 439 assert response['user'] == john 433 440 assert response['user_gender'] == 'Robot' … … 446 453 request.GET['week'] = 25 447 454 response = user_views.profile(john, request) 448 assert len(response.keys()) == 6455 assert len(response.keys()) == 7 449 456 assert response['user'] == john 450 457 assert response['user_gender'] == 'Robot' … … 462 469 request.GET['week'] = 26 463 470 response = user_views.profile(john, request) 464 assert len(response.keys()) == 6471 assert len(response.keys()) == 7 465 472 assert response['user'] == john 466 473 assert response['user_gender'] == 'Robot'
Note: See TracChangeset
for help on using the changeset viewer.