Skip to content

Commit 9f64e6e

Browse files
committed
Implemete clean_data functions and update README
1 parent 7112b64 commit 9f64e6e

7 files changed

+134
-42
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ python3 play_tester.py
182182

183183
There is some starter code provided in `play_tester.py`. This code prints the test data that is used for many of the tests. Looking closely at this data can help us think critically about the expected output for given input for each function. Then, calling each function with this data allows us to observe the **actual** output for given input.
184184

185+
## Test Data
186+
187+
We will note that much of the test data for this project is provided by the file `test_constants.py`. As test data gets more and more complex, it is helpful to organize this data in its own file to enhance consistency and readability. Pytest, like many testing libraries, provide a special too for test data called **fixtures**. We will learn about fixtures later in the curriculum.
188+
189+
For the time being, we need to make sure that the data provided to each test is clean and free of any changes that running another test may have introduced. Recall modifying mutable objects section of the [Variables Are References](https://learn-2.galvanize.com/cohorts/2330/blocks/1829/content_files/variables-and-memory/variables-are-references.md#modifying-mutable-objects) lesson. To ensure that the data for each test is storied in a unique place in memory, there are functions implemented in `test_constants.py` that provide clean test data (i.e. `clean_wave_3_data`) by using `copy.deepcopy`.
190+
185191
## Project Directions
186192

187193
This project is designed such that one could puzzle together how to implement this project without many directions. Being able to read tests to understand what is expected of our program is a skill that needs to be developed; programmers often take years to develop this skill competently.

play_tester.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66

77
# import "pretty-print" library
88
import pprint
9-
10-
# use library to print data structures in a nicely formatted way
119
pp = pprint.PrettyPrinter(indent=4)
1210

13-
print("\n-----Wave 02 user_data-----")
14-
pp.pprint(USER_DATA_2)
11+
# play testing section
12+
print("\n-----Wave 01 test data-----")
13+
pp.pprint(HORROR_1)
14+
pp.pprint(FANTASY_1)
15+
pp.pprint(FANTASY_2)
16+
17+
# print("\n-----Wave 02 user_data-----")
18+
# pp.pprint(clean_wave_2_data())
1519

1620
#print("\n-----Wave 03 user_data-----")
17-
#pp.pprint(USER_DATA_3)
21+
#pp.pprint(clean_wave_3_data())
1822

1923
# Wave 04 user data
2024
#print("\n-----Wave 04 user_data-----")
21-
#pp.pprint(USER_DATA_4)
25+
#pp.pprint(clean_wave_4_data())
2226

2327
# Wave 05 user data
2428
#print("\n-----Wave 05 user_data-----")
25-
#pp.pprint(USER_DATA_5)
29+
#pp.pprint(clean_wave_5_data())

tests/test_constants.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,18 @@
162162
FANTASY_2b,
163163
INTRIGUE_1b,
164164
INTRIGUE_2b
165-
]
165+
]
166+
167+
#----Functions that return clean data for each test----
168+
169+
def clean_wave_2_data():
170+
return copy.deepcopy(USER_DATA_2)
171+
172+
def clean_wave_3_data():
173+
return copy.deepcopy(USER_DATA_3)
174+
175+
def clean_wave_4_data():
176+
return copy.deepcopy(USER_DATA_4)
177+
178+
def clean_wave_5_data():
179+
return copy.deepcopy(USER_DATA_5)

tests/test_wave_02.py

+77-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
11
import pytest
22
from viewing_party.party import *
3-
from tests.test_constants import USER_DATA_2
3+
import copy
44

55
@pytest.mark.skip()
66
def test_calculates_watched_average_rating():
77
# Arrange
8-
janes_data = USER_DATA_2
8+
janes_data = {
9+
'watched': [
10+
{
11+
'title': 'The Lord of the Functions: The Fellowship of the Function',
12+
'genre': 'Fantasy',
13+
'rating': 4.8
14+
},
15+
{
16+
'title': 'The Lord of the Functions: The Two Parameters',
17+
'genre': 'Fantasy',
18+
'rating': 4.0
19+
},
20+
{
21+
'title': 'The Lord of the Functions: The Return of the Value',
22+
'genre': 'Fantasy',
23+
'rating': 4.0
24+
},
25+
{
26+
'title': 'The JavaScript and the React',
27+
'genre': 'Action',
28+
'rating': 2.2
29+
},
30+
{
31+
'title': 'Recursion',
32+
'genre': 'Intrigue',
33+
'rating': 2.0
34+
},
35+
{
36+
'title': 'Instructor Student TA Manager',
37+
'genre': 'Intrigue',
38+
'rating': 4.5
39+
}
40+
]
41+
}
42+
43+
janes_data_before = copy.deepcopy(janes_data)
944

