Skip to content

Instantly share code, notes, and snippets.

@Halicea
Last active August 29, 2015 14:11
Show Gist options
  • Save Halicea/1f96c5d5fda447e1ac73 to your computer and use it in GitHub Desktop.
Save Halicea/1f96c5d5fda447e1ac73 to your computer and use it in GitHub Desktop.
import sys
sys.path.append('../')
from x4c.api import (PropertyDefinition, PropertySetDefinition,
ElementDefinition, ManagerDefinition)
from x4c.types import Numeric, String
from x4c.store import JsonStore
from x4c.registry import Registry
Money = Numeric
Percent = Numeric
Enum = String
ContactType = String # Enum(["person", "company"])
def gen_parts_def():
prDescription = PropertyDefinition("prDescription", String)
prProductNum = PropertyDefinition("prProductNum", String)
prVendorName = PropertyDefinition("prVendorName", String)
psVendor = PropertySetDefinition("psVendor",
[prDescription,
prProductNum,
prVendorName])
prListPrice = PropertyDefinition("prListPrice", Money)
prDiscount = PropertyDefinition("prDiscount", Percent)
psPricing = PropertySetDefinition("psPricing", [prListPrice, prDiscount])
emPart = ElementDefinition("emPart", base=psVendor, req=[psPricing])
mgrParts = ManagerDefinition("mgrParts", base=emPart)
return mgrParts
def gen_contacts_def():
prName = PropertyDefinition("prName", String)
prContactType = PropertyDefinition("prContactType", ContactType)
psName = PropertySetDefinition("psName", [prName, prContactType])
prFirstname = PropertyDefinition("prFirstname", String)
prSurname = PropertyDefinition("prSurname", String)
psPerson = PropertySetDefinition("psPerson", [prFirstname, prSurname])
emContact = ElementDefinition("emContact",
base=psName, opt=[psPerson])
mgrContacts = ManagerDefinition("mgrContacts", base=emContact)
return mgrContacts
class TestDemoRegistry(object):
registry = None
store = None
store_path = './demo_registry.json'
def setuUp(self):
self.store = JsonStore(self.store_path)
self.registry = Registry()
self.registry.load(self.store)
def create_sample_data(self):
c_def = gen_contacts_def()
p_def = gen_parts_def()
c_def.registry = self.registry
p_def.registry = self.registry
self.registry.register(c_def)
self.registry.register(p_def)
c1 = c_def.build()
c1.emContact.psName.prName = 'Costa'
c1.emContact.psName.prContactType = 'person'
c1.emContact.psPerson.prFirstname = 'Costa'
c1.emContact.psPerson.prSurname = 'Halicea'
c2 = c_def.build()
c2.emContact.psName.prName = 'Darko'
c1.emContact.psName.prContactType = 'person'
c2.emContact.psPerson.prFirstname = 'Darko'
c2.emContact.psPerson.prSurname = 'Mitrevski'
p1 = p_def.build()
p1.emPart.psVendor.prDescription = 'SSD Drive'
p1.emPart.psVendor.prVendorName = 'Dell'
p1.emPart.psVendor.prProductNum = 'DELL-1'
p1.emPart.psPricing.prListPrice = 120
p1.emPart.psPricing.prDiscount = 10
p2 = p_def.build()
p2.emPart.psVendor.prDescription = 'Sata Drive'
p2.emPart.psVendor.prVendorName = 'Dell'
p2.emPart.psVendor.prProductNum = 'DELL-2'
p2.emPart.psPricing.prListPrice = 80
p2.emPart.psPricing.prDiscount = 2
for obj in [c1, c2, p1, p2]:
self.registry.register(obj)
def test_obj_are_loaded(self):
self.create_sample_data()
self.registry.save(self.store)
store2 = JsonStore(self.store_path)
reg2 = Registry()
reg2.load(store2)
managers = reg2.select('*')
elements = reg2.select('*', '*')
sets = reg2.select('*', '*', '*')
properties = reg2.select('*', '*', '*', '*')
import pdb; pdb.set_trace() # XXX BREAKPOINT
print [x for x in managers]
demo = TestDemoRegistry()
demo.setuUp()
demo.test_obj_are_loaded()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment