root / rdfalchemy / trunk / test / literal_test.py

Revision 98, 1.6 kB (checked in by phil, 10 months ago)

This reflects a change of all Descriptor names from a prefix of rdflib to rdf
This fixes improves the behavior of Literal for Decimal and datetime types
It also moves rdfSubject into a different file name
adds one unit test for literals

Line 
1from rdfalchemy import Literal,Namespace
2from datetime import datetime
3from decimal import Decimal
4from rdflib.Literal import bind as bindLiteral 
5
6import logging
7log = logging.getLogger()
8if not log.handlers:
9    log.addHandler(logging.StreamHandler())
10#log.setLevel(10)
11
12
13XSD = Namespace(u'http://www.w3.org/2001/XMLSchema#')
14
15
16
17def toPython_decimal_test():
18    """docstring for toPython"""
19    # test a normal iso
20    d = Literal('.1', datatype=XSD.decimal).toPython()
21    assert isinstance(d, Decimal)
22    payments = [Literal(s,datatype=XSD.decimal) for s in '.1 .1 .1 -.3'.split()]
23    assert sum([payment.toPython() for payment in payments]) == 0
24
25
26def toPython_datetime_test():
27    """docstring for toPython"""
28    # test a normal iso
29    d = Literal('2008-02-09T10:46:29', datatype=XSD.dateTime).toPython()
30    assert isinstance(d, datetime)
31    d = Literal('2008-02-09T10:46:29Z', datatype=XSD.dateTime).toPython()
32    assert isinstance(d, datetime)
33    d = Literal('2008-02-09T10:46:29-07:00', datatype=XSD.dateTime).toPython()
34    assert isinstance(d, datetime)
35   
36    d = Literal('2008-02-09T10:46:29.1', datatype=XSD.dateTime).toPython()
37    assert isinstance(d, datetime)
38    d = Literal('2008-02-09T10:46:29.123', datatype=XSD.dateTime).toPython()
39    assert isinstance(d, datetime)
40    d = Literal('2008-02-09T10:46:29.123456', datatype=XSD.dateTime).toPython()
41    assert isinstance(d, datetime)
42    # test a normal iso with fractional seconds
43   
44    d = Literal('2008-02-09 10:46:29', datatype=XSD.dateTime).toPython()
45    assert isinstance(d, datetime)
46    # test an "almost" iso string
47   
Note: See TracBrowser for help on using the browser.