1045
# Act
1146
average = get_watched_avg_rating(janes_data)
1247

1348
# Assert
1449
assert average == pytest.approx(3.58333)
15-
assert janes_data is USER_DATA_2
50+
assert janes_data == janes_data_before
1651

1752
@pytest.mark.skip()
1853
def test_empty_watched_average_rating_is_zero():
1954
# Arrange
2055
janes_data = {
21-
"watched": [
22-
]
56+
"watched": []
2357
}
2458

2559
# Act
@@ -31,14 +65,48 @@ def test_empty_watched_average_rating_is_zero():
3165
@pytest.mark.skip()
3266
def test_most_watched_genre():
3367
# Arrange
34-
janes_data = USER_DATA_2
68+
janes_data = {
69+
'watched': [
70+
{
71+
'title': 'The Lord of the Functions: The Fellowship of the Function',
72+
'genre': 'Fantasy',
73+
'rating': 4.8
74+
},
75+
{
76+
'title': 'The Lord of the Functions: The Two Parameters',
77+
'genre': 'Fantasy',
78+
'rating': 4.0
79+
},
80+
{
81+
'title': 'The Lord of the Functions: The Return of the Value',
82+
'genre': 'Fantasy',
83+
'rating': 4.0
84+
},
85+
{
86+
'title': 'The JavaScript and the React',
87+
'genre': 'Action',
88+
'rating': 2.2
89+
},
90+
{
91+
'title': 'Recursion',
92+
'genre': 'Intrigue',
93+
'rating': 2.0
94+
},
95+
{
96+
'title': 'Instructor Student TA Manager',
97+
'genre': 'Intrigue',
98+
'rating': 4.5
99+
}
100+
]
101+
}
102+
janes_data_before = copy.deepcopy(janes_data)
35103

36104
# Act
37105
popular_genre = get_most_watched_genre(janes_data)
38106

39107
# Assert
40-
assert popular_genre is "Fantasy"
41-
assert janes_data is USER_DATA_2
108+
assert popular_genre == "Fantasy"
109+
assert janes_data == janes_data_before
42110

43111
@pytest.mark.skip()
44112
def test_genre_is_None_if_empty_watched():
@@ -51,4 +119,4 @@ def test_genre_is_None_if_empty_watched():
51119
popular_genre = get_most_watched_genre(janes_data)
52120

53121
# Assert
54-
assert popular_genre is None
122+
assert popular_genre == None

tests/test_wave_03.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,55 @@
55
@pytest.mark.skip()
66
def test_my_unique_movies():
77
# Arrange
8-
amandas_data = USER_DATA_3
8+
amandas_data = clean_wave_3_data()
99

1010
# Act
1111
amandas_unique_movies = get_unique_watched(amandas_data)
1212

1313
# Arrange
14-
assert len(amandas_unique_movies) is 2
14+
assert len(amandas_unique_movies) == 2
1515
assert FANTASY_2 in amandas_unique_movies
1616
assert INTRIGUE_2 in amandas_unique_movies
17-
assert amandas_data is USER_DATA_3
17+
assert amandas_data == clean_wave_3_data()
1818

1919
@pytest.mark.skip()
2020
def test_my_not_unique_movies():
2121
# Arrange
22-
amandas_data = copy.deepcopy(USER_DATA_3)
22+
amandas_data = clean_wave_3_data()
2323
amandas_data["watched"] = []
2424

2525
# Act
2626
amandas_unique_movies = get_unique_watched(amandas_data)
2727

2828
# Arrange
29-
assert len(amandas_unique_movies) is 0
29+
assert len(amandas_unique_movies) == 0
3030

3131
@pytest.mark.skip()
3232
def test_friends_unique_movies():
3333
# Arrange
34-
amandas_data = USER_DATA_3
34+
amandas_data = clean_wave_3_data()
3535

3636
# Act
3737
friends_unique_movies = get_friends_unique_watched(amandas_data)
3838

3939
# Arrange
40-
assert len(friends_unique_movies) is 3
40+
assert len(friends_unique_movies) == 3
4141
assert INTRIGUE_3 in friends_unique_movies
4242
assert HORROR_1 in friends_unique_movies
4343
assert FANTASY_4 in friends_unique_movies
44-
assert amandas_data is USER_DATA_3
44+
assert amandas_data == clean_wave_3_data()
4545

