Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 5b06ac7

Browse files
author
Tom Galloway
committed
More profile tests created.
1 parent 07eaaa1 commit 5b06ac7

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

market/profile.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,30 @@ def update(self, user_info):
3737

3838
def add_social_account(self, account_type, username, proof=None):
3939
s = self.profile.SocialAccount()
40-
for social_account in self.profile.social:
41-
if social_account.type == s.SocialType.Value(account_type.upper()):
42-
self.profile.social.remove(social_account)
43-
s.type = s.SocialType.Value(account_type.upper())
44-
s.username = username
45-
if proof:
46-
s.proof_url = proof
47-
self.profile.social.extend([s])
40+
try:
41+
self._remove_social_if_found(account_type)
42+
s.type = s.SocialType.Value(account_type.upper())
43+
s.username = username
44+
if proof:
45+
s.proof_url = proof
46+
self.profile.social.extend([s])
47+
except ValueError:
48+
return
4849
self.db.set_proto(self.profile.SerializeToString())
4950

5051
def remove_social_account(self, account_type):
52+
try:
53+
self._remove_social_if_found(account_type)
54+
except ValueError:
55+
return
56+
self.db.set_proto(self.profile.SerializeToString())
57+
58+
def _remove_social_if_found(self, account_type):
5159
s = self.profile.SocialAccount()
60+
st = s.SocialType.Value(account_type.upper())
5261
for social_account in self.profile.social:
53-
if social_account.type == s.SocialType.Value(account_type.upper()):
62+
if social_account.type == st:
5463
self.profile.social.remove(social_account)
55-
self.db.set_proto(self.profile.SerializeToString())
5664

5765
def add_pgp_key(self, public_key, signature, guid):
5866
"""

market/tests/test_profile.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def createTestUser(self):
2020
u.name = "test_name"
2121
u.location = 2
2222
u.about = "hello world"
23+
s = u.SocialAccount()
24+
s.username = "test_fb_username"
25+
s.type = s.SocialType.Value("FACEBOOK")
26+
u.social.extend([s])
2327
self.db.ProfileStore().set_proto(u.SerializeToString())
2428

2529
def tearDown(self):
@@ -30,10 +34,79 @@ def test_MarketProfile_get_success(self):
3034
self.assertEqual('test_name', p.name)
3135
self.assertEqual(2, p.location)
3236
self.assertEqual('hello world', p.about)
37+
self.assertEqual(1, len(p.social))
38+
self.assertEqual(1, p.social[0].type)
39+
self.assertEqual('test_fb_username', p.social[0].username)
3340

3441
def test_MarketProfile_remove_field_success(self):
3542
p = Profile(self.db)
3643
p.remove_field("about")
3744
user = p.get()
3845
self.assertEqual('test_name', user.name)
3946
self.assertEqual('', user.about)
47+
48+
def test_MarketProfile_remove_social(self):
49+
p = Profile(self.db)
50+
p.remove_social_account("FACEBOOK")
51+
u = p.get()
52+
self.assertEqual(0, len(u.social))
53+
54+
def test_MarketProfile_remove_lowercase_social(self):
55+
p = Profile(self.db)
56+
p.remove_social_account("facebook")
57+
u = p.get()
58+
self.assertEqual(0, len(u.social))
59+
60+
def test_MarketProfile_remove_social_invalid(self):
61+
p = Profile(self.db)
62+
p.remove_social_account("TEST")
63+
u = p.get()
64+
self.assertEqual(1, len(u.social))
65+
66+
def test_MarketProfile_add_social_no_proof(self):
67+
p = Profile(self.db)
68+
p.add_social_account("TWITTER", "test_twitter_username")
69+
u = p.get()
70+
self.assertEqual(2, len(u.social))
71+
self.assertEqual(1, u.social[0].type)
72+
self.assertEqual('test_fb_username', u.social[0].username)
73+
self.assertEqual(2, u.social[1].type)
74+
self.assertEqual('test_twitter_username', u.social[1].username)
75+
76+
def test_MarketProfile_replace_social_no_proof(self):
77+
p = Profile(self.db)
78+
p.add_social_account("FACEBOOK", "test_updated_username")
79+
u = p.get()
80+
self.assertEqual(1, len(u.social))
81+
self.assertEqual(1, u.social[0].type)
82+
self.assertEqual('test_updated_username', u.social[0].username)
83+
84+
def test_MarketProfile_add_social_with_proof(self):
85+
p = Profile(self.db)
86+
p.add_social_account("TWITTER", "test_twitter_username",
87+
"http://test_url")
88+
u = p.get()
89+
self.assertEqual(2, len(u.social))
90+
self.assertEqual(1, u.social[0].type)
91+
self.assertEqual('test_fb_username', u.social[0].username)
92+
self.assertEqual('', u.social[0].proof_url)
93+
self.assertEqual(2, u.social[1].type)
94+
self.assertEqual('test_twitter_username', u.social[1].username)
95+
self.assertEqual('http://test_url', u.social[1].proof_url)
96+
97+
def test_MarketProfile_replace_social_with_proof(self):
98+
p = Profile(self.db)
99+
p.add_social_account("FACEBOOK", "test_updated_username", "http://fb_url")
100+
u = p.get()
101+
self.assertEqual(1, len(u.social))
102+
self.assertEqual(1, u.social[0].type)
103+
self.assertEqual('test_updated_username', u.social[0].username)
104+
self.assertEqual('http://fb_url', u.social[0].proof_url)
105+
106+
def test_MarketProfile_add_social_invalid(self):
107+
p = Profile(self.db)
108+
p.add_social_account("TEST", "test_twitter_username")
109+
u = p.get()
110+
self.assertEqual(1, len(u.social))
111+
self.assertEqual(1, u.social[0].type)
112+
self.assertEqual('test_fb_username', u.social[0].username)

0 commit comments

Comments
 (0)