[conduit: 68/138] Add some infrastructure for working out sane combinations of dataproviders



commit 3638b6f43ceecc52e01f705cce039e48e3c0ebb5
Author: John Carr <john carr unrouted co uk>
Date:   Sun May 3 04:12:06 2009 -0700

    Add some infrastructure for working out sane combinations of dataproviders
---
 test/soup/data/__init__.py  |   33 +++++++++++++++++++++++++++++++++
 test/soup/data/contact.py   |    2 ++
 test/soup/data/event.py     |    2 ++
 test/soup/data/file.py      |    2 ++
 test/soup/data/music.py     |    2 ++
 test/soup/data/note.py      |    2 ++
 test/soup/data/photo.py     |    2 ++
 test/soup/data/video.py     |    2 ++
 test/soup/test_datatypes.py |    5 +++++
 9 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/test/soup/data/__init__.py b/test/soup/data/__init__.py
index 94ea391..0d70ae4 100644
--- a/test/soup/data/__init__.py
+++ b/test/soup/data/__init__.py
@@ -1,5 +1,7 @@
 import os, sys, glob
 
+from conduit.datatypes import DataType, File
+
 class DataWrapper(object):
     """
     This class provides a wrapper around some test data.
@@ -31,6 +33,37 @@ class DataWrapper(object):
         """ Modify a DataType object randomly """
         return obj
 
+    @classmethod
+    def get_datatype(cls):
+        return cls.wraps
+
+    @classmethod
+    def get_compatible_datatypes(cls):
+        """ Yields DataType classes that are (probably) compatible with this one
+
+            We can't rely on type converter system to make sane convertablity choices,
+            or we'd end up in a situation where we tried to convert a vcard into an mp3
+            (vcard to file, but file could convert to mp3. doh).
+
+            Instead we work out convertability by looking 'down class' (we assume there is a
+            conversion route from GoogleContact to Contact.
+
+            We also make some bold assumptions about being able to convert to and from files """
+        yield cls.get_datatype()
+
+        # Yuck. Assume compatibiliy with File.
+        yield File.File
+
+        tovisit = list(cls.get_datatype().__bases__)
+        while len(tovisit):
+            n = tovisit.pop(0)
+            if not issubclass(n, DataType.DataType):
+                continue
+            if n == File.File:
+                continue
+            tovisit.extend(n.__bases__)
+            yield (cls.get_datatype(), n, cls)
+
 def load_modules():
     basepath = os.path.dirname(__file__)
     for root, dirs, files in os.walk(basepath):
diff --git a/test/soup/data/contact.py b/test/soup/data/contact.py
index 225979e..9f7dd16 100644
--- a/test/soup/data/contact.py
+++ b/test/soup/data/contact.py
@@ -5,6 +5,8 @@ from conduit.datatypes import Contact
 
 class ContactWrapper(soup.data.DataWrapper):
 
+    wraps = Contact.Contact
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*.vcard"):
             txt = open(f).read()
diff --git a/test/soup/data/event.py b/test/soup/data/event.py
index a104f70..ad3baf4 100644
--- a/test/soup/data/event.py
+++ b/test/soup/data/event.py
@@ -5,6 +5,8 @@ from conduit.datatypes import Event
 
 class EventWrapper(soup.data.DataWrapper):
 
+    wraps = Event.Event
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*.ical"):
             txt = open(f).read()
diff --git a/test/soup/data/file.py b/test/soup/data/file.py
index 12c6693..68995fe 100644
--- a/test/soup/data/file.py
+++ b/test/soup/data/file.py
@@ -7,6 +7,8 @@ from conduit.datatypes import File
 class FileWrapper(soup.data.DataWrapper):
     """ Provides access to sample files and generated files """
 
+    wraps = File.File
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*"):
             yield File.File(URI=f)
diff --git a/test/soup/data/music.py b/test/soup/data/music.py
index c5a6ab0..218db42 100644
--- a/test/soup/data/music.py
+++ b/test/soup/data/music.py
@@ -4,6 +4,8 @@ from conduit.datatypes import Audio
 
 class MusicWrapper(soup.data.DataWrapper):
 
+    wraps = Audio.Audio
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*.mp3"):
             a = Audio.Audio(URI=f)
diff --git a/test/soup/data/note.py b/test/soup/data/note.py
index 2697ac3..03b81ec 100644
--- a/test/soup/data/note.py
+++ b/test/soup/data/note.py
@@ -5,6 +5,8 @@ from conduit.datatypes import Note
 
 class NoteWrapper(soup.data.DataWrapper):
 
+    wraps = Note.Note
+
     def iter_samples(self):
         #FIXME: This is not very useful
         for f in self.get_files_from_data_dir("*"):
diff --git a/test/soup/data/photo.py b/test/soup/data/photo.py
index cc0117e..d59cad6 100644
--- a/test/soup/data/photo.py
+++ b/test/soup/data/photo.py
@@ -4,6 +4,8 @@ from conduit.datatypes import Photo
 
 class PhotoWrapper(soup.data.DataWrapper):
 
+    wraps = Photo.Photo
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*.png"):
             p = Photo.Photo(URI=f)
diff --git a/test/soup/data/video.py b/test/soup/data/video.py
index fafac2a..40a1da4 100644
--- a/test/soup/data/video.py
+++ b/test/soup/data/video.py
@@ -4,6 +4,8 @@ from conduit.datatypes import Video
 
 class VideoWrapper(soup.data.DataWrapper):
 
+    wraps = Video.Video
+
     def iter_samples(self):
         for f in self.get_files_from_data_dir("*.mpg"):
             a = Video.Video(URI=f)
diff --git a/test/soup/test_datatypes.py b/test/soup/test_datatypes.py
index f76aec3..9beb6a9 100644
--- a/test/soup/test_datatypes.py
+++ b/test/soup/test_datatypes.py
@@ -36,6 +36,11 @@ def make_testcase(wrp):
             assert obj.get_hash() == clone.get_hash()
             assert obj.get_rid() == clone.get_rid()
 
+        def test_compatible_datatypes(self):
+            """ Should be compatible with other datatypes """
+            others = list(self.wrapper.get_compatible_datatypes())
+            assert len(others) > 1
+
     return TestDatatype
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]