@@ -26,6 +26,52 @@ def test_create_posts_serializer__existing_url(feed_posts):
2626 assert serializer .errors ['posts' ][0 ]['link' ][0 ] == f'Post at `{ post .link } ` already exists in feed.'
2727
2828
29+ @pytest .mark .django_db
30+ def test_create_posts_serializer__CREATE_POSTS_MAX_LENGTH ():
31+ """Test that CreatePostsSerializer uses CREATE_POSTS_MAX_LENGTH from settings"""
32+ from history4feed .app .settings import history4feed_server_settings
33+ from unittest .mock import patch
34+ import importlib
35+
36+ # Mock the settings value before the serializer class is evaluated
37+ with patch .object (history4feed_server_settings , 'CREATE_POSTS_MAX_LENGTH' , 2 ):
38+ # Reload the serializers module so it picks up the mocked value
39+ import history4feed .app .serializers as serializers_module
40+ importlib .reload (serializers_module )
41+
42+ # Now create the serializer with 3 posts (exceeds limit of 2)
43+ serializer = serializers_module .CreatePostsSerializer (data = {
44+ "posts" : [
45+ {
46+ "title" : "Post 1" ,
47+ "link" : "https://example.com/post1" ,
48+ "pubdate" : datetime .now (UTC ).isoformat (),
49+ "author" : "Author" ,
50+ "categories" : ["Category1" ],
51+ },
52+ {
53+ "title" : "Post 2" ,
54+ "link" : "https://example.com/post2" ,
55+ "pubdate" : datetime .now (UTC ).isoformat (),
56+ "author" : "Author" ,
57+ "categories" : ["Category2" ],
58+ },
59+ {
60+ "title" : "Post 3" ,
61+ "link" : "https://example.com/post3" ,
62+ "pubdate" : datetime .now (UTC ).isoformat (),
63+ "author" : "Author" ,
64+ "categories" : ["Category3" ],
65+ }
66+ ]
67+ })
68+
69+ assert not serializer .is_valid ()
70+ assert 'posts' in serializer .errors
71+ assert 'Ensure this field has no more than 2' in str (serializer .errors ['posts' ][0 ])
72+
73+ # Reload again to restore the original module state
74+ importlib .reload (serializers_module )
2975
3076@pytest .mark .django_db
3177def test_post_serializer_excludes_description ():
0 commit comments