Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
leangseu-edx committed Sep 5, 2023
1 parent 5dd4b33 commit 0560ffe
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions openassessment/xblock/test/test_load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

class TestLoadStatic(TestCase):
"""Test load static class"""
default_base_url = '/static/dist/'

def setUp(self):
LoadStatic._manifest = {} # pylint: disable=protected-access
LoadStatic._is_loaded = False # pylint: disable=protected-access
LoadStatic._base_url = '' # pylint: disable=protected-access
LoadStatic._is_dev_server = False # pylint: disable=protected-access
return super().setUp()

def test_urljoin(self):
Expand Down Expand Up @@ -45,60 +44,26 @@ def test_urljoin(self):

def test_get_url_default(self):
key_url = 'some_url.js'
self.assertEqual(LoadStatic.get_url(key_url),
urljoin(self.default_base_url, key_url))
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/static', 'dist', key_url))

def test_get_url_absolute_base_url(self):
# Given my base_url is an absolute URL and is overridden (as in hot-reload
# local dev setup)
@override_settings(STATIC_URL='/cms')
def test_get_url_with_custom_static_url(self):
key_url = 'some_url.js'
absolute_base_url = 'foo://bar'
loaded_key_url = '/some_url.hashchunk.js'
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/cms', 'dist', key_url))

# When I get a static URL
with patch('json.loads') as jsondata:
jsondata.return_value = {
'base_url': absolute_base_url,
'some_url.js': loaded_key_url,
}
# Then I use the base URL instead of default LMS URL
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
absolute_base_url, loaded_key_url))

@patch('pkg_resources.resource_string')
def test_get_url_file_not_found(self, resource_string):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
self.default_base_url, 'some_url.js'))

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_file_not_found_with_root_url(self, resource_string):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
'localhost/', self.default_base_url, 'some_url.js'))

@patch('pkg_resources.resource_string')
def test_get_url_with_manifest(self, resource_string):
def test_is_dev_server_url(self, resource_string):
resource_string.return_value = None
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
'some_url.js': 'some_url.hashchunk.js'
'some_url.js': 'some_url.hash.js',
'is_dev_server': True
}
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
self.default_base_url, 'some_url.hashchunk.js'))
self.assertEqual(LoadStatic.get_url(key_url), 'some_url.hash.js')

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_with_manifest_and_root_url(self, resource_string):
resource_string.return_value = None
def test_get_url_file_not_found(self, resource_string):
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
'some_url.js': 'some_url.hashchunk.js'
}
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
'localhost/', self.default_base_url, 'some_url.hashchunk.js'))
resource_string.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin('/static', 'dist', key_url))

0 comments on commit 0560ffe

Please sign in to comment.