예제 #1
0
    def test_about(self, server_session):
        (server, session) = server_session
        op = session.about()

        # The operation should still be in progress
        assert not op.is_done

        # There shall be one request
        assert server.requests() == 1
        rq = server.next_request()

        # Request shall be a GET
        assert rq.method == 'GET', 'Expecting GET, got %s' % rq

        # Request shall be for base + 'api/about'
        assert rq.uri == BASE_URI + 'api/about'

        # Accept header shall be given
        assert rq.headers['Accept'] == 'text/zinc'

        # Make a grid to respond with
        expected = hszinc.Grid()

        expected.column['haystackVersion'] = {}
        expected.column['tz'] = {}
        expected.column['serverName'] = {}
        expected.column['serverTime'] = {}
        expected.column['serverBootTime'] = {}
        expected.column['productName'] = {}
        expected.column['productUri'] = {}
        expected.column['productVersion'] = {}
        expected.column['moduleName'] = {}
        expected.column['moduleVersion'] = {}
        expected.append({
            'haystackVersion': '2.0',
            'tz': 'UTC',
            'serverName': 'pyhaystack dummy server',
            'serverTime': datetime.datetime.now(tz=pytz.UTC),
            'serverBootTime': datetime.datetime.now(tz=pytz.UTC),
            'productName': 'pyhaystack dummy server',
            'productVersion': '0.0.1',
            'productUri': hszinc.Uri('http://pyhaystack.readthedocs.io'),
            'moduleName': 'tests.client.base',
            'moduleVersion': '0.0.1',
        })

        rq.respond(status=200, headers={
            'Content-Type': 'text/zinc',
        }, content=hszinc.dump(expected, mode=hszinc.MODE_ZINC))

        # State machine should now be done
        assert op.is_done
        actual = op.result
        grid_cmp(expected, actual)
예제 #2
0
    def test_about(self, server_session):
        (server, session) = server_session
        op = session.about()

        # The operation should still be in progress
        assert not op.is_done

        # There shall be one request
        assert server.requests() == 1
        rq = server.next_request()

        # Request shall be a GET
        assert rq.method == "GET", "Expecting GET, got %s" % rq

        # Request shall be for base + 'api/about'
        assert rq.uri == BASE_URI + "api/about"

        # Accept header shall be given
        assert rq.headers[b"Accept"] == "text/zinc"

        # Make a grid to respond with
        expected = hszinc.Grid()

        expected.column["haystackVersion"] = {}
        expected.column["tz"] = {}
        expected.column["serverName"] = {}
        expected.column["serverTime"] = {}
        expected.column["serverBootTime"] = {}
        expected.column["productName"] = {}
        expected.column["productUri"] = {}
        expected.column["productVersion"] = {}
        expected.column["moduleName"] = {}
        expected.column["moduleVersion"] = {}
        expected.append({
            "haystackVersion":
            "2.0",
            "tz":
            "UTC",
            "serverName":
            "pyhaystack dummy server",
            "serverTime":
            datetime.datetime.now(tz=pytz.UTC),
            "serverBootTime":
            datetime.datetime.now(tz=pytz.UTC),
            "productName":
            "pyhaystack dummy server",
            "productVersion":
            "0.0.1",
            "productUri":
            hszinc.Uri("http://pyhaystack.readthedocs.io"),
            "moduleName":
            "tests.client.base",
            "moduleVersion":
            "0.0.1",
        })

        rq.respond(
            status=200,
            headers={b"Content-Type": "text/zinc"},
            content=hszinc.dump(expected, mode=hszinc.MODE_ZINC),
        )

        # State machine should now be done
        assert op.is_done
        actual = op.result
        grid_cmp(expected, actual)
