Exemple #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'])
Exemple #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'])
 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"])
    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'])
    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'])