You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was writing up tests for the icon component package, and I realized that I had to address the same issues with testing components as when I'm writing tests in django-components.
So IMO it'd be helpful if django-components had a way to simplify writing tests.
Some issues with testing components are:
Component IDs are random, so assertions don't work well
Components may be already registered
Our cache of Template instances may have leftover entries
So what I did in the icon package was to wrap that all in a TestEnv class, and then call test_env.setup() and test_env.teardown() in respective methods:
The implementation looks like this, it's taken from django-components from here:
fromtypingimportList, Optionalfromunittest.mockimportpatchfromdjango.testimportSimpleTestCasefromdjango_components.component_registryimportComponentRegistry, all_registriesclassTestEnv:
def__init__(self, registries: Optional[List[ComponentRegistry]] =None):
self.registries=registriesorall_registriesdefsetup(self):
self._start_gen_id_patch()
defteardown(self):
self._stop_gen_id_patch()
forregistryinself.registries:
registry.clear()
fromdjango_components.templateimporttemplate_cache# NOTE: There are 1-2 tests which check Templates, so we need to clear the cacheiftemplate_cache:
template_cache.clear()
# Mock the `generate` function used inside `gen_id` so it returns deterministic IDsdef_start_gen_id_patch(self):
# Random number so that the generated IDs are "hex-looking", e.g. a1bc3dself._gen_id_count=10599485defmock_gen_id(*args, **kwargs):
self._gen_id_count+=1returnhex(self._gen_id_count)[2:]
self._gen_id_patch=patch("django_components.util.misc.generate", side_effect=mock_gen_id)
self._gen_id_patch.start()
def_stop_gen_id_patch(self):
self._gen_id_patch.stop()
self._gen_id_count=10599485
Questions
Should this be part of the main package, or separate?
How should it be imported?
I'm thinking that, since it's just one small class right now, it makes sense to include it in the django_components distribution.
@JuroOravec I think this should be part of the package, available for package builders, and also actually used by the internal tests as well. Could we package our own test logic as a reusable class, use it across tests, and document it as an externally available tool.
I was writing up tests for the icon component package, and I realized that I had to address the same issues with testing components as when I'm writing tests in django-components.
So IMO it'd be helpful if django-components had a way to simplify writing tests.
Some issues with testing components are:
Template
instances may have leftover entriesSo what I did in the icon package was to wrap that all in a
TestEnv
class, and then calltest_env.setup()
andtest_env.teardown()
in respective methods:The implementation looks like this, it's taken from django-components from here:
Questions
I'm thinking that, since it's just one small class right now, it makes sense to include it in the
django_components
distribution.So it could be used e.g. like this:
If it ever gets too large, it could be moved into a separate package, and then exposed via django-components as an optional dependency:
Notes
As part of this issues we should also document how to test components, and maybe also point to the icon package as a live example.
The text was updated successfully, but these errors were encountered: