Esempio n. 1
0
    def test_convert_object_with_simple_list_with_join_serialization(self):
        # given:
        contain_list = {
            'first': 'hello',
            'list': [1, 2, 3, 4],
            'list2': ['one', 'two'],
        }
        schema = skinfer.generate_schema(contain_list)
        serialize_options = dict(method='join_values')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)

        # then:
        self.assertEquals(['first', 'list', 'list2'], f.fieldnames)
        self.assertEquals(['hello', '1,2,3,4', '["one","two"]'],
                          f.flatten(contain_list))

        # and when:
        schema['properties']['list']['flatson_serialize']['separator'] = '+'
        f = Flatson(schema=schema)

        # then:
        self.assertEquals(['hello', '1+2+3+4', '["one","two"]'],
                          f.flatten(contain_list))
Esempio n. 2
0
    def test_disallow_overwriting_official_serialization_methods(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='always_one')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        with self.assertRaises(ValueError):
            f.register_serialization_method('extract_first', lambda _v, **kw: _v[2])
Esempio n. 3
0
    def test_convert_object_with_simple_list_with_default_serialization(self):
        contain_list = {
            'first': 'hello',
            'list': [1, 2, 3, 4],
            'list2': ['one', 'two'],
        }
        schema = skinfer.generate_schema(contain_list)

        f = Flatson(schema=schema)
        self.assertEquals(['first', 'list', 'list2'], f.fieldnames)
        self.assertEquals(['hello', '[1,2,3,4]', '["one","two"]'], f.flatten(contain_list))
Esempio n. 4
0
    def test_lists_with_objects_with_default_serialization(self):
        # given:
        schema = skinfer.generate_schema(SAMPLE_WITH_LIST_OF_OBJECTS)
        f = Flatson(schema=schema)

        # when:
        result = f.flatten(SAMPLE_WITH_LIST_OF_OBJECTS)

        # then:
        expected = '[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}]'
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', expected], result)
Esempio n. 5
0
 def test_flatten_dict(self):
     contain_nested_object = {
         'first': 'hello',
         'second': {
             'one': 1,
             'two': 2,
         }
     }
     schema = skinfer.generate_schema(contain_nested_object)
     f = Flatson(schema=schema)
     expected = {'first': 'hello', 'second.one': 1, 'second.two': 2}
     self.assertEquals(expected, f.flatten_dict(contain_nested_object))
Esempio n. 6
0
 def test_flatten_dict(self):
     contain_nested_object = {
         'first': 'hello',
         'second': {
             'one': 1,
             'two': 2,
         }
     }
     schema = skinfer.generate_schema(contain_nested_object)
     f = Flatson(schema=schema)
     expected = {'first': 'hello', 'second.one': 1, 'second.two': 2}
     self.assertEquals(expected, f.flatten_dict(contain_nested_object))
Esempio n. 7
0
    def test_convert_object_with_simple_list_with_default_serialization(self):
        contain_list = {
            'first': 'hello',
            'list': [1, 2, 3, 4],
            'list2': ['one', 'two'],
        }
        schema = skinfer.generate_schema(contain_list)

        f = Flatson(schema=schema)
        self.assertEquals(['first', 'list', 'list2'], f.fieldnames)
        self.assertEquals(['hello', '[1,2,3,4]', '["one","two"]'],
                          f.flatten(contain_list))
Esempio n. 8
0
    def test_lists_with_objects_with_default_serialization(self):
        # given:
        schema = skinfer.generate_schema(SAMPLE_WITH_LIST_OF_OBJECTS)
        f = Flatson(schema=schema)

        # when:
        result = f.flatten(SAMPLE_WITH_LIST_OF_OBJECTS)

        # then:
        expected = '[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}]'
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', expected], result)
Esempio n. 9
0
    def test_disallow_overwriting_official_serialization_methods(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='always_one')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        with self.assertRaises(ValueError):
            f.register_serialization_method('extract_first',
                                            lambda _v, **kw: _v[2])
Esempio n. 10
0
 def test_convert_nested_objects(self):
     contain_nested_object = {
         'first': 'hello',
         'second': {
             'one': 1,
             'two': 2,
         }
     }
     schema = skinfer.generate_schema(contain_nested_object)
     f = Flatson(schema=schema)
     self.assertEquals(['first', 'second.one', 'second.two'], f.fieldnames)
     self.assertEquals(['hello', 1, 2], f.flatten(contain_nested_object))
