Пример #1
0
def test_class_dumps():
    """Test the dump of the jason value of a class by using
       the __jason__ method.
    """
    class C:
        def __json__(self):
            return 42

    assert jason.dumps(C()) == '42'

    class D(object):
        def __init__(self):
            self.a = 42

    assert jason.json_eval(jason.dumps(D())) == {"a": 42}
Пример #2
0
def test_sqlgrid(db, rf):
    usr = User.objects.create_user('username', '*****@*****.**', 'password',
                                   first_name=u'hello', last_name=u'world')
    
    class MySqlGrid(SqlGrid):
        def get_sql_query(self, request, *args, **kwargs):
            return SelectStmt(
                select="username, email, date_joined",
                from_="auth_user",
                where="1=1",
                order_by="username",
                limit=100,
                offset=0
            )
        
    view = MySqlGrid.as_view()
    r = view(rf.post('!get-records'))
    print("R:", r)
    assert r.status_code == 200
    
    val = dict(jason.loads(r.content.decode('u8')))
    print(jason.dumps(val))
    rows = [dict(r) for r in val['rows']]
    print("ROWS:", rows)
    assert len(rows) == 1
    row = rows[0]
    assert row['k'] == 0
    assert row['c'] == [
        'username',
        '*****@*****.**',
        '@datetime:' + usr.date_joined.isoformat(),
    ]
    assert len(val['cols']) == 3
    assert val['cols'][0]['name'] == 'username'
    assert val['cols'][0]['type'] == 'varchar'
Пример #3
0
def test_modelgrid(db, rf):
    class MG(ModelGrid):
        model = User
        columns = ['username']
        
    view = MG.as_view()
    r = view(rf.get('!get-records'))
    assert r.status_code == 200
    val = dict(jason.loads(r.content.decode('u8')))
    print(jason.dumps(val))
    assert val['rows'] == []
    assert len(val['cols']) == 1
    assert val['cols'][0]['name'] == 'username'
    assert val['cols'][0]['type'] == 'Char'
Пример #4
0
def test_set_dumps():
    assert jason.json_eval(jason.dumps(1)) == 1
    assert jason.json_eval(jason.dumps(set())) == []
    assert jason.json_eval(jason.dumps({1, 2})) == [1, 2]
    assert jason.json_eval(jason.dumps(ttcal.Year(2017))) == {
        'year': 2017,
        'kind': 'YEAR'
    }
    assert jason.dumps(ttcal.Duration.parse('1:10:01')) == '"@duration:4201"'
    assert jason.dumps(datetime.date(2019, 3, 15)) == '"@date:2019-03-15"'
    assert jason.json_eval(
        jason.dumps(datetime.time(hour=1, minute=10, second=1))) == {
            'hour': 1,
            'minute': 10,
            'second': 1,
            'microsecond': 0,
            'kind': 'TIME'
        }

    class Foo(object):
        __slots__ = ['a', 'b']

    with pytest.raises(TypeError):
        jason.dumps(Foo())  # not JSON serializable
Пример #5
0
def test_modelgrid_sort(db, rf):
    class MG(ModelGrid):
        model = User
        columns = 'username email date_joined'.split()
        
        def get_queryset(self, request, *args, **kwargs):
            qs = super(MG, self).get_queryset(request, *args, **kwargs)
            return qs
        
        def get_columns(self, request, qs, object_list):
            columns = super(MG, self).get_columns(request, qs, object_list)
            
            @columns.append
            class full_name(Column):
                def value(self, item):
                    return item.get_full_name()
                
            return columns

    usr = User.objects.create_user('username', '*****@*****.**', 'password', 
                                   first_name=u'hello', last_name=u'world')
    view = MG.as_view()
    r = view(rf.post('!get-records', {'s': '-username,email'}))
    assert r.status_code == 200
    val = dict(jason.loads(r.content.decode('u8')))
    print(jason.dumps(val))
    rows = [dict(r) for r in val['rows']]
    print("ROWS:", rows)
    assert len(rows) == 1
    row = rows[0]
    assert row['k'] == 1
    assert row['c'] == [
        'username', 
        '*****@*****.**', 
        '@datetime:' + usr.date_joined.isoformat(),
        u'hello world'
    ]
    assert len(val['cols']) == 4
    assert val['cols'][0]['name'] == 'username'
    assert val['cols'][0]['type'] == 'Char'
    assert val['cols'][3]['name'] == 'full_name'
Пример #6
0
def test_children():
    """
            `-- a  (root)
                `-- b
                    |-- c
                    `-- d
    """
    d = Section(id=4, name='d')
    c = Section(id=3, name='c')
    b = Section(id=2, name='b', children=[c, d])
    root = Section(id=1, name='a', children=[b])

    sections = [root, b, c, d]

    ds = TreeDatasource()
    with ds:
        ds += sections
    # for node in ds.cache.values():
    #     print "NODE:", node
    assert ds.validate()
    # print jason.dumps(ds)
    assert jason.loads(jason.dumps(ds)) == treeds_result
Пример #7
0
def test_parent():
    """
            `-- a  (root)
                `-- b
                    |-- c
                    `-- d
    """
    root = Section(id=1, name='a', parent=None)
    b = Section(id=2, name='b', parent=root)
    c = Section(id=3, name='c', parent=b)
    d = Section(id=4, name='d', parent=b)

    sections = [root, b, c, d]

    ds = TreeDatasource()
    with ds:
        ds += sections
        ds += []
    print(ds)  # coverage for __repr__
    assert ds.validate()
    # print jason.dumps(ds)
    assert json.loads(jason.dumps(ds)) == treeds_result
Пример #8
0
def test_path():
    """
            `-- a  (root)
                `-- b
                    |-- c
                    `-- d
    """
    root = Section(id=1, name='a', path=[1])
    b = Section(id=2, name='b', path=[1, 2])
    c = Section(id=3, name='c', path=[1, 2, 3])
    d = Section(id=4, name='d', path=[1, 2, 4])

    sections = [root, b, c, d]

    ds = TreeDatasource()
    with ds:
        ds += sections
    # for node in ds.cache.values():
    #     print "NODE:", node
    assert ds.validate()
    # print jason.dumps(ds)
    assert jason.loads(jason.dumps(ds)) == treeds_result
Пример #9
0
 def __repr__(self):
     return jason.dumps(self)
Пример #10
0
def test_tds():
    print("VALS:", vals)
    ds = _mktree(vals)
    print(jason.dumps(ds.cache, indent=4))
    print(repr(ds))
    assert ds.validate()
Пример #11
0
def test_dj_dumps():
    from django.contrib.auth.models import User
    assert jason.dumps(User.objects.none()) == '[]'
Пример #12
0
def test_dumps():
    "Test the dumps function."
    assert jason.dumps(datetime.datetime(2012, 4, 2, 6, 12),
                       indent=None) == '"@datetime:2012-04-02T06:12:00"'
    assert jason.dumps(decimal.Decimal('3.14159263')) == repr(
        float('3.14159263'))
Пример #13
0
def roundtrip(v):
    "Convenience function to thest the roundtrip (dump/eval)."
    return jason.json_eval(jason.dumps(v)) == v