micromongo icon indicating copy to clipboard operation
micromongo copied to clipboard

Sub-documents return with wrong class (parent?)

Open mattbodman opened this issue 12 years ago • 0 comments

I think I have figured out that this issue is more of a limitation that a bug. Its because the wrapper classes are associated by collection, then there is no way to determine which wrapper to use for a sub-document, so it gets the parent wrapper by default. To store sub-documents then it may be better to use DBRef's instead because then the sub-document's collection is available so it can be correctly wrapped. There is currently an issue with returning DBRef's from mongodb #2.

# with pymongo 2.4.1
# python 2.7.3

import unittest
from micromongo import *
from micromongo.spec import Field

c = connect()

class Program(Model):
    collection = 'tests.programs'
    spec = dict(
            Title=Field()
                )

class Episode(Model):
    collection = 'tests.episodes'
    spec = dict(
            Program=Field()
                )

class TestStuff(unittest.TestCase):
    def setUp(self):
        c.drop_database('tests')

    def tearDown(self):
        c.drop_database('tests')

    def test_sub_doc_return(self):
        p = Program.new(Title=u'bla')
        p.save()
        e = Episode.new(Program=p)
        e.save()
        self.assertTrue(isinstance(e.find_one().Program, Program))  # fails, is type 'Episode' instead

mattbodman avatar Dec 20 '12 00:12 mattbodman