#29443: Unify registries in Django
-------------------------------------+-------------------------------------
     Reporter:  Johannes Hoppe       |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Utilities            |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  plugin, registry,    |             Triage Stage:
  pattern, utils                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Johannes Hoppe):

 I also have tests for the example, but it's written in PyTest but it's a
 start:
 {{{
 class Plugin:
     pass


 class TestRegistry:

     def test_init(self):
         reg = Registry()
         assert reg.unique is True
         assert reg.entry_type is None

         reg = Registry(entry_type=Plugin, unique=False)
         assert reg.unique is False
         assert reg.entry_type is Plugin

         with pytest.raises(TypeError) as e:
             Registry(entry_type=Plugin())
         assert str(e.value) == '"entry_type" expects a class, but got an
 instance.'

     def test_iter(self):
         reg = Registry()
         assert reg._register is not reg.__iter__()

         for i in range(5):
             reg.register(i)

         for i, entry in enumerate(reg):
             reg.register(i + 5)
             assert entry < 5

     def test_len(self):
         reg = Registry()
         assert not reg
         reg.register(1)
         assert len(reg) == 1

     def test_clear(self):
         reg = Registry()
         reg.register(1)
         assert len(reg) == 1
         reg.clear()
         assert not reg
         assert list(reg) == []

     def test_register(self):
         reg = Registry()
         reg.register(1)
         assert reg._register == [1], "1 should be in registry"

         with pytest.raises(ValueError) as e:
             reg.register(1)
         assert str(e.value) == '"1" is already registered.'
         assert reg._register.count(1) == 1, "1 is only once in the
 registry"

         reg = Registry(entry_type=list)
         with pytest.raises(TypeError) as e:
             reg.register(entry={})

         assert str(e.value) == '"entry" expects <class \'list\'>, but got
 {}'

         with pytest.raises(TypeError) as e:
             reg.register(entry=dict)

         assert str(e.value) == '"entry" expects <class \'list\'>, but got
 <class \'dict\'>'

     def test_unregister(self):
         reg = Registry()
         with pytest.raises(Exception) as e:
             reg.unregister(1)
         assert str(e.value) == '"1" is not registered.'
         assert not reg
         reg.register(1)
         assert len(reg) == 1
         assert 1 in reg._register
         reg.unregister(1)
         assert not reg
         assert 1 not in reg._register

         reg = Registry(unique=False)
         reg.register(1)
         reg.register(2)
         reg.register(1)

         assert list(reg) == [1, 2, 1]
         reg.unregister(1)
         assert reg._register == [2]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29443#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.66cda767d2c9b030d0924606410a1cf7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to