This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_db_utils.py
147 lines (120 loc) · 3.97 KB
/
test_db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# pylint: disable-all
import unittest
from unittest.mock import patch, MagicMock
from werkzeug.datastructures import MultiDict
from db_utils import get_channels, get_ads
class MockAccountClass:
def __init__(self, id, username, password, email, channel_owner):
self.id: int = id
self.username: str = username
self.password = password
self.email: str = email
self.channel_owner: int = channel_owner
class MockChannelClass:
def __init__(
self,
id,
owner_id,
show_channel,
channel_name,
subscribers,
topics,
preferred_reward,
):
self.id: int = id
self.owner_id: int = owner_id
self.show_channel: bool = show_channel
self.channel_name: str = channel_name
self.subscribers: int = subscribers
self.topics: str = topics
self.preferred_reward: int = preferred_reward
class MockAdClass:
def __init__(self, id, creator_id, title, topics, text, reward, show_in_list):
self.id: int = id
self.creator_id: int = creator_id
self.title: str = title
self.topics: str = topics
self.text: str = text
self.reward: int = reward
self.show_in_list: bool = show_in_list
# initialize accounts for Account.querry.all()
accounts = []
for i in range(5):
accounts.append(
MockAccountClass(i, "user" str(i), i, "user" str(i) "@test.com", True)
)
# initialize channels for Channel.query.filter_by(show_channel=True).all()
channels = []
for i in range(5):
channels.append(
MockChannelClass(
i,
i,
True,
"channel" str(i),
i,
"topic" str(i) ",topic" str(i 1),
i,
)
)
# initialize ads for Ad.query.filter_by(show_in_list=True).all()
ads = []
for i in range(5):
ads.append(
MockAdClass(
i,
i,
"title" str(i),
"topic" str(i) ",topic" str(i 1),
"text" str(i),
i,
True,
)
)
class Database_test(unittest.TestCase):
@patch("db_utils.Account")
@patch("db_utils.Channel")
def test_get_channels(self, mock_Channel, mock_Account):
self.accounts = accounts
self.channels = channels
expected_channels = []
for i in range(5):
expected_channels.append(
{
"id": i,
"ownerName": "user" str(i),
"channelName": "channel" str(i),
"subscribers": i,
"topics": "topic" str(i) ", topic" str(i 1),
"preferredReward": i,
}
)
mock_Account.query.all.return_value = self.accounts
mock_Channel.query.filter_by().all.return_value = self.channels
valid_args = MultiDict({"for": "channelsPage"})
# get_channels is supposed to return expected_channels output
self.assertEqual(get_channels(valid_args), expected_channels)
@patch("db_utils.Account")
@patch("db_utils.Ad")
def test_get_ads(self, mock_Ad, mock_Account):
self.accounts = accounts
self.ads = ads
expected_ads = []
for i in range(5):
expected_ads.append(
{
"id": i,
"creatorName": "user" str(i),
"title": "title" str(i),
"topics": "topic" str(i) ", topic" str(i 1),
"text": "text" str(i),
"reward": i,
}
)
mock_Account.query.all.return_value = self.accounts
mock_Ad.query.filter_by().all.return_value = self.ads
# get_ads is supposed to return expected_ads output
valid_args = MultiDict({"for": "adsPage"})
self.assertEqual(get_ads(valid_args), expected_ads)
if __name__ == "__main__":
unittest.main()