askrobots / dbbasic-follows
Public
Follows, friends and connection suggestions for DBBasic apps: a social graph module with an admin interface.
test_follows.py · 13.9 KB · 434 lines
| 1 | """Tests for dbbasic-follows""" |
| 2 | |
| 3 | import pytest |
| 4 | from pathlib import Path |
| 5 | import tempfile |
| 6 | import shutil |
| 7 | from dbbasic_follows import Follows |
| 8 | |
| 9 | |
| 10 | @pytest.fixture |
| 11 | def temp_data_dir(): |
| 12 | """Create temporary data directory for tests""" |
| 13 | temp_dir = tempfile.mkdtemp() |
| 14 | yield temp_dir |
| 15 | shutil.rmtree(temp_dir) |
| 16 | |
| 17 | |
| 18 | @pytest.fixture |
| 19 | def follows(temp_data_dir): |
| 20 | """Create Follows instance with temp data dir""" |
| 21 | return Follows(data_dir=temp_data_dir) |
| 22 | |
| 23 | |
| 24 | class TestFollowOperations: |
| 25 | """Test core follow/unfollow operations""" |
| 26 | |
| 27 | def test_follow(self, follows): |
| 28 | """Test creating a follow relationship""" |
| 29 | result = follows.follow(follower_id=1, followee_id=2) |
| 30 | assert result is True |
| 31 | assert follows.is_following(1, 2) is True |
| 32 | |
| 33 | def test_follow_duplicate(self, follows): |
| 34 | """Test that duplicate follows return False""" |
| 35 | follows.follow(1, 2) |
| 36 | result = follows.follow(1, 2) |
| 37 | assert result is False |
| 38 | |
| 39 | def test_follow_self(self, follows): |
| 40 | """Test that users cannot follow themselves""" |
| 41 | with pytest.raises(ValueError, match="Cannot follow yourself"): |
| 42 | follows.follow(1, 1) |
| 43 | |
| 44 | def test_unfollow(self, follows): |
| 45 | """Test unfollowing a user""" |
| 46 | follows.follow(1, 2) |
| 47 | result = follows.unfollow(1, 2) |
| 48 | assert result is True |
| 49 | assert follows.is_following(1, 2) is False |
| 50 | |
| 51 | def test_unfollow_nonexistent(self, follows): |
| 52 | """Test unfollowing when not following returns False""" |
| 53 | result = follows.unfollow(1, 2) |
| 54 | assert result is False |
| 55 | |
| 56 | def test_is_following_false(self, follows): |
| 57 | """Test is_following returns False when not following""" |
| 58 | assert follows.is_following(1, 2) is False |
| 59 | |
| 60 | |
| 61 | class TestFriendRelationships: |
| 62 | """Test friend relationship detection""" |
| 63 | |
| 64 | def test_are_friends_both_follow(self, follows): |
| 65 | """Test that mutual follows create friendship""" |
| 66 | follows.follow(1, 2) |
| 67 | follows.follow(2, 1) |
| 68 | assert follows.are_friends(1, 2) is True |
| 69 | |
| 70 | def test_are_friends_one_way(self, follows): |
| 71 | """Test that one-way follow is not friendship""" |
| 72 | follows.follow(1, 2) |
| 73 | assert follows.are_friends(1, 2) is False |
| 74 | |
| 75 | def test_are_friends_no_relationship(self, follows): |
| 76 | """Test that no relationship is not friendship""" |
| 77 | assert follows.are_friends(1, 2) is False |
| 78 | |
| 79 | def test_are_friends_symmetric(self, follows): |
| 80 | """Test that friendship is symmetric""" |
| 81 | follows.follow(1, 2) |
| 82 | follows.follow(2, 1) |
| 83 | assert follows.are_friends(1, 2) is True |
| 84 | assert follows.are_friends(2, 1) is True |
| 85 | |
| 86 | |
| 87 | class TestGetRelationships: |
| 88 | """Test retrieving followers, following, friends""" |
| 89 | |
| 90 | def test_get_followers(self, follows): |
| 91 | """Test getting list of followers""" |
| 92 | follows.follow(1, 3) |
| 93 | follows.follow(2, 3) |
| 94 | follows.follow(4, 3) |
| 95 | |
| 96 | followers = follows.get_followers(user_id=3) |
| 97 | follower_ids = [f['follower_id'] for f in followers] |
| 98 | |
| 99 | assert len(followers) == 3 |
| 100 | assert set(follower_ids) == {1, 2, 4} |
| 101 | |
| 102 | def test_get_followers_empty(self, follows): |
| 103 | """Test getting followers when none exist""" |
| 104 | followers = follows.get_followers(user_id=1) |
| 105 | assert followers == [] |
| 106 | |
| 107 | def test_get_followers_with_limit(self, follows): |
| 108 | """Test getting followers with limit""" |
| 109 | for i in range(10): |
| 110 | follows.follow(i, 100) |
| 111 | |
| 112 | followers = follows.get_followers(user_id=100, limit=5) |
| 113 | assert len(followers) == 5 |
| 114 | |
| 115 | def test_get_following(self, follows): |
| 116 | """Test getting list of following""" |
| 117 | follows.follow(1, 2) |
| 118 | follows.follow(1, 3) |
| 119 | follows.follow(1, 4) |
| 120 | |
| 121 | following = follows.get_following(user_id=1) |
| 122 | followee_ids = [f['followee_id'] for f in following] |
| 123 | |
| 124 | assert len(following) == 3 |
| 125 | assert set(followee_ids) == {2, 3, 4} |
| 126 | |
| 127 | def test_get_following_empty(self, follows): |
| 128 | """Test getting following when none exist""" |
| 129 | following = follows.get_following(user_id=1) |
| 130 | assert following == [] |
| 131 | |
| 132 | def test_get_following_with_limit(self, follows): |
| 133 | """Test getting following with limit""" |
| 134 | for i in range(10): |
| 135 | follows.follow(1, i + 100) |
| 136 | |
| 137 | following = follows.get_following(user_id=1, limit=5) |
| 138 | assert len(following) == 5 |
| 139 | |
| 140 | def test_get_friends(self, follows): |
| 141 | """Test getting list of friends""" |
| 142 | # Create mutual follows (friends) |
| 143 | follows.follow(1, 2) |
| 144 | follows.follow(2, 1) |
| 145 | follows.follow(1, 3) |
| 146 | follows.follow(3, 1) |
| 147 | |
| 148 | # Create one-way follow (not friend) |
| 149 | follows.follow(1, 4) |
| 150 | |
| 151 | friends = follows.get_friends(user_id=1) |
| 152 | assert len(friends) == 2 |
| 153 | assert set(friends) == {2, 3} |
| 154 | |
| 155 | def test_get_friends_empty(self, follows): |
| 156 | """Test getting friends when none exist""" |
| 157 | follows.follow(1, 2) # One-way only |
| 158 | friends = follows.get_friends(user_id=1) |
| 159 | assert friends == [] |
| 160 | |
| 161 | def test_get_friends_with_limit(self, follows): |
| 162 | """Test getting friends with limit""" |
| 163 | for i in range(10): |
| 164 | follows.follow(1, i + 100) |
| 165 | follows.follow(i + 100, 1) |
| 166 | |
| 167 | friends = follows.get_friends(user_id=1, limit=5) |
| 168 | assert len(friends) == 5 |
| 169 | |
| 170 | |
| 171 | class TestCounts: |
| 172 | """Test count methods""" |
| 173 | |
| 174 | def test_get_follower_count(self, follows): |
| 175 | """Test counting followers""" |
| 176 | follows.follow(1, 10) |
| 177 | follows.follow(2, 10) |
| 178 | follows.follow(3, 10) |
| 179 | |
| 180 | assert follows.get_follower_count(10) == 3 |
| 181 | |
| 182 | def test_get_follower_count_zero(self, follows): |
| 183 | """Test counting followers when zero""" |
| 184 | assert follows.get_follower_count(1) == 0 |
| 185 | |
| 186 | def test_get_following_count(self, follows): |
| 187 | """Test counting following""" |
| 188 | follows.follow(1, 2) |
| 189 | follows.follow(1, 3) |
| 190 | follows.follow(1, 4) |
| 191 | |
| 192 | assert follows.get_following_count(1) == 3 |
| 193 | |
| 194 | def test_get_following_count_zero(self, follows): |
| 195 | """Test counting following when zero""" |
| 196 | assert follows.get_following_count(1) == 0 |
| 197 | |
| 198 | def test_get_friend_count(self, follows): |
| 199 | """Test counting friends""" |
| 200 | follows.follow(1, 2) |
| 201 | follows.follow(2, 1) |
| 202 | follows.follow(1, 3) |
| 203 | follows.follow(3, 1) |
| 204 | follows.follow(1, 4) # One-way, not a friend |
| 205 | |
| 206 | assert follows.get_friend_count(1) == 2 |
| 207 | |
| 208 | def test_get_friend_count_zero(self, follows): |
| 209 | """Test counting friends when zero""" |
| 210 | assert follows.get_friend_count(1) == 0 |
| 211 | |
| 212 | |
| 213 | class TestSuggestions: |
| 214 | """Test suggestion algorithms""" |
| 215 | |
| 216 | def test_suggest_follows_second_degree(self, follows): |
| 217 | """Test follow suggestions from 2nd degree connections""" |
| 218 | # User 1 follows User 2 |
| 219 | follows.follow(1, 2) |
| 220 | |
| 221 | # User 2 follows Users 3, 4, 5 |
| 222 | follows.follow(2, 3) |
| 223 | follows.follow(2, 4) |
| 224 | follows.follow(2, 5) |
| 225 | |
| 226 | # Get suggestions for User 1 |
| 227 | suggestions = follows.suggest_follows(user_id=1, limit=10) |
| 228 | |
| 229 | # Should suggest 3, 4, 5 |
| 230 | suggested_ids = [s['user_id'] for s in suggestions] |
| 231 | assert set(suggested_ids) == {3, 4, 5} |
| 232 | |
| 233 | def test_suggest_follows_excludes_self(self, follows): |
| 234 | """Test that suggestions don't include self""" |
| 235 | follows.follow(1, 2) |
| 236 | follows.follow(2, 1) # Creates circular reference |
| 237 | |
| 238 | suggestions = follows.suggest_follows(user_id=1, limit=10) |
| 239 | suggested_ids = [s['user_id'] for s in suggestions] |
| 240 | |
| 241 | assert 1 not in suggested_ids |
| 242 | |
| 243 | def test_suggest_follows_excludes_already_following(self, follows): |
| 244 | """Test that suggestions exclude users already followed""" |
| 245 | follows.follow(1, 2) |
| 246 | follows.follow(1, 3) # Already following 3 |
| 247 | follows.follow(2, 3) |
| 248 | |
| 249 | suggestions = follows.suggest_follows(user_id=1, limit=10) |
| 250 | suggested_ids = [s['user_id'] for s in suggestions] |
| 251 | |
| 252 | assert 3 not in suggested_ids |
| 253 | |
| 254 | def test_suggest_follows_ranking(self, follows): |
| 255 | """Test that suggestions are ranked by mutual connections""" |
| 256 | # User 1 follows 2, 3, 4 |
| 257 | follows.follow(1, 2) |
| 258 | follows.follow(1, 3) |
| 259 | follows.follow(1, 4) |
| 260 | |
| 261 | # Users 2, 3, 4 all follow User 10 (score: 3) |
| 262 | follows.follow(2, 10) |
| 263 | follows.follow(3, 10) |
| 264 | follows.follow(4, 10) |
| 265 | |
| 266 | # Only User 2 follows User 11 (score: 1) |
| 267 | follows.follow(2, 11) |
| 268 | |
| 269 | suggestions = follows.suggest_follows(user_id=1, limit=10) |
| 270 | |
| 271 | # User 10 should be ranked higher |
| 272 | assert suggestions[0]['user_id'] == 10 |
| 273 | assert suggestions[0]['score'] == 3 |
| 274 | assert suggestions[1]['user_id'] == 11 |
| 275 | assert suggestions[1]['score'] == 1 |
| 276 | |
| 277 | def test_suggest_follows_with_limit(self, follows): |
| 278 | """Test that limit parameter works""" |
| 279 | follows.follow(1, 2) |
| 280 | for i in range(10): |
| 281 | follows.follow(2, i + 100) |
| 282 | |
| 283 | suggestions = follows.suggest_follows(user_id=1, limit=5) |
| 284 | assert len(suggestions) == 5 |
| 285 | |
| 286 | def test_suggest_follows_empty(self, follows): |
| 287 | """Test suggestions when no follows exist""" |
| 288 | suggestions = follows.suggest_follows(user_id=1, limit=10) |
| 289 | assert suggestions == [] |
| 290 | |
| 291 | def test_suggest_friends(self, follows): |
| 292 | """Test friend suggestions (users who follow you back)""" |
| 293 | # Users 2, 3, 4 follow User 1 |
| 294 | follows.follow(2, 1) |
| 295 | follows.follow(3, 1) |
| 296 | follows.follow(4, 1) |
| 297 | |
| 298 | # User 1 already follows User 2 back (already friends) |
| 299 | follows.follow(1, 2) |
| 300 | |
| 301 | # Get friend suggestions for User 1 |
| 302 | suggestions = follows.suggest_friends(user_id=1, limit=10) |
| 303 | suggested_ids = [s['user_id'] for s in suggestions] |
| 304 | |
| 305 | # Should suggest 3 and 4 (not 2, already friends) |
| 306 | assert set(suggested_ids) == {3, 4} |
| 307 | assert all(s['follows_you'] is True for s in suggestions) |
| 308 | |
| 309 | def test_suggest_friends_ranking(self, follows): |
| 310 | """Test friend suggestions ranked by mutual connections""" |
| 311 | # Users 2, 3 follow User 1 |
| 312 | follows.follow(2, 1) |
| 313 | follows.follow(3, 1) |
| 314 | |
| 315 | # User 1 follows some people |
| 316 | follows.follow(1, 10) |
| 317 | follows.follow(1, 11) |
| 318 | |
| 319 | # User 2 also follows those same people (more mutual connections) |
| 320 | follows.follow(2, 10) |
| 321 | follows.follow(2, 11) |
| 322 | |
| 323 | # User 3 follows only one of them |
| 324 | follows.follow(3, 10) |
| 325 | |
| 326 | suggestions = follows.suggest_friends(user_id=1, limit=10) |
| 327 | |
| 328 | # User 2 should be ranked higher (more mutual connections) |
| 329 | assert suggestions[0]['user_id'] == 2 |
| 330 | assert suggestions[0]['score'] >= suggestions[1]['score'] |
| 331 | |
| 332 | def test_suggest_friends_empty(self, follows): |
| 333 | """Test friend suggestions when none exist""" |
| 334 | suggestions = follows.suggest_friends(user_id=1, limit=10) |
| 335 | assert suggestions == [] |
| 336 | |
| 337 | |
| 338 | class TestStatistics: |
| 339 | """Test statistics methods""" |
| 340 | |
| 341 | def test_get_stats(self, follows): |
| 342 | """Test getting complete stats""" |
| 343 | # Create relationships for User 1 |
| 344 | follows.follow(1, 2) |
| 345 | follows.follow(1, 3) |
| 346 | follows.follow(2, 1) # Friend |
| 347 | follows.follow(4, 1) # Follower only |
| 348 | |
| 349 | stats = follows.get_stats(user_id=1) |
| 350 | |
| 351 | assert stats['followers'] == 2 # Users 2, 4 |
| 352 | assert stats['following'] == 2 # Users 2, 3 |
| 353 | assert stats['friends'] == 1 # User 2 only |
| 354 | |
| 355 | def test_get_stats_empty(self, follows): |
| 356 | """Test stats for user with no relationships""" |
| 357 | stats = follows.get_stats(user_id=1) |
| 358 | |
| 359 | assert stats['followers'] == 0 |
| 360 | assert stats['following'] == 0 |
| 361 | assert stats['friends'] == 0 |
| 362 | |
| 363 | |
| 364 | class TestComplexScenarios: |
| 365 | """Test complex real-world scenarios""" |
| 366 | |
| 367 | def test_complex_social_graph(self, follows): |
| 368 | """Test a complex social graph with multiple relationships""" |
| 369 | # Create a small social network |
| 370 | # User 1: follows 2, 3, 4 |
| 371 | # User 2: follows 1, 3, 5 |
| 372 | # User 3: follows 1, 2 |
| 373 | # User 4: follows 1 |
| 374 | # User 5: follows 2 |
| 375 | |
| 376 | follows.follow(1, 2) |
| 377 | follows.follow(1, 3) |
| 378 | follows.follow(1, 4) |
| 379 | follows.follow(2, 1) |
| 380 | follows.follow(2, 3) |
| 381 | follows.follow(2, 5) |
| 382 | follows.follow(3, 1) |
| 383 | follows.follow(3, 2) |
| 384 | follows.follow(4, 1) |
| 385 | follows.follow(5, 2) |
| 386 | |
| 387 | # Verify User 1's stats |
| 388 | stats = follows.get_stats(1) |
| 389 | assert stats['followers'] == 3 # 2, 3, 4 |
| 390 | assert stats['following'] == 3 # 2, 3, 4 |
| 391 | assert stats['friends'] == 2 # 2, 3 (mutual) |
| 392 | |
| 393 | # Verify User 1's friends |
| 394 | friends = follows.get_friends(1) |
| 395 | assert set(friends) == {2, 3} |
| 396 | |
| 397 | # User 1 should get suggestion for User 5 |
| 398 | # (User 2 follows User 5, and User 1 follows User 2) |
| 399 | suggestions = follows.suggest_follows(1, limit=10) |
| 400 | suggested_ids = [s['user_id'] for s in suggestions] |
| 401 | assert 5 in suggested_ids |
| 402 | |
| 403 | def test_unfollow_breaks_friendship(self, follows): |
| 404 | """Test that unfollowing breaks a friendship""" |
| 405 | # Create friendship |
| 406 | follows.follow(1, 2) |
| 407 | follows.follow(2, 1) |
| 408 | assert follows.are_friends(1, 2) is True |
| 409 | |
| 410 | # One person unfollows |
| 411 | follows.unfollow(1, 2) |
| 412 | |
| 413 | # No longer friends |
| 414 | assert follows.are_friends(1, 2) is False |
| 415 | assert follows.is_following(2, 1) is True # Other direction still exists |
| 416 | |
| 417 | def test_mutual_follow_sequence(self, follows): |
| 418 | """Test the sequence of creating a mutual follow""" |
| 419 | # Initially not following |
| 420 | assert follows.is_following(1, 2) is False |
| 421 | assert follows.is_following(2, 1) is False |
| 422 | assert follows.are_friends(1, 2) is False |
| 423 | |
| 424 | # User 1 follows User 2 |
| 425 | follows.follow(1, 2) |
| 426 | assert follows.is_following(1, 2) is True |
| 427 | assert follows.is_following(2, 1) is False |
| 428 | assert follows.are_friends(1, 2) is False |
| 429 | |
| 430 | # User 2 follows User 1 back |
| 431 | follows.follow(2, 1) |
| 432 | assert follows.is_following(1, 2) is True |
| 433 | assert follows.is_following(2, 1) is True |
| 434 | assert follows.are_friends(1, 2) is True |