def test_extract_with_preset_value(self): widget = factory('select2', name='multi', value=['1', '2'], props={ 'multiple': True, 'inputtag': True }) self.assertEqual( widget(), ('<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2" />')) request = {'multi': '1,2,3'} data = widget.extract(request) self.assertEqual( widget(data=data), ('<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2,3" />')) self.assertEqual(data.extracted, ['1', '2', '3']) request = {'multi': ''} data = widget.extract(request) self.assertEqual( widget(data=data), ('<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="" />')) self.assertEqual(data.extracted, [])
def test_extract_with_preset_value(self): widget = factory( 'select2', name='multi', value=['1', '2'], props={ 'multiple': True, 'inputtag': True }) self.assertEqual(widget(), ( '<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2" />' )) request = {'multi': '1,2,3'} data = widget.extract(request) self.assertEqual(widget(data=data), ( '<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2,3" />' )) self.assertEqual(data.extracted, ['1', '2', '3']) request = {'multi': ''} data = widget.extract(request) self.assertEqual(widget(data=data), ( '<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="" />' )) self.assertEqual(data.extracted, [])
def test_display_renderer_empty(self): widget = factory( 'select2', name='empty', mode='display') self.assertEqual(widget(), '<div class="display-select2" id="display-empty"></div>' )
def test_display_renderer_single(self): widget = factory( 'select2', name='single', value='foo', props={'vocabulary': [('foo', 'Foo'), ('bar', 'Bar')]}, mode='display') self.assertEqual( widget(), '<div class="display-select2" id="display-single">Foo</div>')
def test_render_multiple(self): widget = factory('select2', name='multi', props={ 'required': True, 'multiple': True }) self.assertEqual(widget(), ( '<input id="exists-multi" name="multi-exists" type="hidden" ' 'value="exists" /><select class="select2" id="input-multi" ' 'multiple="multiple" name="multi" required="required"> </select>'))
def test_render_single_tag_mode(self): widget = factory( 'select2', name='single', props={ 'inputtag': True }) self.assertEqual(widget(), ( '<input class="select2" id="input-single" name="single" ' 'value="" />' ))
def test_render_single(self): widget = factory( 'select2', name='single', props={ 'required': True }) self.assertEqual(widget(), ( '<select class="select2" id="input-single" name="single" ' 'required="required" />' ))
def test_render_multiple_tag_mode(self): widget = factory('select2', name='multi', value=['1', '2'], props={ 'inputtag': True, 'multiple': True }) self.assertEqual( widget(), ('<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2" />'))
def test_display_renderer_multiple(self): widget = factory('select2', name='multi', value=['foo', 'bar'], props={ 'vocabulary': [('foo', 'Foo'), ('bar', 'Bar')], 'multiple': True, }, mode='display') self.assertEqual(widget(), ('<ul class="display-select2" ' 'id="display-multi"><li>Foo</li><li>Bar</li></ul>'))
def test_display_renderer_single(self): widget = factory( 'select2', name='single', value='foo', props={ 'vocabulary': [('foo', 'Foo'), ('bar', 'Bar')] }, mode='display') self.assertEqual(widget(), '<div class="display-select2" id="display-single">Foo</div>' )
def test_render_multiple_tag_mode(self): widget = factory( 'select2', name='multi', value=['1', '2'], props={ 'inputtag': True, 'multiple': True }) self.assertEqual(widget(), ( '<input class="select2" data-multiple=\'true\' id="input-multi" ' 'name="multi" value="1,2" />' ))
def test_render_multiple(self): widget = factory( 'select2', name='multi', props={ 'required': True, 'multiple': True }) self.assertEqual(widget(), ( '<input id="exists-multi" name="multi-exists" type="hidden" ' 'value="exists" /><select class="select2" id="input-multi" ' 'multiple="multiple" name="multi" required="required" />' ))
def test_display_renderer_multiple(self): widget = factory( 'select2', name='multi', value=['foo', 'bar'], props={ 'vocabulary': [('foo', 'Foo'), ('bar', 'Bar')], 'multiple': True, }, mode='display') self.assertEqual(widget(), ( '<ul class="display-select2" ' 'id="display-multi"><li>Foo</li><li>Bar</li></ul>' ))
def test_render_with_vocabulary(self): # Provide a vocabulary if value terms consists of id / label pairs vocab = OrderedDict() vocab['1'] = 'Label 1' vocab['2'] = 'Label 2' widget = factory('select2', name='multi', value=['1', '2'], props={ 'inputtag': True, 'multiple': True, 'vocabulary': vocab, }) self.assertEqual( widget(), ('<input class="select2" data-multiple=\'true\' ' 'data-vocabulary=\'{"1": "Label 1", "2": "Label 2"}\' ' 'id="input-multi" name="multi" value="1,2" />'))
def test_render_with_vocabulary(self): # Provide a vocabulary if value terms consists of id / label pairs vocab = OrderedDict() vocab['1'] = 'Label 1' vocab['2'] = 'Label 2' widget = factory( 'select2', name='multi', value=['1', '2'], props={ 'inputtag': True, 'multiple': True, 'vocabulary': vocab, }) self.assertEqual(widget(), ( '<input class="select2" data-multiple=\'true\' ' 'data-vocabulary=\'{"1": "Label 1", "2": "Label 2"}\' ' 'id="input-multi" name="multi" value="1,2" />' ))
def test_render_single_tag_mode(self): widget = factory('select2', name='single', props={'inputtag': True}) self.assertEqual( widget(), ('<input class="select2" id="input-single" name="single" ' 'value="" />'))
def test_render_single(self): widget = factory('select2', name='single', props={'required': True}) self.assertEqual( widget(), ('<select class="select2" id="input-single" name="single" ' 'required="required"> </select>'))
def test_display_renderer_empty(self): widget = factory('select2', name='empty', mode='display') self.assertEqual( widget(), '<div class="display-select2" id="display-empty"></div>')