Esempio n. 11
0
 def test_convert_nested_objects(self):
     contain_nested_object = {
         'first': 'hello',
         'second': {
             'one': 1,
             'two': 2,
         }
     }
     schema = skinfer.generate_schema(contain_nested_object)
     f = Flatson(schema=schema)
     self.assertEquals(['first', 'second.one', 'second.two'], f.fieldnames)
     self.assertEquals(['hello', 1, 2], f.flatten(contain_nested_object))
Esempio n. 12
0
    def test_convert_object_with_nested_simple_list_with_default_serialization(self):
        contain_list = {
            'first': 'hello',
            'second': {
                'list1': [1, 2, 3, 4],
                'word': 'world',

            },
        }
        schema = skinfer.generate_schema(contain_list)
        f = Flatson(schema=schema)
        self.assertEquals(['first', 'second.list1', 'second.word'], f.fieldnames)
        self.assertEquals(['hello', '[1,2,3,4]', 'world'], f.flatten(contain_list))
Esempio n. 13
0
    def test_array_serialization_with_extract_key_values(self):
        # given:
        schema = skinfer.generate_schema(SAMPLE_WITH_LIST_OF_OBJECTS)
        serialize_options = dict(method='extract_key_values')

        # when:
        schema['properties']['list']['flatson_serialize'] = serialize_options
        f = Flatson(schema=schema)
        result = f.flatten(SAMPLE_WITH_LIST_OF_OBJECTS)

        # then:
        expected = 'key1:value1,key2:value2;key1:value3,key2:value4'
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', expected], result)
Esempio n. 14
0
    def test_array_serialization_with_extract_key_values(self):
        # given:
        schema = skinfer.generate_schema(SAMPLE_WITH_LIST_OF_OBJECTS)
        serialize_options = dict(method='extract_key_values')

        # when:
        schema['properties']['list']['flatson_serialize'] = serialize_options
        f = Flatson(schema=schema)
        result = f.flatten(SAMPLE_WITH_LIST_OF_OBJECTS)

        # then:
        expected = 'key1:value1,key2:value2;key1:value3,key2:value4'
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', expected], result)
Esempio n. 15
0
    def test_register_custom_serialization_method(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='always_one')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        f.register_serialization_method('always_one', lambda _v, **kw: '1')
        result = f.flatten(sample)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', '1'], result)
Esempio n. 16
0
 def test_convert_object_with_nested_simple_list_with_default_serialization(
         self):
     contain_list = {
         'first': 'hello',
         'second': {
             'list1': [1, 2, 3, 4],
             'word': 'world',
         },
     }
     schema = skinfer.generate_schema(contain_list)
     f = Flatson(schema=schema)
     self.assertEquals(['first', 'second.list1', 'second.word'],
                       f.fieldnames)
     self.assertEquals(['hello', '[1,2,3,4]', 'world'],
                       f.flatten(contain_list))
Esempio n. 17
0
    def test_register_custom_serialization_method(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='always_one')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        f.register_serialization_method('always_one', lambda _v, **kw: '1')
        result = f.flatten(sample)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', '1'], result)
Esempio n. 18
0
    def test_array_serialization_with_extract_first(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='extract_first')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        result = f.flatten(sample)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', 'one'], result)

        # and when:
        sample2 = {'first': 'hello', 'list': []}
        result = f.flatten(sample2)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', None], result)
Esempio n. 19
0
    def test_array_serialization_with_extract_first(self):
        # given:
        sample = {'first': 'hello', 'list': ['one', 'two']}
        schema = skinfer.generate_schema(sample)
        serialize_options = dict(method='extract_first')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)
        result = f.flatten(sample)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', 'one'], result)

        # and when:
        sample2 = {'first': 'hello', 'list': []}
        result = f.flatten(sample2)

        # then:
        self.assertEquals(['first', 'list'], f.fieldnames)
        self.assertEquals(['hello', None], result)
Esempio n. 20
0
    def test_convert_object_with_simple_list_with_join_serialization(self):
        # given:
        contain_list = {
            'first': 'hello',
            'list': [1, 2, 3, 4],
            'list2': ['one', 'two'],
        }
        schema = skinfer.generate_schema(contain_list)
        serialize_options = dict(method='join_values')
        schema['properties']['list']['flatson_serialize'] = serialize_options

        # when:
        f = Flatson(schema=schema)

        # then:
        self.assertEquals(['first', 'list', 'list2'], f.fieldnames)
        self.assertEquals(['hello', '1,2,3,4', '["one","two"]'], f.flatten(contain_list))

        # and when:
        schema['properties']['list']['flatson_serialize']['separator'] = '+'
        f = Flatson(schema=schema)

        # then:
        self.assertEquals(['hello', '1+2+3+4', '["one","two"]'], f.flatten(contain_list))
