Exemplo n.º 1
0
    def test_multivaluedict(self):
        d = MultiValueDict({
            'name': ['Adrian', 'Simon'],
            'position': ['Developer']
        })

        self.assertEqual(d['name'], 'Simon')
        self.assertEqual(d.get('name'), 'Simon')
        self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
        self.assertEqual(sorted(d.items()), [('name', 'Simon'),
                                             ('position', 'Developer')])

        self.assertEqual(sorted(d.lists()), [('name', ['Adrian', 'Simon']),
                                             ('position', ['Developer'])])

        with self.assertRaises(MultiValueDictKeyError) as cm:
            d.__getitem__('lastname')
        self.assertEqual(str(cm.exception), "'lastname'")

        self.assertIsNone(d.get('lastname'))
        self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
        self.assertEqual(d.getlist('lastname'), [])
        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
                         ['Adrian', 'Simon'])

        d.setlist('lastname', ['Holovaty', 'Willison'])
        self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
        self.assertEqual(sorted(d.values()),
                         ['Developer', 'Simon', 'Willison'])
Exemplo n.º 2
0
    def test_multivaluedict(self):
        d = MultiValueDict({'name': ['Adrian', 'Simon'],
                            'position': ['Developer']})

        self.assertEqual(d['name'], 'Simon')
        self.assertEqual(d.get('name'), 'Simon')
        self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
        self.assertEqual(
            sorted(six.iteritems(d)),
            [('name', 'Simon'), ('position', 'Developer')]
        )

        self.assertEqual(
            sorted(six.iterlists(d)),
            [('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
        )

        with self.assertRaisesMessage(MultiValueDictKeyError, 'lastname'):
            d.__getitem__('lastname')

        self.assertIsNone(d.get('lastname'))
        self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
        self.assertEqual(d.getlist('lastname'), [])
        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
                         ['Adrian', 'Simon'])

        d.setlist('lastname', ['Holovaty', 'Willison'])
        self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
        self.assertEqual(sorted(six.itervalues(d)),
                         ['Developer', 'Simon', 'Willison'])
Exemplo n.º 3
0
 def test_multivaluedict(self):
     d = MultiValueDict({
         "name": ["Adrian", "Simon"],
         "position": ["Developer"],
         "empty": []
     })
     self.assertEqual(d["name"], "Simon")
     self.assertEqual(d.get("name"), "Simon")
     self.assertEqual(d.getlist("name"), ["Adrian", "Simon"])
     self.assertEqual(
         list(d.items()),
         [("name", "Simon"), ("position", "Developer"), ("empty", [])],
     )
     self.assertEqual(
         list(d.lists()),
         [("name", ["Adrian", "Simon"]), ("position", ["Developer"]),
          ("empty", [])],
     )
     with self.assertRaisesMessage(MultiValueDictKeyError, "'lastname'"):
         d.__getitem__("lastname")
     self.assertIsNone(d.get("empty"))
     self.assertEqual(d.get("empty", "nonexistent"), "nonexistent")
     self.assertIsNone(d.get("lastname"))
     self.assertEqual(d.get("lastname", "nonexistent"), "nonexistent")
     self.assertEqual(d.getlist("lastname"), [])
     self.assertEqual(d.getlist("doesnotexist", ["Adrian", "Simon"]),
                      ["Adrian", "Simon"])
     d.setlist("lastname", ["Holovaty", "Willison"])
     self.assertEqual(d.getlist("lastname"), ["Holovaty", "Willison"])
     self.assertEqual(list(d.values()),
                      ["Simon", "Developer", [], "Willison"])
Exemplo n.º 4
0
    def test_multivaluedict(self):
        d = MultiValueDict({
            'name': ['Adrian', 'Simon'],
            'position': ['Developer']
        })

        self.assertEqual(d['name'], 'Simon')
        self.assertEqual(d.get('name'), 'Simon')
        self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
        self.assertEqual(sorted(six.iteritems(d)), [('name', 'Simon'),
                                                    ('position', 'Developer')])

        self.assertEqual(sorted(six.iterlists(d)),
                         [('name', ['Adrian', 'Simon']),
                          ('position', ['Developer'])])

        with self.assertRaisesMessage(MultiValueDictKeyError, 'lastname'):
            d.__getitem__('lastname')

        self.assertEqual(d.get('lastname'), None)
        self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
        self.assertEqual(d.getlist('lastname'), [])
        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
                         ['Adrian', 'Simon'])

        d.setlist('lastname', ['Holovaty', 'Willison'])
        self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
        self.assertEqual(sorted(six.itervalues(d)),
                         ['Developer', 'Simon', 'Willison'])
Exemplo n.º 5
0
    def test_multivaluedict(self):
        d = MultiValueDict({'name': ['Adrian', 'Simon'],
                            'position': ['Developer']})

        self.assertEqual(d['name'], 'Simon')
        self.assertEqual(d.get('name'), 'Simon')
        self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
        self.assertEqual(
            sorted(d.items()),
            [('name', 'Simon'), ('position', 'Developer')]
        )

        self.assertEqual(
            sorted(d.lists()),
            [('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
        )

        with self.assertRaises(MultiValueDictKeyError) as cm:
            d.__getitem__('lastname')
        self.assertEqual(str(cm.exception), "'lastname'")

        self.assertIsNone(d.get('lastname'))
        self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
        self.assertEqual(d.getlist('lastname'), [])
        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
                         ['Adrian', 'Simon'])

        d.setlist('lastname', ['Holovaty', 'Willison'])
        self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
        self.assertEqual(sorted(d.values()), ['Developer', 'Simon', 'Willison'])