4646
@pytest.mark.skip()
4747
def test_friends_unique_movies_not_duplicated():
4848
# Arrange
49-
amandas_data = copy.deepcopy(USER_DATA_3)
49+
amandas_data = clean_wave_3_data()
5050
amandas_data["friends"][0]["watched"].append(INTRIGUE_3)
5151

5252
# Act
5353
friends_unique_movies = get_friends_unique_watched(amandas_data)
5454

5555
# Arrange
56-
assert len(friends_unique_movies) is 3
56+
assert len(friends_unique_movies) == 3
5757
assert INTRIGUE_3 in friends_unique_movies
5858
assert HORROR_1 in friends_unique_movies
5959
assert FANTASY_4 in friends_unique_movies
@@ -84,4 +84,4 @@ def test_friends_not_unique_movies():
8484
friends_unique_movies = get_friends_unique_watched(amandas_data)
8585

8686
# Arrange
87-
assert len(friends_unique_movies) is 0
87+
assert len(friends_unique_movies) == 0

tests/test_wave_04.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
@pytest.mark.skip()
66
def test_get_available_friend_rec():
77
# Arrange
8-
amandas_data = USER_DATA_4
8+
amandas_data = clean_wave_4_data()
99

1010
# Act
1111
recommendations = get_available_recs(amandas_data)
1212

1313
# Arrange
14-
assert len(recommendations) is 2
14+
assert len(recommendations) == 2
1515
assert HORROR_1b in recommendations
1616
assert FANTASY_4b in recommendations
17-
assert amandas_data is USER_DATA_4
17+
assert amandas_data == clean_wave_4_data()
1818

1919
@pytest.mark.skip()
2020
def test_no_available_friend_recs():
@@ -36,4 +36,4 @@ def test_no_available_friend_recs():
3636
recommendations = get_available_recs(amandas_data)
3737

3838
# Arrange
39-
assert len(recommendations) is 0
39+
assert len(recommendations) == 0

tests/test_wave_05.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
@pytest.mark.skip()
66
def test_new_genre_rec():
77
# Arrange
8-
sonyas_data = USER_DATA_5
8+
sonyas_data = clean_wave_5_data()
99

1010
# Act
1111
recommendations = get_new_rec_by_genre(sonyas_data)
1212

1313
# Assert
1414
for rec in recommendations:
1515
assert rec not in sonyas_data["watched"]
16-
assert len(recommendations) is 1
16+
assert len(recommendations) == 1
1717
assert FANTASY_4b in recommendations
18-
assert sonyas_data is USER_DATA_5
18+
assert sonyas_data == clean_wave_5_data()
1919

2020
@pytest.mark.skip()
2121
def test_new_genre_rec_from_empty_watched():
@@ -36,7 +36,7 @@ def test_new_genre_rec_from_empty_watched():
3636
recommendations = get_new_rec_by_genre(sonyas_data)
3737

3838
# Assert
39-
assert len(recommendations) is 0
39+
assert len(recommendations) == 0
4040

4141
@pytest.mark.skip()
4242
def test_new_genre_rec_from_empty_friends():
@@ -57,21 +57,21 @@ def test_new_genre_rec_from_empty_friends():
5757
recommendations = get_new_rec_by_genre(sonyas_data)
5858

5959
# Assert
60-
assert len(recommendations) is 0
60+
assert len(recommendations) == 0
6161

6262
@pytest.mark.skip()
6363
def test_unique_rec_from_favorites():
6464
# Arrange
65-
sonyas_data = USER_DATA_5
65+
sonyas_data = clean_wave_5_data()
6666

6767
# Act
6868
recommendations = get_rec_from_favorites(sonyas_data)
6969

7070
# Assert
71-
assert len(recommendations) is 2
71+
assert len(recommendations) == 2
7272
assert FANTASY_2b in recommendations
7373
assert INTRIGUE_2b in recommendations
74-
assert sonyas_data is USER_DATA_5
74+
assert sonyas_data == clean_wave_5_data()
7575

7676
@pytest.mark.skip()
7777
def test_unique_from_empty_favorites():
@@ -92,7 +92,7 @@ def test_unique_from_empty_favorites():
9292
recommendations = get_new_rec_by_genre(sonyas_data)
9393

9494
# Assert
95-
assert len(recommendations) is 0
95+
assert len(recommendations) == 0
9696

9797
@pytest.mark.skip()
9898
def test_new_rec_from_empty_friends():
@@ -113,4 +113,4 @@ def test_new_rec_from_empty_friends():
113113
recommendations = get_new_rec_by_genre(sonyas_data)
114114

115115
# Assert
116-
assert len(recommendations) is 0
116+
assert len(recommendations) == 0

0 commit comments

Comments
 (0)