1. Chris Perkins
  2. sprox-0.7

Commits

Alessandro Molina  committed e491a93

Improve relationships management, now all tests should pass

  • Participants
  • Parent commits 9746a91
  • Branches default

Comments (0)

Files changed (4)

File sprox/mg/provider.py

View file
             join = field.related
             iter = join.query.find()
             rel_cls = join
+
         view_field = self.get_view_field_name(rel_cls, view_names)
         return [ (str(obj._id), getattr(obj, view_field)) for obj in iter ]
 
     def get_default_values(self, entity, params):
         return params
 
+    def _related_object_id(self, value):
+        if isinstance(value, MappedClass):
+            return value._id
+        return ObjectId(value)
+
     def _cast_value(self, entity, key, value):
         
         #handles the case where an record with no id is being created
         if key in relations:
             related = field.related
             if isinstance(value, list):
-                return related.query.find({'_id':{'$in':[ObjectId(i) for i in value]}}).all()
+                return related.query.find({'_id':{'$in':[self._related_object_id(i) for i in value]}}).all()
             else:
-                return self.get_obj(related, {'_id':value})
+                return self.get_obj(related, {'_id':self._related_object_id(value)})
 
         field = getattr(field, 'field', None)
         if field is not None:
             if field.type is S.DateTime or field.type is datetime.datetime:
                 return datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
-            if field.type is S.Binary:
+            elif field.type is S.Binary:
                 return bson.Binary(value)
+            elif field.type in (S.Int, int):
+                return int(value)
             elif field.type is S.Bool:
                 if value in ('true', 'false'):
                     return value == 'true' and True or False
         fields = self.get_fields(entity)
         for key, value in params.iteritems():
             if key not in fields:
-                continue;
+                continue
             value = self._cast_value(entity, key, value)
             if value is not None:
                 try:
             if value is not None:
                 try:
                     setattr(obj,key,value)
-                except:
+                except TypeError:
                     pass
         return obj
 

File sprox/test/mg/base.py

View file
     user.email_address = u"asdf@asdf.com"
     user.password = u"asdf"
 
-    arvada = Town(_id=1,name=u'Arvada')
+    arvada = Town(name=u'Arvada')
     session.flush()
     user.town_id = arvada._id
 
-    Town(_id=2,name=u'Denver')
-    Town(_id=3,name=u'Golden')
-    Town(_id=4,name=u'Boulder')
+    Town(name=u'Denver')
+    Town(name=u'Golden')
+    Town(name=u'Boulder')
 
     #test_table.insert(values=dict(BLOB=FieldStorage('asdf', StringIO()).value)).execute()
     #user_reference_table.insert(values=dict(user_id=user.user_id)).execute()

File sprox/test/mg/model_relational.py

View file
     class __mongometa__:
         name = 'town_rs'
     
-    _id = FieldProperty(int)
+    _id = FieldProperty(S.ObjectId)
     name = FieldProperty(str)
     
 

File tests/test_mgormprovider.py

View file
 
     def test__widget__(self):
         rendered = self.base.__widget__()
-        assert_in_xml("""<tr class="odd" id="submit.container" title="" >
+        assert_in_xml("""<tr class="even" id="submit.container" title="" >
             <td class="labelcol">
                 <label id="submit.label" for="submit" class="fieldlabel"></label>
             </td>
 
     def test_get_dropdown_options_fk(self):
         options = self.provider.get_dropdown_options(User, 'town')
-        eq_(set(options), set((('1', u'Arvada'), ('2', u'Denver'), ('3', u'Golden'), ('4', u'Boulder'))))
+        for _id, name in options:
+            town = Town.query.find({'name':name}).first()
+            assert str(town._id) == str(_id)
 
     def test_get_dropdown_options_fk_multi(self):
         options = self.provider.get_dropdown_options(Document, 'category')
         params = {}
         params['email_address'] = u'asdf@asdf.commy'
         params['created'] = '2008-3-30 12:21:21'
-        params['user_id'] = 2
+        params['_id'] = 2
         new_user = self.provider.update(User, params, omit_fields=['email_address', 'groups'])
         q_user = self.session.query(User).get(2)
 
         users = User.query.find().all()
         assert len(users) == 0
 
-    def test_create_with_unicode_cast_to_int(self):
-        self.provider.create(User, dict(user_id=u'34', user_name=u'something'))
-
     def test_create_with_DateTime(self):
         self.provider.create(Document, dict(edited='2011-03-30 12:21:21'))
 
-    # expected failure; need many-to-many support and writing into RelationProperty
-    @raises(TypeError)
     def test_create_relationships_with_wacky_relation(self):
         obj = Group.query.find().first()
         user = User.query.find().first()
-        params = {'_id':obj._id, 'users':user}
-        self.provider.update(Group, params)
-        assert user in obj.users
+        self.provider.update(Group, {'_id':obj._id, 'users':[user._id]})
+        assert user._id == obj.users[0].user_id
 
-    # expected failure; ming does not yet support writing into RelationProperties
-    @raises(TypeError)
     def test_create_relationships_remove_groups(self):
         obj = Group.query.find().first()
-        obj.users.append(self.user)
-        self.provider.update(User, {'user_id':self.user.user_id, 'groups':[]})
-        session.flush()
-        user = User.query.find().get(1)
+        self.provider.update(User, {'_id':self.user._id, 'groups':[]})
         assert user not in obj.users
 
-    # expected failure; ming does not yet support writing into RelationProperties
-    @raises(TypeError)
-    def test_create_relationships_remove_town(self):
-        town = Town.query.find().first()
+    def test_create_relationships_change_town(self):
+        town = Town.query.find({'name':'Arvada'}).first()
 
         self.user.town = town
         self.session.flush()
 
-        self.provider.update(User, {'user_id':self.user.user_id, 'town':None})
-        assert self.user.town is None
+        self.provider.update(User, {'_id':self.user._id, 'town':Town.query.find({'name':'Denver'}).first()})
+        assert self.user.town.name == 'Denver'
 
 #dojo tests
 from sprox.dojo.formbase import DojoAddRecordForm, DojoEditableForm, DojoFormBase
     def test_formbase(self):
         base = DojoUserForm(session)
         rendered = base.__widget__()
-        assert_in_xhtml("""<tr class="even" id="email_address.container" title="" >
+        assert_in_xhtml("""<tr class="odd" id="email_address.container" title="" >
                <td class="labelcol">
                    <label id="email_address.label" for="email_address" class="fieldlabel">Email Address</label>
                </td>
     def test_addrecord(self):
         base = DojoUserAddRecordForm(session)
         rendered = base.__widget__()
-        assert_in_xhtml("""<tr class="even" id="email_address.container" title="" >
+        assert_in_xhtml("""<tr class="odd" id="email_address.container" title="" >
                <td class="labelcol">
                    <label id="email_address.label" for="email_address" class="fieldlabel">Email Address</label>
                </td>
     def test_editableform(self):
         base = DojoUserEditableForm(session)
         rendered = base.__widget__()
-        assert_in_xhtml("""<tr class="even" id="email_address.container" title="" >
+        assert_in_xhtml("""<tr class="odd" id="email_address.container" title="" >
                <td class="labelcol">
                    <label id="email_address.label" for="email_address" class="fieldlabel">Email Address</label>
                </td>