예제 #3
0
def json_decode(raw_json):
    '''
    Recusively scan the de-serialised JSON tree for objects that correspond to
    standard Haystack types and return the Python equivalents.
    '''
    # Simple cases
    if raw_json is None:
        return None
    elif raw_json == MARKER:
        return hszinc.MARKER
    elif isinstance(raw_json, dict):
        result = {}
        for key, value in raw_json.items():
            result[json_decode(key)] = json_decode(value)
        return result
    elif isinstance(raw_json, list):
        return list(map(json_decode, raw_json))
    elif isinstance(raw_json, tuple):
        return tuple(map(json_decode, raw_json))

    # Is it a number?
    match = NUMBER_RE.match(raw_json)
    if match:
        # We'll get a value and a unit, amongst other tokens.
        matched = match.groups()
        value = float(matched[0])
        if matched[-1] is not None:
            # It's a quantity
            return hszinc.Quantity(value, matched[-1])
        else:
            # It's a raw value
            return value

    # Is it a string?
    match = STR_RE.match(raw_json)
    if match:
        return match.group(1)

    # Is it a reference?
    match = REF_RE.match(raw_json)
    if match:
        matched = match.groups()
        if matched[-1] is not None:
            return hszinc.Ref(matched[0], matched[-1], has_value=True)
        else:
            return hszinc.Ref(matched[0])

    # Is it a date?
    match = DATE_RE.match(raw_json)
    if match:
        (year, month, day) = match.groups()
        return datetime.date(year=int(year), month=int(month), day=int(day))

    # Is it a time?
    match = TIME_RE.match(raw_json)
    if match:
        (hour, minute, second) = match.groups()
        # Convert second to seconds and microseconds
        second = float(second)
        int_second = int(second)
        second -= int_second
        microsecond = second * 1e6
        return datetime.time(hour=int(hour),
                             minute=int(minute),
                             second=int_second,
                             microseconds=microsecond)

    # Is it a date/time?
    match = DATETIME_RE.match(raw_json)
    if match:
        matches = match.groups()
        # Parse ISO8601 component
        isodate = iso8601.parse_date(matches[0])
        # Parse timezone
        tzname = matches[-1]
        if tzname is None:
            return isodate  # No timezone given
        else:
            try:
                tz = zoneinfo.timezone(tzname)
                return isodate.astimezone(tz)
            except:
                return isodate

    # Is it a URI?
    match = URI_RE.match(raw_json)
    if match:
        return hszinc.Uri(match.group(1))

    # Is it a Bin?
    match = BIN_RE.match(raw_json)
    if match:
        return hszinc.Bin(match.group(1))

    # Is it a co-ordinate?
    match = COORD_RE.match(raw_json)
    if match:
        (lat, lng) = match.groups()
        return hszinc.Coord(lat, lng)

    # Maybe it's a bare string?
    return raw_json
예제 #4
0
def test_data_types_json():
    grid = hszinc.Grid(version=hszinc.VER_2_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'A null value',
            'value': None,
        },
        {
            'comment': 'A marker',
            'value': hszinc.MARKER,
        },
        {
            'comment': 'A boolean, indicating False',
            'value': False,
        },
        {
            'comment': 'A boolean, indicating True',
            'value': True,
        },
        {
            'comment': 'A reference, without value',
            'value': hszinc.Ref('a-ref'),
        },
        {
            'comment': 'A reference, with value',
            'value': hszinc.Ref('a-ref', 'a value'),
        },
        {
            'comment': 'A binary blob',
            'value': hszinc.Bin('text/plain'),
        },
        {
            'comment': 'A quantity',
            'value': hszinc.Quantity(500,'miles'),
        },
        {
            'comment': 'A quantity without unit',
            'value': hszinc.Quantity(500,None),
        },
        {
            'comment': 'A coordinate',
            'value': hszinc.Coordinate(-27.4725,153.003),
        },
        {
            'comment': 'A URI',
            'value': hszinc.Uri('http://www.example.com'),
        },
        {
            'comment': 'A string',
            'value':    'This is a test\n'\
                        'Line two of test\n'\
                        '\tIndented with "quotes" and \\backslashes\\',
        },
        {
            'comment': 'A date',
            'value': datetime.date(2016,1,13),
        },
        {
            'comment': 'A time',
            'value': datetime.time(7,51,43,microsecond=12345),
        },
        {
            'comment': 'A timestamp (non-UTC)',
            'value': pytz.timezone('Europe/Berlin').localize(\
                    datetime.datetime(2016,1,13,7,51,42,12345)),
        },
        {
            'comment': 'A timestamp (UTC)',
            'value': pytz.timezone('UTC').localize(\
                    datetime.datetime(2016,1,13,7,51,42,12345)),
        },
    ])
    grid_json = json.loads(hszinc.dump(grid, mode=hszinc.MODE_JSON))
    assert grid_json == {
            'meta': {'ver':'2.0'},
            'cols': [
                {'name':'comment'},
                {'name':'value'},
            ],
            'rows': [
                {   'comment': 's:A null value',
                    'value': None},
                {   'comment': 's:A marker',
                    'value': 'm:'},
                {   'comment': 's:A boolean, indicating False',
                    'value': False},
                {   'comment': 's:A boolean, indicating True',
                    'value': True},
                {   'comment': 's:A reference, without value',
                    'value': 'r:a-ref'},
                {   'comment': 's:A reference, with value',
                    'value': 'r:a-ref a value'},
                {   'comment': 's:A binary blob',
                    'value': 'b:text/plain'},
                {   'comment': 's:A quantity',
                    'value': 'n:500.000000 miles'},
                {   'comment': 's:A quantity without unit',
                    'value': 'n:500.000000'},
                {   'comment': 's:A coordinate',
                    'value': 'c:-27.472500,153.003000'},
                {   'comment': 's:A URI',
                    'value': 'u:http://www.example.com'},
                {   'comment': 's:A string',
                    'value': 's:This is a test\n'\
                            'Line two of test\n'\
                            '\tIndented with \"quotes\" '\
                            'and \\backslashes\\'},
                {   'comment': 's:A date',
                    'value': 'd:2016-01-13'},
                {   'comment': 's:A time',
                    'value': 'h:07:51:43.012345'},
                {   'comment': 's:A timestamp (non-UTC)',
                    'value': 't:2016-01-13T07:51:42.012345+01:00 Berlin'},
                {   'comment': 's:A timestamp (UTC)',
                    'value': 't:2016-01-13T07:51:42.012345+00:00 UTC'},
            ],
    }
