Coverage for src/tests/test_visible_users.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1from sqlalchemy.sql import func 

2 

3from couchers.models import FriendRelationship, User 

4from couchers.sql import couchers_select as select 

5from tests.test_fixtures import ( # noqa 

6 db, 

7 generate_user, 

8 make_friends, 

9 make_user_block, 

10 make_user_invisible, 

11 session_scope, 

12) 

13 

14 

15class _FakeContext: 

16 def __init__(self, user_id): 

17 self.user_id = user_id 

18 

19 

20# Also tests different ways to make users invisible 

21def test_is_visible_property(db): 

22 user1, token1 = generate_user() 

23 user2, token2 = generate_user() 

24 user3, token3 = generate_user() 

25 user4, token4 = generate_user() 

26 user5, token5 = generate_user(delete_user=True) 

27 

28 with session_scope() as session: 

29 session.execute(select(User).where(User.id == user2.id)).scalar_one().is_banned = True 

30 session.execute(select(User).where(User.id == user3.id)).scalar_one().is_deleted = True 

31 

32 make_user_invisible(user4.id) 

33 

34 visible_users = session.execute(select(User).where(User.is_visible)).scalars().all() 

35 assert len(visible_users) == 1 

36 

37 

38def test_select_dot_where_users_visible(db): 

39 user1, token1 = generate_user() 

40 user2, token2 = generate_user(delete_user=True) 

41 user3, token3 = generate_user() 

42 user4, token4 = generate_user() 

43 

44 make_user_block(user1, user3) 

45 make_user_block(user4, user1) 

46 

47 context = _FakeContext(user1.id) 

48 with session_scope() as session: 

49 assert session.execute(select(func.count()).select_from(User).where_users_visible(context)).scalar_one() == 1 

50 

51 

52def test_select_dot_where_users_column_visible(db): 

53 user1, token1 = generate_user() 

54 user2, token2 = generate_user() 

55 user3, token3 = generate_user() 

56 user4, token4 = generate_user() 

57 user5, token5 = generate_user() 

58 

59 make_friends(user1, user2) 

60 make_friends(user1, user3) 

61 make_friends(user1, user4) 

62 make_friends(user1, user5) 

63 

64 make_user_invisible(user3.id) 

65 make_user_block(user1, user4) 

66 make_user_block(user5, user1) 

67 

68 context = _FakeContext(user1.id) 

69 with session_scope() as session: 

70 assert ( 

71 session.execute( 

72 select(func.count()) 

73 .select_from(FriendRelationship) 

74 .where_users_column_visible(context, FriendRelationship.to_user_id) 

75 ).scalar_one() 

76 == 1 

77 )