A third party ATProto appview
1#!/usr/bin/env python3
2"""Test script to verify SQL generation for post_viewer_states"""
3
4def test_sql_generation():
5 """Test the SQL generation logic"""
6
7 test_cases = [
8 {
9 'name': 'Like only',
10 'like_uri': 'at://did:plc:test/app.bsky.feed.like/abc123',
11 'repost_uri': None,
12 'bookmarked': False
13 },
14 {
15 'name': 'Repost only',
16 'like_uri': None,
17 'repost_uri': 'at://did:plc:test/app.bsky.feed.repost/xyz789',
18 'bookmarked': False
19 },
20 {
21 'name': 'Both like and repost',
22 'like_uri': 'at://did:plc:test/app.bsky.feed.like/abc123',
23 'repost_uri': 'at://did:plc:test/app.bsky.feed.repost/xyz789',
24 'bookmarked': False
25 },
26 {
27 'name': 'Nothing set',
28 'like_uri': None,
29 'repost_uri': None,
30 'bookmarked': False
31 },
32 {
33 'name': 'Bookmarked',
34 'like_uri': None,
35 'repost_uri': None,
36 'bookmarked': True
37 }
38 ]
39
40 for test in test_cases:
41 print(f"\n{'='*60}")
42 print(f"Test Case: {test['name']}")
43 print(f"{'='*60}")
44
45 like_uri = test['like_uri']
46 repost_uri = test['repost_uri']
47 bookmarked = test['bookmarked']
48
49 # Simulate the function logic
50 updates = []
51 insert_params = ['at://post/uri', 'did:plc:viewer']
52 param_idx = 3
53
54 # Build VALUES clause with proper parameter handling
55 like_param = f'${param_idx}' if like_uri else 'NULL'
56 if like_uri:
57 insert_params.append(like_uri)
58 param_idx += 1
59
60 repost_param = f'${param_idx}' if repost_uri else 'NULL'
61 if repost_uri:
62 insert_params.append(repost_uri)
63 param_idx += 1
64
65 bookmarked_value = 'true' if bookmarked else 'false'
66
67 # Build update clauses
68 update_idx = 3
69 if like_uri:
70 updates.append(f'like_uri = ${update_idx}')
71 update_idx += 1
72
73 if repost_uri:
74 updates.append(f'repost_uri = ${update_idx}')
75 update_idx += 1
76
77 if bookmarked:
78 updates.append('bookmarked = true')
79
80 # Generate SQL
81 sql = f"""
82 INSERT INTO post_viewer_states (post_uri, viewer_did, like_uri, repost_uri, bookmarked, thread_muted, reply_disabled, embedding_disabled, pinned)
83 VALUES ($1, $2, {like_param}, {repost_param}, {bookmarked_value}, false, false, false, false)
84 ON CONFLICT (post_uri, viewer_did) DO UPDATE SET
85 {', '.join(updates) if updates else 'like_uri = post_viewer_states.like_uri'}
86 """
87
88 print(f"\nGenerated SQL:")
89 print(sql.strip())
90 print(f"\nParameters: {insert_params}")
91 print(f"Parameter count: {len(insert_params)}")
92
93 # Check for errors
94 if '$NULL' in sql or '$false' in sql or '$true' in sql:
95 print("\n❌ ERROR: Found invalid parameter placeholder!")
96 else:
97 print("\n✅ SQL looks correct!")
98
99if __name__ == '__main__':
100 print("Testing SQL Generation for post_viewer_states")
101 print("="*60)
102 test_sql_generation()
103 print("\n" + "="*60)
104 print("Test complete!")