Exemple #1
0
class CacheTests(unittest.TestCase):

    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()



    def test_iterate_dogs(self):

        me = Person(name="paul")
        self.db.put(me)

        for i in range(500):
            dog = Dog(name="dog_%s" % i, owner=me)
            self.db.put(dog)

        counter = 0
        for dog in me.dogs:
            counter += 1

        self.assertEqual(counter, 500)
Exemple #2
0
class CacheTests(unittest.TestCase):
    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()

    def test_iterate_dogs(self):

        me = Person(name="paul")
        self.db.put(me)

        for i in range(500):
            dog = Dog(name="dog_%s" % i, owner=me)
            self.db.put(dog)

        counter = 0
        for dog in me.dogs:
            counter += 1

        self.assertEqual(counter, 500)
Exemple #3
0
class IndexTests(unittest.TestCase):

    db = None

    def setUp(self):
        filepath = os.path.join(THIS_DIR, "animal_views.js")
        mapper = ClassMapper([Dog, Cat, Person, JackRussell], designs=[filepath])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()


    def test_uniqueness(self):

        paul = Person(name="paul")
        self.db.put(paul)
        dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456")
        dog2 = Dog.create(self.db, name="fly", owner_id=paul.key)

        # raises if setting one value
        self.assertRaises(FamUniqueError, dog2.update, {"kennel_club_membership": "123456"})
        self.assertIsNone(dog2.kennel_club_membership)

        # raises if creating a new one
        self.assertRaises(FamUniqueError, Dog.create, self.db, name="steve", owner_id=paul.key, kennel_club_membership="123456")

    def test_uniqueness_delete(self):

        paul = Person(name="paul")
        self.db.put(paul)
        dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456")

        dog1.delete(self.db)
        dog2 = Dog.create(self.db, name="another", owner_id=paul.key, kennel_club_membership="123456")


    def test_get_unique(self):

        paul = Person(name="paul")
        self.db.put(paul)
        dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456")

        dog2 = Dog.get_unique_instance(self.db, "kennel_club_membership", "123456")
        self.assertIsNotNone(dog2)
        self.assertTrue(dog2.kennel_club_membership == "123456")
Exemple #4
0
class MapperValidationTests(unittest.TestCase):

    def test_make_a_validator(self):

        mapper = ClassMapper([Dog, Cat, Person, JackRussell])
        validator = ModelValidator(classes=[Dog, Cat, Person, JackRussell])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        cat = Cat(name="puss", owner_id=paul.key)

        self.assertRaises(FamValidationError, cat.save, self.db)

        self.db.session.close()


    def test_make_a_validator_from_modules(self):

        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(classes=[Dog, Cat, Person, JackRussell])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        #missing legs
        cat = Cat(name="puss", owner_id=paul.key)

        self.assertRaises(FamValidationError, cat.save, self.db)

        #additional properties
        def failing_cat():
            cat = Cat(name="puss", owner_id=paul.key, legs=2, collar="green")

        self.assertRaises(FamValidationError, failing_cat)
        dog = Dog(name="fly")
        self.db.put(dog)
        dog.tail = "long"
        self.db.put(dog)

        self.db.session.close()

        # print dog.as_json()


    def test_included_refs_from_in_validator(self):
        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(modules=[test01])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)

        paul_id = paul.key
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        paul = Person.get(self.db, paul_id)

        paul.save(self.db)

        self.db.session.close()



    def test_string_format(self):

        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(modules=[test01])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")


        cat = Cat(name="puss", owner_id=paul.key, legs=3, email="*****@*****.**")
        cat.save(self.db)
        cat.email = "paulglowinthedark.co.uk"
        self.assertRaises(FamValidationError, self.db.put, cat)

        self.db.session.close()
Exemple #5
0
class IndexTests(unittest.TestCase):

    db = None

    def setUp(self):
        filepath = os.path.join(THIS_DIR, "animal_views.js")
        mapper = ClassMapper([Dog, Cat, Person, JackRussell],
                             designs=[filepath])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()

    def test_create_index(self):
        filepath = os.path.join(THIS_DIR, "animal_views.js")

        as_dict = self.db.mapper._js_design_as_doc(filepath)

        expected = {
            "_id": "_design/animal_views",
            "views": {
                "cat_legs": {
                    "map":
                    "function(doc) {\n  if (doc.type == \"cat\") {\n    emit(doc.legs, doc);\n  }\n}"
                }
            }
        }

        self.assertEqual(as_dict, expected)

    def test_query_view(self):

        paul = Person(name="Paul")
        self.db.put(paul)
        cat1 = Cat(owner=paul, legs=4)
        self.db.put(cat1)
        cat2 = Cat(owner=paul, legs=3)
        self.db.put(cat2)
        three_legged_cats = Cat.all_with_n_legs(self.db, 3)
        self.assertEqual(len(three_legged_cats), 1)

        self.assertEqual(three_legged_cats[0].key, cat2.key)

    def test_long_polling(self):
        paul = Person(name="Paul")
        self.db.put(paul)
        cat1 = Cat(owner=paul, legs=4)
        self.db.put(cat1)
        cat2 = Cat(owner=paul, legs=3)
        self.db.put(cat2)
        three_legged_cats = self.db.view("animal_views/cat_legs", key=3)
        self.assertEqual(len(three_legged_cats), 1)
        self.assertEqual(three_legged_cats[0].key, cat2.key)
