root/rdfalchemy/trunk/test/company_test.py

Revision 98, 3.2 KB (checked in by phil, 4 years 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 rdfSubject, rdfSingle, rdfMultiple
2from rdflib import ConjunctiveGraph, Namespace, Literal, BNode, URIRef
3
4from StringIO import StringIO
5
6n3data = """
7@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
8@prefix ov:    <http://owl.openvest.org/2005/10/Portfolio#>.
9@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
10@prefix iso3166: <http://www.daml.org/2001/09/countries/iso-3166-ont#>.
11
12ov:C_US_SUNW a ov:Company;
13     ov:GICS_2 ov:GICS_2_45;
14     ov:companyName "Sun Microsystems";
15     ov:country iso3166:US;
16     ov:nasdaqSymbol "SUNW";
17     ov:secCik "0000709519";
18     ov:symbol "JAVA".
19     
20ov:C_US_IBM a ov:Company;
21     ov:GICS_2 ov:GICS_2_45;
22     ov:companyName "International Business Machines Corp.";
23     ov:country iso3166:US;
24     ov:nyseSymbol "IBM";
25     ov:secCik "0000051143";
26     ov:symbol "IBM";
27     ov:yindustry "Diversified Computer Systems";
28     ov:ysector "Technology";
29     vcard:tel "914-499-1900";
30     vcard:url "http://www.ibm.com"^^<http://www.w3.org/2001/XMLSchema#anyURI>;
31     ov:stockDescription "International Business Machines Corporation (IBM) operates as an information technology (IT) company worldwide. It has .....".
32"""
33
34OV = Namespace('http://owl.openvest.org/2005/10/Portfolio#')
35VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")
36
37rdfSubject.db = ConjunctiveGraph()
38rdfSubject.db.parse(StringIO(n3data), format='n3')
39
40class Company(rdfSubject):
41    rdf_type = OV.Company
42    symbol = rdfSingle(OV.symbol,'symbol')
43    cik = rdfSingle(OV.secCik,'cik')
44    companyName = rdfSingle(OV.companyName)
45    address = rdfSingle(VCARD.adr)
46
47
48# Above here would typically go in a model.py file and be imported
49##########################################################################
50# Below here goes in the file with business logic agnostic of persistance
51
52c = Company.get_by(symbol='IBM')
53## this will enable us to see that the reads are cached
54import logging 
55log=logging.getLogger('rdfAlchemy')
56## comment out to quite debug messages
57log.setLevel(logging.DEBUG)
58
59def test_1():
60    c=Company.get_by(cik="0000051143")
61    assert c.symbol == "IBM"
62
63## list Companies
64for c in Company.ClassInstances():
65    print "%s has an SEC symbol of %s" % (c.companyName, c.cik)
66print ''
67
68
69def test_2():
70    ## Add a descriptor on the fly
71    Company.stockDescription = rdfSingle(OV.stockDescription,'stockDescription')
72
73    assert  c.companyName == c[OV.companyName]
74   
75   
76## add another descriptor on the fly
77Company.industry = rdfSingle(OV.yindustry,'industry')
78
79## add an attribute (from the database)
80c = Company.get_by(symbol = 'JAVA')
81c.industry = 'Computer stuff'
82
83def test_3():
84    ## delete an attribute (from the database)
85    c = Company.get_by(symbol = 'IBM')
86    assert c.industry == "Diversified Computer Systems"
87    del c.industry
88    assert c.industry <> "Diversified Computer Systems"
89    c = Company.get_by(symbol = 'IBM')
90    assert not c.industry
91   
92def test_creating():
93    c1=Company()
94    c2=Company(OV.A)
95    c3=Company('<http://owl.openvest.org/2005/10/Portfolio#A>')
96    c4=Company('_:xyz123')
97    #assert c2==c3
98    assert c2.resUri == c3.resUri
99    assert c4.resUri == BNode('xyz123')
100
101# write out the new n3 file to see the changes
102c.db.serialize('example-out.n3',format='n3')
Note: See TracBrowser for help on using the browser.