Esempio n. 21
0
    def get(self, host):
        path = self.get_argument('path', None)
        method = self.get_argument('method', None)

        collection = self.settings['db'].proxyservice['documentation']
        cursor = collection.find({'request.host': host}).sort([('request.path', 1), ('response.status', 1)])
        res = cursor.to_list(100)
        endpoints = yield res

        # Get a list of requests per endpoints
        tree = util.tree()

        for item in endpoints:
            if item['request']['path']:
                parts = filter(None, item['request']['path'].split('/'))
            else:
                parts = ['']
            o = tree
            for part in parts:
                o = o['children'][part]

            o['methods'][item['request']['method']][item['response']['status']] = item['_id']

        req = {'request.host': host, 'request.method': method}
        if path and path != '/':
            req['request.path'] = path
        else:
            req['request.path'] = {'$in': [False, '/']}

        entries = yield collection.find(req).sort([('response.status', 1)]).to_list(100)

        for k, entry in enumerate(entries):
            entries[k]['request']['headers'] = self.nice_headers(entry['request']['headers'])
            entries[k]['response']['headers'] = self.nice_headers(entry['response']['headers'])

            if entry['request']['query']:
                entries[k]['request']['query'] = urlparse.parse_qsl(entry['request']['query'], keep_blank_values=True)

            reqbody = None
            if 'body' in entries[k]['request'] and entries[k]['request']['body']:
                reqbody = entries[k]['request']['body']
                entries[k]['request']['content-type'] = util.get_content_type(entry['request']['headers'])
            elif 'request' in entry and 'fileid' in entry['request']:
                reqbody, entries[k]['request']['content-type'] = yield self.get_gridfs_body(entry['request']['fileid'], entry['request']['headers'])

            if 'content-type' in entries[k]['request'] and reqbody:
                if entries[k]['request']['content-type'] == 'application/x-www-form-urlencoded':
                    entries[k]['request']['body'] = urlparse.parse_qsl(reqbody, keep_blank_values=True)
                else:
                    entries[k]['request']['body'] = self.get_formatted_body(reqbody, entries[k]['request']['content-type'], 'break-all')

            if 'response' in entry:
                if 'fileid' in entry['response']:
                    entries[k]['response']['body'], entries[k]['response']['content-type'] = yield self.get_gridfs_body(entry['response']['fileid'], entry['response']['headers'])
                elif 'body' in entry['response']:
                    entry['response']['content-type'] = util.get_content_type(entry['response']['headers'])

                if util.get_format(entry['response']['content-type']) == 'json':
                    entries[k]['response']['schema'] = skinfer.generate_schema(json.loads(entry['response']['body']))
                    #genson.Schema().add_object(json.loads(resbody)).to_dict()
                
                print entries[k]['response']['content-type']
                entries[k]['response']['body'] = self.get_formatted_body(entries[k]['response']['body'], entries[k]['response']['content-type'], 'break-all')

        collection = self.settings['db'].proxyservice['docsettings']
        row = yield collection.find_one({'host': host})

        self.render("documentationhost.html", host=host, entries=entries, tree=tree, render_tree=self.render_tree, render_document=self.render_document, currentpath=path, method=method, row=row, ObjectId=ObjectId)
Esempio n. 22
0
test_flatson
----------------------------------

Tests for `flatson` module.
"""

import json
import os
import skinfer
import unittest

from flatson import Flatson
import tempfile


EMPTY_SCHEMA = skinfer.generate_schema({})

SIMPLE_SCHEMA = skinfer.generate_schema({'a_prop': ''})

LIST_SCHEMA = skinfer.generate_schema([])

SAMPLE_WITH_LIST_OF_OBJECTS = {
    'first': 'hello',
    'list': [{'key1': 'value1', 'key2': 'value2'}, {'key1': 'value3', 'key2': 'value4'}]
}

SAMPLE_WITH_LIST_OF_TUPLES = {
    'first': 'hello',
    'list': [['value1', 'value2'], ['value3', 'value4']]
}
Esempio n. 23
0
"""
test_flatson
----------------------------------

Tests for `flatson` module.
"""

import json
import os
import skinfer
import unittest

from flatson import Flatson
import tempfile

EMPTY_SCHEMA = skinfer.generate_schema({})

SIMPLE_SCHEMA = skinfer.generate_schema({'a_prop': ''})

LIST_SCHEMA = skinfer.generate_schema([])

SAMPLE_WITH_LIST_OF_OBJECTS = {
    'first':
    'hello',
    'list': [{
        'key1': 'value1',
        'key2': 'value2'
    }, {
        'key1': 'value3',
        'key2': 'value4'
    }]