Exemple #6
0
class IndexTests(unittest.TestCase):

    db = None

    def setUp(self):
        filepath = os.path.join(THIS_DIR, "animal_views.js")
        mapper = ClassMapper([Dog, Cat, Person, JackRussell], designs=[filepath])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()

    def test_create_index(self):
        filepath = os.path.join(THIS_DIR, "animal_views.js")

        as_dict = self.db.mapper._js_design_as_doc(filepath)

        expected = {
            "_id": "_design/animal_views",
            "views": {
                "cat_legs": {
                    "map": "function(doc) {\n  if (doc.type == \"cat\") {\n    emit(doc.legs, doc);\n  }\n}"
                }
            }
        }

        self.assertEqual(as_dict, expected)


    def test_query_view(self):

        paul = Person(name="Paul")
        self.db.put(paul)
        cat1 = Cat(owner=paul, legs=4)
        self.db.put(cat1)
        cat2 = Cat(owner=paul, legs=3)
        self.db.put(cat2)
        three_legged_cats = Cat.all_with_n_legs(self.db, 3)
        self.assertEqual(len(three_legged_cats), 1)

        self.assertEqual(three_legged_cats[0].key, cat2.key)


    def test_long_polling(self):
        paul = Person(name="Paul")
        self.db.put(paul)
        cat1 = Cat(owner=paul, legs=4)
        self.db.put(cat1)
        cat2 = Cat(owner=paul, legs=3)
        self.db.put(cat2)
        three_legged_cats = self.db.view("animal_views/cat_legs", key=3)
        self.assertEqual(len(three_legged_cats), 1)
        self.assertEqual(three_legged_cats[0].key, cat2.key)
Exemple #7
0
class MapperValidationTests(unittest.TestCase):
    def test_make_a_validator(self):

        mapper = ClassMapper([Dog, Cat, Person, JackRussell])
        validator = ModelValidator(None,
                                   classes=[Dog, Cat, Person, JackRussell])
        self.db = CouchDBWrapper(mapper,
                                 COUCHDB_URL,
                                 COUCHDB_NAME,
                                 reset=True,
                                 validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)

        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        cat = Cat(name="puss", owner_id=paul.key)

        self.assertRaises(FamValidationError, cat.save, self.db)

        self.db.session.close()

    def test_make_a_validator_from_modules(self):

        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(mapper)
        self.db = CouchDBWrapper(mapper,
                                 COUCHDB_URL,
                                 COUCHDB_NAME,
                                 reset=True,
                                 validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        #missing legs
        cat = Cat(name="puss", owner_id=paul.key)

        self.assertRaises(FamValidationError, cat.save, self.db)

        #additional properties
        def failing_cat():
            cat = Cat(name="puss", owner_id=paul.key, legs=2, collar="green")

        self.assertRaises(FamValidationError, failing_cat)
        dog = Dog(name="fly")
        self.db.put(dog)
        dog.tail = "long"
        self.db.put(dog)

        self.db.session.close()

        # print dog.as_json()

    def test_included_refs_from_in_validator(self):
        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(mapper)
        self.db = CouchDBWrapper(mapper,
                                 COUCHDB_URL,
                                 COUCHDB_NAME,
                                 reset=True,
                                 validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)

        paul_id = paul.key
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        paul = Person.get(self.db, paul_id)

        paul.save(self.db)

        self.db.session.close()

    def test_string_format(self):

        mapper = ClassMapper([], modules=[test01])
        validator = ModelValidator(mapper)
        self.db = CouchDBWrapper(mapper,
                                 COUCHDB_URL,
                                 COUCHDB_NAME,
                                 reset=True,
                                 validator=validator)
        self.db.update_designs()

        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=4)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

        cat = Cat(name="puss",
                  owner_id=paul.key,
                  legs=3,
                  email="*****@*****.**")
        cat.save(self.db)
        cat.email = "paulglowinthedark.co.uk"
        self.assertRaises(FamValidationError, self.db.put, cat)

        self.db.session.close()
Exemple #8
0
class CacheTests(unittest.TestCase):

    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        pass


    def test_cache_saves(self):

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)

        got = self.db.get(dog.key)

        self.assertTrue(got is not None)


    def test_cache_doesnt_save(self):
        # doesnt save until were done

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)
            got = self.db.get(dog.key)
            self.assertTrue(got is None)

        got = self.db.get(dog.key)
        self.assertTrue(got is not None)

    def test_cache_gets(self):
        # doesnt save until were done

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertEqual(id(dog), id(fetched))


    def test_cache_gets_from_db(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertNotEqual(id(dog), id(fetched))
            fetched_again = dbc.get(dog.key)

            self.assertEqual(id(fetched), id(fetched_again))

    def test_cache_gets_change_from_db(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertNotEqual(id(dog), id(fetched))
            fetched_again = dbc.get(dog.key)
            self.assertTrue(fetched_again is not None)

            self.assertEqual(id(fetched), id(fetched_again))
            dog.name = "fly"
            self.db.put(dog)

            fetched_yet_again = dbc.get(dog.key)
            self.assertTrue(fetched_yet_again is not None)

            self.assertEqual(id(fetched), id(fetched_yet_again))
            self.assertEqual(fetched_yet_again.name, 'fly')
            fetched_yet_again.name = "bluebottle"
            db_fetched = self.db.get(dog.key)
            self.assertEqual(db_fetched.name, 'fly')

        db_fetched = self.db.get(dog.key)
        self.assertEqual(db_fetched.name, 'fly')


    def test_saves_putted(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            fetched.name = "bluebottle"
            dbc.put(fetched)

        db_fetched = self.db.get(dog.key)
        self.assertEqual(db_fetched.name, 'bluebottle')



    def test_refs_from(self):

        with buffered_db(self.db) as dbc:

            person = Person(name="paul")
            dbc.put(person)

            dog = Dog(name="woofer", owner=person)
            dbc.put(dog)

            self.assertEqual(person.dogs, [dog])