def test_flatten(self): for (test, result) in [ ({}, []), ([], []), (['foo'], [('0', 'foo')]), ({'foo': 'bar'}, [('foo', 'bar')]), ({'foo': {'bar': [10, 11, 12, {'wibble': list('matt')}]}}, [('foo.bar.0', 10), ('foo.bar.1', 11), ('foo.bar.2', 12), ('foo.bar.3.wibble.0', 'm'), ('foo.bar.3.wibble.1', 'a'), ('foo.bar.3.wibble.2', 't'), ('foo.bar.3.wibble.3', 't')])]: self.assertEqual(list(api.flatten(test)), result)
def build_request(self, data): """ Helps us create WebOb-like request to feed Formish's forms. Inspired by formish/tests/testish/testish/lib/forms.py """ request = webob.Request.blank(cherrypy.url(), environ={'REQUEST_METHOD': 'POST'}) fields = [] for k, v in flatten(dotted(data)): fields.append((k, v)) request.body = urlencode(fields) return request
def request(self, d): r = webob.Request.blank('http://localhost/') r.method = 'POST' r.content_type = 'application/x-www-form-urlencoded' kvpairs = [('__formish_form__', 'form')] for k,v in flatten(d): lastsegment = k.split('.')[-1] try: int(lastsegment) k = '.'.join(k.split('.')[:-1]) except ValueError: pass for v in d[k]: kvpairs.append( (k,v) ) r.body = urllib.urlencode(kvpairs) return r
def test_flatten(self): for (test, result) in [({}, []), ([], []), (['foo'], [('0', 'foo')]), ({ 'foo': 'bar' }, [('foo', 'bar')]), ({ 'foo': { 'bar': [10, 11, 12, { 'wibble': list('matt') }] } }, [('foo.bar.0', 10), ('foo.bar.1', 11), ('foo.bar.2', 12), ('foo.bar.3.wibble.0', 'm'), ('foo.bar.3.wibble.1', 'a'), ('foo.bar.3.wibble.2', 't'), ('foo.bar.3.wibble.3', 't')])]: self.assertEqual(list(api.flatten(test)), result)
def test_flatten(self): for (test, result) in [ ({}, []), ([], []), (["foo"], [("0", "foo")]), ({"foo": "bar"}, [("foo", "bar")]), ( {"foo": {"bar": [10, 11, 12, {"wibble": list("matt")}]}}, [ ("foo.bar.0", 10), ("foo.bar.1", 11), ("foo.bar.2", 12), ("foo.bar.3.wibble.0", "m"), ("foo.bar.3.wibble.1", "a"), ("foo.bar.3.wibble.2", "t"), ("foo.bar.3.wibble.3", "t"), ], ), ]: self.assertTrue(list(api.flatten(test)) == result)