11import unittest .mock
22from pathlib import Path
33
4- import fastapi
54import pytest
65import reflex as rx
76from reflex import Component , constants
109 generate_links_for_sitemap ,
1110 generate_static_sitemap ,
1211 generate_xml ,
13- remove_sitemap_file ,
1412)
1513from reflex .utils import prerequisites
1614
@@ -90,7 +88,7 @@ def test_generate_xml():
9088
9189
9290def test_generate_static_sitemaps (app_instance , index_page , about_page ):
93- """Test the creation of sitemap.xml through generate_static_sitemaps function ."""
91+ """Test if the generated sitemap file is currently stored in static website or not ."""
9492 pages = {"index" : index_page , "about" : about_page }
9593 # remove the sitemap.xml file if it exists.
9694 sitemap_file_path .unlink (missing_ok = True )
@@ -104,49 +102,17 @@ def test_generate_static_sitemaps(app_instance, index_page, about_page):
104102 assert sitemap_file_path .exists () # check if the sitemap.xml file exists.
105103
106104
107- # Unit test for `serve_sitemap`
108- @pytest .mark .asyncio
109- async def test_serve_sitemap (app_instance , index_page , about_page ):
110- """Test if the correct response is returned when the `serve_sitemap` method is called."""
111- pages = {"index" : index_page , "about" : about_page }
112-
113- # because app_instance automatically generates sitemap.xml from empty _pages dictionary, we need to remove this file.
114- remove_sitemap_file ()
115-
116- # mock self._pages to return the dictionary pages.
117- with unittest .mock .patch .object (app_instance , "_pages" , pages ):
118- # Call the `serve_sitemap` method
119- response : fastapi .Response = await app_instance .serve_sitemap ()
120-
121- # Assert that the response is of type `Response`
122- assert isinstance (response , fastapi .Response )
123- # Assert the content type is 'application/xml'
124- assert response .media_type == "application/xml"
125-
126- # Convert memoryview to bytes explicitly (if it's a memoryview)
127- if isinstance (response .body , memoryview ):
128- response_body = response .body .tobytes ().decode ("utf-8" )
129- else :
130- # If it's already bytes, decode directly
131- response_body = response .body .decode ("utf-8" )
132-
133- # Assert that the XML content is correct (mocked value)
134- assert response_body == mock_xml
135-
136-
137- def test_generate_links_for_sitemap (app_instance , index_page , about_page ):
138- # Add pages to the app
139-
140- pages = {"index" : index_page , "about" : index_page }
105+ def test_generate_links_for_sitemap ():
106+ """Test if the links are generated correctly for the sitemap from the sitemap config file when no deploy url is
107+ given.
108+ """
141109 sitemap_properties = {
142110 "index" : {"priority" : 0.9 , "changefreq" : "weekly" },
143111 "about" : {"priority" : 0.9 , "changefreq" : "weekly" },
144112 }
145113
146- # mock self._pages to return the dictionary pages.
147- # with unittest.mock.patch.object(app_instance, "get_pages", pages) and unittest.mock.patch.object(app_instance, "_sitemap_properties", sitemap_properties):
114+ links = generate_links_for_sitemap (sitemap_properties )
148115
149- links = generate_links_for_sitemap (pages , sitemap_properties )
150116 # Assert that the links are generated correctly
151117 assert links == [
152118 {"loc" : "http://localhost:3000/" , "changefreq" : "weekly" , "priority" : 0.9 },
@@ -156,3 +122,28 @@ def test_generate_links_for_sitemap(app_instance, index_page, about_page):
156122 "priority" : 0.9 ,
157123 },
158124 ]
125+
126+
127+ def test_generate_links_for_sitemap_deploy_url ():
128+ """Test if the links are generated correctly for the sitemap from the sitemap config file when a deploy url is
129+ given.
130+ """
131+ sitemap_properties = {
132+ "index" : {"priority" : 0.9 , "changefreq" : "weekly" },
133+ "about" : {"priority" : 0.9 , "changefreq" : "weekly" },
134+ }
135+
136+ with unittest .mock .patch ("reflex.sitemap.get_config" ) as mock_get_config :
137+ mock_get_config ().deploy_url = "http://www.google.com"
138+
139+ links = generate_links_for_sitemap (sitemap_properties )
140+
141+ # Assert that the links are generated correctly
142+ assert links == [
143+ {"loc" : "http://www.google.com/" , "changefreq" : "weekly" , "priority" : 0.9 },
144+ {
145+ "loc" : "http://www.google.com/about" ,
146+ "changefreq" : "weekly" ,
147+ "priority" : 0.9 ,
148+ },
149+ ]
0 commit comments