Exemplo n.º 1
0
def test_scalar_dict_trio_v3():
    grid = shaystack.Grid(version=shaystack.VER_3_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'An empty dict',
            'value': {},
        },
        {
            'comment': 'A marker in a dict',
            'value': {
                "marker": shaystack.MARKER
            },
        },
        {
            'comment': 'A references in a dict',
            'value': {
                "ref": shaystack.Ref('a-ref'),
                "ref2": shaystack.Ref('a-ref', 'a value')
            },
        },
        {
            'comment': 'A quantity in a dict',
            'value': {
                "quantity": shaystack.Quantity(500, 'miles')
            },
        },
    ])
    grid_str = shaystack.dump(grid, mode=shaystack.MODE_TRIO)
    # noinspection PyPep8,PyPep8
    ref_str = textwrap.dedent('''
        comment: An empty dict
        value: {}
        ---
        comment: A marker in a dict
        value: {marker:M}
        ---
        comment: A references in a dict
        value: {'''[1:]) + \
              (" ".join([str(k) + ":" + str(v)
                         for k, v in {"ref": "@a-ref", "ref2": "@a-ref"}.items()])
               .replace("ref2:@a-ref", "ref2:@a-ref \"a value\"")) + \
              textwrap.dedent('''
        }
        ---
        comment: A quantity in a dict
        value: {quantity:500miles}
        '''[1:])
    assert grid_str == ref_str
Exemplo n.º 2
0
def test_ref_not_ref_ne():
    try:
        ref_1 = shaystack.Ref(name='a.ref')
        ref_2 = 'not.a.ref'
        assert ref_1 is not ref_2  # pylint: disable=pointless-statement
        assert False
    except AssertionError:
        pass
Exemplo n.º 3
0
def test_scalar_dict_zinc_v3():
    grid = shaystack.Grid(version=shaystack.VER_3_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'An empty dict',
            'value': {},
        },
        {
            'comment': 'A marker in a dict',
            'value': {
                "marker": shaystack.MARKER
            },
        },
        {
            'comment': 'A references in a dict',
            'value': {
                "ref": shaystack.Ref('a-ref'),
                "ref2": shaystack.Ref('a-ref', 'a value')
            },
        },
        {
            'comment': 'A quantity in a dict',
            'value': {
                "quantity": shaystack.Quantity(500, 'miles')
            },
        },
    ])
    grid_str = shaystack.dump(grid, mode=shaystack.MODE_ZINC)
    assert grid_str == ("ver:\"3.0\"\n"
                        "comment,value\n"
                        "\"An empty dict\",{}\n"
                        "\"A marker in a dict\",{marker:M}\n"
                        "\"A references in a dict\",{" + " ".join([
                            str(k) + ":" + str(v) for k, v in {
                                "ref": "@a-ref",
                                "ref2": "@a-ref"
                            }.items()
                        ]).replace("ref2:@a-ref", "ref2:@a-ref \"a value\"") +
                        "}\n"
                        "\"A quantity in a dict\",{quantity:500miles}\n")
