Ejemplo n.º 1
0
    def test_basic_wrapping(self):
        """Test basic model wrapping."""
        c = connect(*from_env())
        col = c.test_db.test_collection
        uuids = [uuid(), uuid(), uuid()]
        col.save({'docid': 0, 'uuid': uuids[0]})
        col.save({'docid': 1, 'uuid': uuids[1]})
        col.save({'docid': 2, 'uuid': uuids[2]})

        d1 = col.find_one({'docid': 0})
        self.assertEqual(type(d1), dict)
        self.assertEqual(d1['uuid'], uuids[d1['docid']])

        class Foo(Model):
            collection = col.full_name

        # sanity check the correctness of these documents
        d1 = col.find_one({'docid': 0})
        self.assertEqual(type(d1), Foo)
        self.assertEqual(d1.docid, 0)
        for i in range(3):
            d = col.find_one({'docid': i})
            self.assertEqual(d.uuid, uuids[d.docid])

        d2 = col.find_one({'docid': 1})
        d2.uuid = uuid()
        d2.save()

        d3 = col.find_one({'docid': 1})
        self.assertEqual(d3.uuid, d2.uuid)
        self.assertTrue(d3.uuid != uuids[d3.docid])
Ejemplo n.º 2
0
    def test_lists(self):
        """Test that nested lists get un-documented."""
        # NOTE, the saves here would raise an exception and fail the test
        # if the underlying manipulator was not functioning properly
        c = connect(*from_env())
        col = c.test_db.test_collection

        class Foo(Model):
            collection = col.full_name

        col.save({'foo': [{'one': 1}, 'two', {'three': {'3': 4}}]})
        col.save({
            'foo': [{
                'one': [1, 2, {
                    'three': 3,
                    'four': [1, 2, 3, {
                        'five': [5]
                    }]
                }]
            }]
        })

        self.assertEqual(Foo.find().count(), 2)

        for f in Foo.find():
            f.save()
Ejemplo n.º 3
0
    def test_validation(self):
        c = connect(*from_env())
        col = c.test_db.test_collection

        class Foo(Model):
            collection = col.full_name
            spec = {
                'docid': Field(type=int, default=0),
                'req': Field(required=True),
                'enum': Field(type=['foo', 'bar'], default='foo'),
                'baz': Field(type=float),
                'any' : Field(type=(int, float, long)),
            }

        # this should work
        f = Foo.new(baz=10.4)
        f.save()

        # f should also have _id set now..
        self.assertEqual(len(f.keys()), 5)
        self.assertEqual(f.docid, 0)
        self.assertEqual(f.req, None)
        self.assertAlmostEqual(f.baz, 10.4)

        f.baz = 10
        self.assertRaises(ValueError, f.save)

        f.baz = 1.0
        f.save()

        f.enum = 'baz'
        self.assertRaises(ValueError, f.save)

        f.enum = 'bar'
        f.save()

        del f['req']
        self.assertRaises(ValueError, f.save)

        f.req = "okay"
        f.save()

        f.docid = 1234L
        self.assertRaises(ValueError, f.save)

        f.docid = 0
        f.any = 1234
        f.save()
        f.any = 1234L
        f.save()
        f.any = 123.4
        f.save()
Ejemplo n.º 4
0
    def test_validation(self):
        c = connect(*from_env())
        col = c.test_db.test_collection

        class Foo(Model):
            collection = col.full_name
            spec = {
                'docid': Field(type=int, default=0),
                'req': Field(required=True),
                'enum': Field(type=['foo', 'bar'], default='foo'),
                'baz': Field(type=float),
                'any': Field(type=(int, float, long)),
            }

        # this should work
        f = Foo.new(baz=10.4)
        f.save()

        # f should also have _id set now..
        self.assertEqual(len(f.keys()), 5)
        self.assertEqual(f.docid, 0)
        self.assertEqual(f.req, None)
        self.assertAlmostEqual(f.baz, 10.4)

        f.baz = 10
        self.assertRaises(ValueError, f.save)

        f.baz = 1.0
        f.save()

        f.enum = 'baz'
        self.assertRaises(ValueError, f.save)

        f.enum = 'bar'
        f.save()

        del f['req']
        self.assertRaises(ValueError, f.save)

        f.req = "okay"
        f.save()

        f.docid = 1234L
        self.assertRaises(ValueError, f.save)

        f.docid = 0
        f.any = 1234
        f.save()
        f.any = 1234L
        f.save()
        f.any = 123.4
        f.save()
Ejemplo n.º 5
0
    def test_dicts(self):
        """Test that nested dicts get un-documented."""
        # NOTE, the saves here would raise an exception and fail the test
        # if the underlying manipulator was not functioning properly
        c = connect(*from_env())
        col = c.test_db.test_collection

        class Foo(Model):
            collection = col.full_name

        col.save({'foo': {'bar': {'baz': 1}}})
        col.save({'foo': 1, 'bar': {'baz': 1}})

        self.assertEqual(Foo.find().count(), 2)

        for f in Foo.find():
            f.save()
Ejemplo n.º 6
0
    def test_son_manipulator(self):
        """Test the SONManipulator, only with compatible pymongo versions."""
        from micromongo.backend import require_manipulator
        if not require_manipulator:
            return
        c = connect(*from_env())
        col = c.test_db.test_collection
        col.save({'docid': 17, 'subdoc': {'test': 1}})

        class Foo(Model):
            collection = col.full_name

        # first, test that saving this mess works once we hvae the class
        col.save({'docid': 18, 'subdoc': {'test': 1}})

        d = col.find_one({'docid': 18})
        d.subdoc.test = 3
        d.save()

        d2 = col.find_one({'docid': 18})
        self.assertEqual(d2.subdoc.test, 3)
Ejemplo n.º 7
0
    def test_reiteration(self):
        """Test that cursors can be re-iterated."""
        c = connect(*from_env())
        col = c.test_db.test_collection

        class Foo(Model):
            collection = col.full_name

        col.save({'foo': [{'one': 1}, 'two', {'three': {'3': 4}}]})
        col.save({
            'foo': [{
                'one': [1, 2, {
                    'three': 3,
                    'four': [1, 2, 3, {
                        'five': [5]
                    }]
                }]
            }]
        })

        foos = Foo.find()

        self.assertEqual(len(list(foos)), 2)
        self.assertEqual(len(list(foos)), 2)
Ejemplo n.º 8
0
 def tearDown(self):
     from micromongo.models import AccountingMeta
     AccountingMeta.collection_map = {}
     c = connect(*from_env())
     # dropping the db makes tests time consuming
     c.test_db.drop_collection('test_collection')
Ejemplo n.º 9
0
 def tearDown(self):
     from micromongo.models import AccountingMeta
     AccountingMeta.collection_map = {}
     c = connect(*from_env())
     # dropping the db makes tests time consuming
     c.test_db.drop_collection('test_collection')