basic Message class, with tests
This commit is contained in:
parent
1bc2583de8
commit
353109e105
6 changed files with 35 additions and 17 deletions
|
|
@ -6,10 +6,15 @@ from scopes import shape
|
||||||
|
|
||||||
class Message(shape.Record):
|
class Message(shape.Record):
|
||||||
|
|
||||||
headFields = ['domain', 'action', 'class', 'item']
|
headFields = ('domain', 'action', 'class', 'item')
|
||||||
|
headFieldsAsKeywords = {'domain', 'action', 'class'}
|
||||||
|
|
||||||
customer = None
|
customer = None
|
||||||
|
|
||||||
def __repr__(self):
|
def addr(self):
|
||||||
return f'<{self.__class__.__name__}: {self.headValues}, {self.data}>'
|
hv = self.headValues
|
||||||
|
return hv[0], hv[2], hv[3]
|
||||||
|
|
||||||
|
def categ(self):
|
||||||
|
return self.domain, self.head['class']
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,20 @@
|
||||||
|
|
||||||
"""really abstract basic data shape definitions"""
|
"""really abstract basic data shape definitions"""
|
||||||
|
|
||||||
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
class Record:
|
class Record:
|
||||||
|
|
||||||
headFields = [] # to be defined by subclass
|
headFields = () # to be defined by subclass
|
||||||
|
headFieldsAsKeywords = set()
|
||||||
|
defaultHeadValue = ''
|
||||||
|
|
||||||
def __init__(self, *headValues, data=None, **kw):
|
def __init__(self, *headValues, data=None, **kw):
|
||||||
self.head = {}
|
self.head = dict((k, self.defaultHeadValue) for k in self.headFields)
|
||||||
for k, v in kw.items():
|
for k, v in kw.items():
|
||||||
if k in self.headFields:
|
self.set(k, v)
|
||||||
self.head[k] = kw.pop(k)
|
|
||||||
for ix, hv in enumerate(headValues):
|
for ix, hv in enumerate(headValues):
|
||||||
self.head[self.headFields[ix]] = hv
|
self.set(self.headFields[ix], hv)
|
||||||
for k in self.headFields:
|
|
||||||
if self.head.get(k) is None:
|
|
||||||
self.head[k] = ''
|
|
||||||
setattr(self, k, self.head[k])
|
|
||||||
self.data = data or {}
|
self.data = data or {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -26,7 +25,9 @@ class Record:
|
||||||
def set(self, attr, value):
|
def set(self, attr, value):
|
||||||
if attr in self.headFields:
|
if attr in self.headFields:
|
||||||
if value is None:
|
if value is None:
|
||||||
value = ''
|
value = self.defaultHeadValue
|
||||||
|
elif attr in self.headFieldsAsKeywords:
|
||||||
|
value = k(value)
|
||||||
self.head[attr] = value
|
self.head[attr] = value
|
||||||
setattr(self, attr, value)
|
setattr(self, attr, value)
|
||||||
else:
|
else:
|
||||||
|
|
@ -41,7 +42,7 @@ class Record:
|
||||||
self.data.update(data)
|
self.data.update(data)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (self.__class__.__name__, self.asDict())
|
return f'<{self.__class__.__name__}: {self.headValues}, {self.data}>'
|
||||||
|
|
||||||
def asDict(self):
|
def asDict(self):
|
||||||
return dict(head=self.head, data=self.data)
|
return dict(head=self.head, data=self.data)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,9 @@ class Symbols:
|
||||||
setattr(self, k, sym)
|
setattr(self, k, sym)
|
||||||
return sym
|
return sym
|
||||||
|
|
||||||
|
def __call__(self, k):
|
||||||
|
return getattr(self, k)
|
||||||
|
|
||||||
keywords = Symbols()
|
keywords = Symbols()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import os, sys
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from scopes.storage import tracking
|
from scopes.storage import tracking
|
||||||
import transaction
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
@ -26,7 +25,7 @@ def storage():
|
||||||
tr01 = tracking.Track('t01', 'john')
|
tr01 = tracking.Track('t01', 'john')
|
||||||
tr01.update(dict(activity='testing'))
|
tr01.update(dict(activity='testing'))
|
||||||
trid01 = tracks.save(tr01)
|
trid01 = tracks.save(tr01)
|
||||||
transaction.commit()
|
storage.commit()
|
||||||
return storage
|
return storage
|
||||||
|
|
||||||
def async_client():
|
def async_client():
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,13 @@
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from scopes.core.message import Message
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
|
def test_message():
|
||||||
|
from scopes.core.message import Message
|
||||||
|
msg = Message(k.test, k.data, 'person', '4711',
|
||||||
|
data=dict(firstName='Bob', lastName='Miller'))
|
||||||
|
assert msg.head['class'] is k.person
|
||||||
|
assert msg.addr() == (k.test, k.person, '4711')
|
||||||
|
assert msg.categ() == (k.test, k.person)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,6 @@ def test_keywords():
|
||||||
assert k.dummy is k.dummy
|
assert k.dummy is k.dummy
|
||||||
assert k.dummy is not k.other
|
assert k.dummy is not k.other
|
||||||
assert getattr(k, "abcd"[-2:]) is k.cd
|
assert getattr(k, "abcd"[-2:]) is k.cd
|
||||||
|
assert k('dummy') is k.dummy
|
||||||
|
assert k('new') is k('new')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue