1414from django .test import TestCase
1515from django .urls import reverse
1616
17+ import json
18+ from xml .etree import ElementTree as ET
19+
1720from works .models import Work , GlobalRegion
1821
1922
23+ NSPS = {"atom" : "http://www.w3.org/2005/Atom" }
24+
25+
2026# Expected work counts per region based on test_data_global_feeds fixture
2127# These values are hardcoded from the actual fixture data to ensure tests
2228# verify the exact expected behavior
4147}
4248
4349
44- class FeedLandingPageTests (TestCase ):
50+ class GlobalFeedsAndLandingPageTests (TestCase ):
4551 fixtures = ["test_data_global_feeds" ]
4652
4753 @classmethod
4854 def setUpTestData (cls ):
55+ call_command ("flush" , "--no-input" )
4956 call_command ("load_global_regions" )
5057 call_command ("loaddata" , "test_data_global_feeds" )
5158
52- def _slugify (self , name ):
59+ def slugify (self , name ):
5360 """Convert region name to slug."""
5461 return name .lower ().replace (" " , "-" )
5562
63+ def test_global_region_load (self ):
64+ regions = GlobalRegion .objects .all ()
65+ self .assertEqual (len (regions ), 15 )
66+
67+ def test_georss_feed_per_region (self ):
68+ for region in GlobalRegion .objects .all ():
69+ slug = self .slugify (region .name )
70+ # Use new API v1 endpoint based on region type
71+ if region .region_type == 'continent' :
72+ url = reverse ("optimap:api-continent-georss" , kwargs = {"continent_slug" : slug })
73+ else : # ocean
74+ url = reverse ("optimap:api-ocean-georss" , kwargs = {"ocean_slug" : slug })
75+
76+ resp = self .client .get (url )
77+ self .assertEqual (resp .status_code , 200 ,
78+ f"{ region .name } GeoRSS feed failed" )
79+
80+ root = ET .fromstring (resp .content )
81+ titles = [item .find ("title" ).text
82+ for item in root .findall (".//item" )]
83+
84+ expected_titles = list (
85+ Work .objects
86+ .filter (
87+ status = "p" ,
88+ geometry__isnull = False ,
89+ geometry__intersects = region .geom
90+ )
91+ .order_by ("-creationDate" )
92+ .values_list ("title" , flat = True )
93+ )
94+
95+ self .assertCountEqual (
96+ titles , expected_titles ,
97+ f"GeoRSS feed for { region .name } returned { titles !r} , expected { expected_titles !r} "
98+ )
99+
100+ def test_geoatom_feed_australia (self ):
101+ # Use new API v1 Atom endpoint
102+ url = reverse ("optimap:api-continent-atom" , kwargs = {"continent_slug" : "australia" })
103+ response = self .client .get (url )
104+ self .assertEqual (response .status_code , 200 )
105+
106+ root = ET .fromstring (response .content )
107+ titles = [entry .find ("atom:title" , namespaces = NSPS ).text
108+ for entry in root .findall (".//atom:entry" , namespaces = NSPS )]
109+
110+ self .assertEqual (len (titles ), 6 , "Atom feed for Australia should return 6 entries" )
111+ self .assertEqual (titles [0 ], "Migration Route: Asia to Australia" , "Atom feed for Australia returned unexpected title" )
112+
113+ def test_georss_feed_south_atlantic (self ):
114+ # Use new API v1 GeoRSS endpoint
115+ url = reverse ("optimap:api-ocean-georss" , kwargs = {"ocean_slug" : "south-atlantic-ocean" })
116+ response = self .client .get (url )
117+ self .assertEqual (response .status_code , 200 )
118+
119+ root = ET .fromstring (response .content )
120+ titles = [item .find ("title" ).text
121+ for item in root .findall (".//item" , namespaces = NSPS )]
122+
123+ self .assertEqual (len (titles ), 10 , "GeoRSS feed for South Atlantic Ocean should return 10 entries" )
124+ self .assertEqual (titles [0 ], "Marine Biology and Oceanography of the Southern Ocean" , "GeoRSS feed for South Atlantic Ocean returned unexpected first title" )
125+ self .assertEqual (titles [9 ], "Global Climate Network" , "GeoRSS feed for South Atlantic Ocean returned unexpected last title" )
126+
127+
56128 def test_all_continent_pages_display_correct_work_counts (self ):
57129 """Test that all continent feed pages display the correct number of works."""
58130 continents = GlobalRegion .objects .filter (region_type = GlobalRegion .CONTINENT )
59131
60132 for region in continents :
61133 with self .subTest (continent = region .name ):
62- slug = self ._slugify (region .name )
134+ slug = self .slugify (region .name )
63135 expected_count = EXPECTED_COUNTS .get (slug , 0 )
64136
65137 url = reverse ('optimap:feed-continent-page' , kwargs = {'continent_slug' : slug })
@@ -81,19 +153,19 @@ def test_all_continent_pages_display_correct_work_counts(self):
81153
82154 # Verify the count is shown in the HTML
83155 if expected_count > 0 :
84- self .assertContains (response , f'Showing { expected_count } publication ' ,
156+ self .assertContains (response , f'{ expected_count } research works ' ,
85157 msg_prefix = f"Work count not displayed for { region .name } " )
86158
87159 # Verify at least the first work title appears
88160 self .assertContains (response , actual_works [0 ].title ,
89161 msg_prefix = f"First work title not found for { region .name } " )
90162
91163 # Should NOT show empty message
92- self .assertNotContains (response , 'No publications found' ,
164+ self .assertNotContains (response , 'No works found' ,
93165 msg_prefix = f"{ region .name } should not show empty message" )
94166 else :
95167 # Should show empty message
96- self .assertContains (response , 'No publications found' ,
168+ self .assertContains (response , 'No works found' ,
97169 msg_prefix = f"{ region .name } should show empty message" )
98170
99171 def test_all_ocean_pages_display_correct_work_counts (self ):
@@ -102,7 +174,7 @@ def test_all_ocean_pages_display_correct_work_counts(self):
102174
103175 for region in oceans :
104176 with self .subTest (ocean = region .name ):
105- slug = self ._slugify (region .name )
177+ slug = self .slugify (region .name )
106178 expected_count = EXPECTED_COUNTS .get (slug , 0 )
107179
108180 url = reverse ('optimap:feed-ocean-page' , kwargs = {'ocean_slug' : slug })
@@ -124,25 +196,25 @@ def test_all_ocean_pages_display_correct_work_counts(self):
124196
125197 # Verify the count is shown in the HTML
126198 if expected_count > 0 :
127- self .assertContains (response , f'Showing { expected_count } publication ' ,
199+ self .assertContains (response , f'{ expected_count } research works ' ,
128200 msg_prefix = f"Work count not displayed for { region .name } " )
129201
130202 # Verify at least the first work title appears
131203 self .assertContains (response , actual_works [0 ].title ,
132204 msg_prefix = f"First work title not found for { region .name } " )
133205
134206 # Should NOT show empty message
135- self .assertNotContains (response , 'No publications found' ,
207+ self .assertNotContains (response , 'No works found' ,
136208 msg_prefix = f"{ region .name } should not show empty message" )
137209 else :
138210 # Should show empty message
139- self .assertContains (response , 'No publications found' ,
211+ self .assertContains (response , 'No works found' ,
140212 msg_prefix = f"{ region .name } should show empty message" )
141213
142214 def test_continent_page_shows_region_metadata (self ):
143215 """Test that continent pages show correct region metadata."""
144216 region = GlobalRegion .objects .filter (region_type = GlobalRegion .CONTINENT ).first ()
145- slug = self ._slugify (region .name )
217+ slug = self .slugify (region .name )
146218 url = reverse ('optimap:feed-continent-page' , kwargs = {'continent_slug' : slug })
147219 response = self .client .get (url )
148220
@@ -162,7 +234,7 @@ def test_continent_page_shows_region_metadata(self):
162234 def test_ocean_page_shows_region_metadata (self ):
163235 """Test that ocean pages show correct region metadata."""
164236 region = GlobalRegion .objects .filter (region_type = GlobalRegion .OCEAN ).first ()
165- slug = self ._slugify (region .name )
237+ slug = self .slugify (region .name )
166238 url = reverse ('optimap:feed-ocean-page' , kwargs = {'ocean_slug' : slug })
167239 response = self .client .get (url )
168240
@@ -182,7 +254,7 @@ def test_ocean_page_shows_region_metadata(self):
182254 def test_feed_page_cache_refresh (self ):
183255 """Test that ?now parameter forces cache refresh."""
184256 region = GlobalRegion .objects .filter (region_type = GlobalRegion .CONTINENT ).first ()
185- slug = self ._slugify (region .name )
257+ slug = self .slugify (region .name )
186258 url = reverse ('optimap:feed-continent-page' , kwargs = {'continent_slug' : slug })
187259
188260 # First request (no cache)
0 commit comments