예제 #5
0
def test_data_types_v2():
    grid = hszinc.Grid(version=hszinc.VER_2_0)
    grid.column['comment'] = {}
    grid.column['value'] = {}
    grid.extend([
        {
            'comment': 'A null value',
            'value': None,
        },
        {
            'comment': 'A marker',
            'value': hszinc.MARKER,
        },
        {
            'comment': 'A "remove" object',
            'value': hszinc.REMOVE,
        },
        {
            'comment': 'A boolean, indicating False',
            'value': False,
        },
        {
            'comment': 'A boolean, indicating True',
            'value': True,
        },
        {
            'comment': 'A reference, without value',
            'value': hszinc.Ref('a-ref'),
        },
        {
            'comment': 'A reference, with value',
            'value': hszinc.Ref('a-ref', 'a value'),
        },
        {
            'comment': 'A binary blob',
            'value': hszinc.Bin('text/plain'),
        },
        {
            'comment': 'A quantity',
            'value': hszinc.Quantity(500,'miles'),
        },
        {
            'comment': 'A quantity without unit',
            'value': hszinc.Quantity(500,None),
        },
        {
            'comment': 'A coordinate',
            'value': hszinc.Coordinate(-27.4725,153.003),
        },
        {
            'comment': 'A URI',
            'value': hszinc.Uri(u'http://www.example.com#`unicode:\u1234\u5678`'),
        },
        {
            'comment': 'A string',
            'value':    u'This is a test\n'\
                        u'Line two of test\n'\
                        u'\tIndented with "quotes", \\backslashes\\ and '\
                        u'Unicode characters: \u1234\u5678 and a $ dollar sign',
        },
        {
            'comment': 'A date',
            'value': datetime.date(2016,1,13),
        },
        {
            'comment': 'A time',
            'value': datetime.time(7,51,43,microsecond=12345),
        },
        {
            'comment': 'A timestamp (non-UTC)',
            'value': pytz.timezone('Europe/Berlin').localize(\
                    datetime.datetime(2016,1,13,7,51,42,12345)),
        },
        {
            'comment': 'A timestamp (UTC)',
            'value': pytz.timezone('UTC').localize(\
                    datetime.datetime(2016,1,13,7,51,42,12345)),
        },
    ])
    grid_str = hszinc.dump(grid)
    ref_str = '''ver:"2.0"
comment,value
"A null value",N
"A marker",M
"A \\"remove\\" object",R
"A boolean, indicating False",F
"A boolean, indicating True",T
"A reference, without value",@a-ref
"A reference, with value",@a-ref "a value"
"A binary blob",Bin(text/plain)
"A quantity",500miles
"A quantity without unit",500
"A coordinate",C(-27.472500,153.003000)
"A URI",`http://www.example.com#\\`unicode:\\u1234\\u5678\\``
"A string","This is a test\\nLine two of test\\n\\tIndented with \\"quotes\\", \\\\backslashes\\\\ and Unicode characters: \\u1234\\u5678 and a \\$ dollar sign"
"A date",2016-01-13
"A time",07:51:43.012345
"A timestamp (non-UTC)",2016-01-13T07:51:42.012345+01:00 Berlin
"A timestamp (UTC)",2016-01-13T07:51:42.012345+00:00 UTC
'''
    assert grid_str == ref_str
예제 #6
0
def gen_random_uri():
    return hszinc.Uri(gen_random_str())
예제 #7
0
def gen_random_uri():
    return hszinc.Uri(gen_random_str(charset= \
                                         string.ascii_letters + string.digits))