← Back to team overview

oship-dev team mailing list archive

[Branch ~oship-dev/oship/devel] Rev 513: Enforced invariants in DvText class and created a representation method

 

------------------------------------------------------------
revno: 513
fixes bug(s): https://launchpad.net/bugs/609156
committer: Wagner Francisco Mezaroba <wagner@wagner-laptop>
branch nick: oship
timestamp: Sun 2010-11-21 00:17:24 -0200
message:
  Enforced invariants in DvText class and created a representation method
modified:
  src/oship/openehr/rm/datatypes/text/__init__.py
  src/oship/openehr/rm/datatypes/text/tests/dvtext.py


--
lp:oship
https://code.launchpad.net/~oship-dev/oship/devel

Your team OSHIP Development Team is subscribed to branch lp:oship.
To unsubscribe from this branch go to https://code.launchpad.net/~oship-dev/oship/devel/+edit-subscription
=== modified file 'src/oship/openehr/rm/datatypes/text/__init__.py'
--- src/oship/openehr/rm/datatypes/text/__init__.py	2010-11-21 01:25:08 +0000
+++ src/oship/openehr/rm/datatypes/text/__init__.py	2010-11-21 02:17:24 +0000
@@ -80,13 +80,23 @@
 
     grok.implements(IDvText)
 
-    def __init__(self,value,mappings=None,formatting=None,hyperlink=None,language=None,encoding=None):
-        if value is not None and value != '':
-            self.value = value
-        else:
-            raise AttributeError("DvText.value")
+    def __init__(self,value,mappings=None,formatting=None,hyperlink=None,language=None,encoding=None, terminologyService=None):
+        if value is None or len(value.strip()) == 0:
+            raise AttributeError(u'value attribute must not be None or empty')
+        if '\n' in value or '\r' in value:
+            raise AttributeError(u'carriage return and line feed characters not allowed in value attribute')
         if mappings is not None and len(mappings) == 0:
             raise AttributeError(u'mappings must not be empty')
+        if formatting is not None and len(formatting.strip()) == 0:
+            raise AttributeError(u'formatting must not be empty')
+
+        #TODO we need to remove this consistence when we finish the terminologyService
+        if terminologyService is not None:
+            if language is not None and not terminologyService.codeSetForId(u'code_set_id_languages').hasCode(language):
+                raise AttributeError(u'language must be in the language code set of the terminology service')
+            if encoding is not None and not terminologyService.codeSetForId(u'code_set_id_character_sets').hasCode(encoding):
+                raise AttributeError(u'encoding must be in the character code set of the terminology service')
+        self.value=value
         self.mappings=mappings
         self.formatting = formatting
         self.hyperlink = hyperlink
@@ -110,6 +120,16 @@
     def __str__(self):
         return self.value
 
+    def __repr__(self):
+        str = 'DvText('
+        str += 'value=' + value
+        str += 'mappings=' + mappings
+        str += 'formatting=' + formatting
+        str += 'hyperlink=' + hyperlink
+        str += 'language=' + language
+        str += 'encoding=' + encoding
+        str += ')'
+        return str
 
 class CodePhrase(grok.Model):
     """

=== modified file 'src/oship/openehr/rm/datatypes/text/tests/dvtext.py'
--- src/oship/openehr/rm/datatypes/text/tests/dvtext.py	2010-11-19 14:15:48 +0000
+++ src/oship/openehr/rm/datatypes/text/tests/dvtext.py	2010-11-21 02:17:24 +0000
@@ -20,8 +20,77 @@
     def testShouldNotInitializeWithEmptymappings(self):
         self.assertRaises(AttributeError, DvText, value(), [])
 
+    def testShouldInitializeWithNullFormatting(self):
+        text = DvText(value())
+
+    def testShouldInitializeWithNotEmptyFormatting(self):
+        text = DvText(value(), formatting=u'formatting')
+
+    def testShouldNotInitializeWithEmptyFormatting(self):
+        self.assertRaises(AttributeError, DvText, value(), formatting=u'  ')
+
+    def testShouldNotInitializeWithValueContainingLineFeed(self):
+        self.assertRaises(AttributeError, DvText, u'aa\naa')
+
+    def testShouldNotInitializeWithValueContainingCarriageReturn(self):
+        self.assertRaises(AttributeError, DvText, u'aa\raa')
+
+    def testShouldNotInitializeWithNoneValue(self):
+        self.assertRaises(AttributeError, DvText, None)
+
+    def testShouldNotInitializeWithEmptyValue(self):
+        self.assertRaises(AttributeError, DvText, u'   ')
+
+    def testShouldInitializeWithNullLanguage(self):
+        text = DvText(value(), language=None, terminologyService=DummyTerminologyService(False))
+
+    def testShouldNotInitializeWithInvalidLanguage(self):
+        self.assertRaises(AttributeError, DvText, value(), language=language(), terminologyService=DummyTerminologyService(False))
+
+    def testShouldInitializeWithValidLanguage(self):
+        text = DvText(value(), language=language(), terminologyService=DummyTerminologyService(True))
+
+    def testShouldInitializeWithNullEncoding(self):
+        text = DvText(value(), encoding=None, terminologyService=DummyTerminologyService(False))
+
+    def testShouldNotInitializeWithInvalidEncoding(self):
+        self.assertRaises(AttributeError, DvText, value(), encoding=encoding(), terminologyService=DummyTerminologyService(False))
+
+    def testShouldInitializeWithValidEncoding(self):
+        text = DvText(value(), encoding=encoding(), terminologyService=DummyTerminologyService(True))
+
+
+
 def value():
     return 'some text'
 
 def mappings():
     return [TermMapping(None, '<', None)]
+
+def language():
+    terminologyId = TerminologyId(u'terminology(1.0)')
+    codeString = u'english'
+    return CodePhrase(terminologyId, codeString)
+
+def encoding():
+    terminologyId = TerminologyId(u'terminology(1.0)')
+    codeString = u'utf8'
+    return CodePhrase(terminologyId, codeString)
+
+
+class DummyTerminologyService(object):
+
+    def __init__(self, returnValue):
+        self.returnValue = returnValue
+
+    def codeSetForId(self, id):
+        return DummyCodeSetAccess(self.returnValue)
+
+class DummyCodeSetAccess(object):
+
+    def __init__(self, returnValue):
+        self.returnValue = returnValue
+
+    def hasCode(self, code):
+        return self.returnValue
+