Exemplo n.º 4
0
def test_scalar_dict_csv_v3():
    grid = shaystack.Grid(version=shaystack.VER_3_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'An empty dict',
            'value': {},
        },
        {
            'comment': 'A marker in a dict',
            'value': {
                "marker": shaystack.MARKER
            },
        },
        {
            'comment': 'A references in a dict',
            'value': {
                "ref": shaystack.Ref('a-ref'),
                "ref2": shaystack.Ref('a-ref', 'a value')
            },
        },
        {
            'comment': 'A quantity in a dict',
            'value': {
                "quantity": shaystack.Quantity(500, 'miles')
            },
        },
    ])
    grid_csv = shaystack.dump(grid, mode=shaystack.MODE_CSV)
    assert list(reader(grid_csv.splitlines()))
    assert grid_csv == u'''comment,value
"An empty dict","{}"
"A marker in a dict","{marker:M}"
"A references in a dict","{''' + \
           " ".join([k + ":" + v
                     for k, v in {'ref': '@a-ref', 'ref2': '@a-ref ""a value""'}.items()]) + \
           '''}"
Exemplo n.º 5
0
def test_ref_mixed_neq():
    ref_1 = shaystack.Ref(name='a.ref')
    ref_2 = shaystack.Ref(name='a.ref', value='display text')
    assert ref_1 is not ref_2
    assert ref_1 == ref_2
Exemplo n.º 6
0
def test_ref_simple_neq_id():
    ref_1 = shaystack.Ref(name='a.ref')
    ref_2 = shaystack.Ref(name='another.ref')
    assert ref_1 is not ref_2
    assert ref_1 != ref_2
Exemplo n.º 7
0
def test_ref_simple_eq():
    ref_1 = shaystack.Ref(name='a.ref')
    ref_2 = shaystack.Ref(name='a.ref')
    assert ref_1 is not ref_2
    assert ref_1 == ref_2
Exemplo n.º 8
0
def test_ref_std_method():
    assert str(shaystack.Ref(
        name='a.ref', value='display text')) == '@a.ref \'display text\''
Exemplo n.º 9
0
def test_ref_hash():
    assert hash(shaystack.Ref(name='a.ref', value='display text')) == \
           hash('a.ref')
Exemplo n.º 10
0
def test_ref_with_dis_neq_dis():
    ref_1 = shaystack.Ref(name='a.ref', value='display text')
    ref_2 = shaystack.Ref(name='a.ref', value='different display text')
    assert ref_1 is not ref_2
    assert ref_1 == ref_2
Exemplo n.º 11
0
def test_ref_with_dis_neq_id():
    ref_1 = shaystack.Ref(name='a.ref', value='display text')
    ref_2 = shaystack.Ref(name='another.ref', value='display text')
    assert ref_1 is not ref_2
    assert ref_1 != ref_2
Exemplo n.º 12
0
def test_scalar_ref_csv():
    # No need to be exhaustive, the underlying function is tested heavily by
    # the grid dump tests.
    assert shaystack.dump_scalar(
        shaystack.Ref('areference', 'a display name'),
        mode=shaystack.MODE_CSV) == '@areference a display name'
Exemplo n.º 13
0
def test_scalar_dict_json_v3():
    grid = shaystack.Grid(version=shaystack.VER_3_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'An empty dict',
            'value': {},
        },
        {
            'comment': 'A marker in a dict',
            'value': {
                "marker": shaystack.MARKER
            },
        },
        {
            'comment': 'A references in a dict',
            'value': {
                "ref": shaystack.Ref('a-ref'),
                "ref2": shaystack.Ref('a-ref', 'a value')
            },
        },
        {
            'comment': 'A quantity in a dict',
            'value': {
                "quantity": shaystack.Quantity(500, 'miles')
            },
        },
    ])
    grid_json = json.loads(shaystack.dump(grid, mode=shaystack.MODE_JSON))
    assert grid_json == {
        'meta': {
            'ver': '3.0'
        },
        'cols': [
            {
                'name': 'comment'
            },
            {
                'name': 'value'
            },
        ],
        'rows': [{
            'comment': "s:An empty dict",
            'value': {}
        }, {
            'comment': "s:A marker in a dict",
            'value': {
                'marker': 'm:'
            }
        }, {
            'comment': "s:A references in a dict",
            'value': {
                'ref': 'r:a-ref',
                'ref2': 'r:a-ref a value'
            }
        }, {
            'comment': "s:A quantity in a dict",
            'value': {
                "quantity": 'n:500.000000 miles'
            }
        }]
    }
Exemplo n.º 14
0
 },
 {
     'comment': 'A "remove" object',
     'value': shaystack.REMOVE,
 },
 {
     'comment': 'A boolean, indicating False',
     'value': False,
 },
 {
     'comment': 'A boolean, indicating True',
     'value': True,
 },
 {
     'comment': 'A reference, without value',
     'value': shaystack.Ref('a-ref'),
 },
 {
     'comment': 'A reference, with value',
     'value': shaystack.Ref('a-ref', 'a value'),
 },
 {
     'comment': 'A binary blob',
     'value': shaystack.Bin('text/plain'),
 },
 {
     'comment': 'A quantity',
     'value': shaystack.Quantity(500, 'miles'),
 },
 {
     'comment': 'A quantity without unit',