def test_return_correct_length(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.assertEquals(len(self.document), 2)
Пример #2
0
 def run(self, document_id):
     """
     This method is called by Celery, and should not be overridden.
     It will call the `process` method with a dictionary containing all the
     document information and will update de database with results.
     """
     document = MongoDictAdapter(doc_id=document_id,
             host=config.MONGODB_CONFIG['host'],
             port=config.MONGODB_CONFIG['port'],
             database=config.MONGODB_CONFIG['database'])
     # Create a dictionary out of our document. We could simply pass
     # it on to the process method, but for now we won't let the user
     # manipulate the MongoDict directly.
     dic = {k: v for k, v in document.iteritems()}
     result = self.process(dic)
     document.update(result)
     return document_id
Пример #3
0
 def run(self, document_id):
     """
     This method is called by Celery, and should not be overridden.
     It will call the `process` method with a dictionary containing all the
     document information and will update de database with results.
     """
     document = MongoDictAdapter(doc_id=document_id,
                                 host=config.MONGODB_CONFIG['host'],
                                 port=config.MONGODB_CONFIG['port'],
                                 database=config.MONGODB_CONFIG['database'])
     # Create a dictionary out of our document. We could simply pass
     # it on to the process method, but for now we won't let the user
     # manipulate the MongoDict directly.
     dic = {k: v for k, v in document.iteritems()}
     result = self.process(dic)
     document.update(result)
     return document_id
    def test_iterating_through_keys_does_not_bring_keys_from_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3
        keys = [k for k in self.document]

        self.assertIn('key_1', keys)
        self.assertIn('key_2', keys)
        self.assertNotIn('key_3', keys)

        self.assertEquals(['key_1', 'key_2'], self.document.keys())
    def test_clear_should_not_remove_keys_for_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.document.clear()

        with self.assertRaises(KeyError):
            self.document['key_1']
            self.document['key_2']

        self.assertEqual(other_document['other_key'], 3)
Пример #6
0
 def setUp(self):
     self.fake_id = '1234'
     self.document = MongoDictAdapter(self.fake_id, database=self.db_name)
     self.db = pymongo.Connection()[self.db_name]
Пример #7
0
class TestMongoDictAdapter(unittest.TestCase):
    db_name = 'test_mongodictbyid'

    def setUp(self):
        self.fake_id = '1234'
        self.document = MongoDictAdapter(self.fake_id, database=self.db_name)
        self.db = pymongo.Connection()[self.db_name]

    def tearDown(self):
        self.db.main.remove({})

    @classmethod
    def tearDownClass(cls):
        pymongo.MongoClient().drop_database(cls.db_name)

    def test_creating_a_new_key_should_saved_the_information(self):
        self.document['new_key'] = 'value'
        stored_value = self.db.main.find_one(
            {'_id': 'id:{}:new_key'.format(self.fake_id)})
        self.assertIsNotNone(stored_value)
        # This decodes the value with the defaults for MongoDict
        decoded_value = pickle.loads(str(stored_value['v']))
        self.assertEqual(decoded_value, 'value')

    def test_reading_an_existing_key_should_read_saved_information(self):
        encoded_value = Binary(pickle.dumps(
            'value', protocol=pickle.HIGHEST_PROTOCOL))

        self.db.main.insert(
                {'_id': 'id:{}:key'.format(self.fake_id), 'v': encoded_value})

        self.assertEqual(self.document['key'], 'value')

    def test_deleting_an_existing_key_should_delete_saved_information(self):
        encoded_value = Binary(pickle.dumps(
            'value', protocol=pickle.HIGHEST_PROTOCOL))

        self.db.main.insert(
                {'_id': 'id:{}:key'.format(self.fake_id), 'v': encoded_value})

        self.assertEqual(self.document['key'], 'value')
        del self.document['key']

        stored_value = self.db.main.find_one(
            {'_id': 'id:{}:key'.format(self.fake_id)})
        self.assertIsNone(stored_value)

    def test_iterating_through_keys_does_not_bring_keys_from_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3
        keys = [k for k in self.document]

        self.assertIn('key_1', keys)
        self.assertIn('key_2', keys)
        self.assertNotIn('key_3', keys)

        self.assertEquals(['key_1', 'key_2'], self.document.keys())

    def test_clear_should_not_remove_keys_for_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.document.clear()

        with self.assertRaises(KeyError):
            self.document['key_1']
            self.document['key_2']

        self.assertEqual(other_document['other_key'], 3)

    def test_return_correct_length(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.assertEquals(len(self.document), 2)

    def test_contains(self):
        self.document['key'] = 1
        self.assertIn('key', self.document)
        self.assertNotIn('inexistent_key', self.document)

    def test_has_key(self):
        self.document['key'] = 1
        self.assertTrue(self.document.has_key('key'))
        self.assertFalse(self.document.has_key('inexistent_key'))
Пример #8
0
 def setUp(self):
     app.conf.update(CELERY_ALWAYS_EAGER=True)
     self.fake_id = '1234'
     self.document = MongoDictAdapter(self.fake_id, database=self.db_name)
     self.db = pymongo.Connection()[self.db_name]
 def setUp(self):
     self.fake_id = '1234'
     self.document = MongoDictAdapter(self.fake_id, database=self.db_name)
     self.db = pymongo.Connection()[self.db_name]
class TestMongoDictAdapter(unittest.TestCase):
    db_name = 'test_mongodictbyid'

    def setUp(self):
        self.fake_id = '1234'
        self.document = MongoDictAdapter(self.fake_id, database=self.db_name)
        self.db = pymongo.Connection()[self.db_name]

    def tearDown(self):
        self.db.main.remove({})

    @classmethod
    def tearDownClass(cls):
        pymongo.MongoClient().drop_database(cls.db_name)

    def test_creating_a_new_key_should_saved_the_information(self):
        self.document['new_key'] = 'value'
        stored_value = self.db.main.find_one(
            {'_id': 'id:{}:new_key'.format(self.fake_id)})
        self.assertIsNotNone(stored_value)
        # This decodes the value with the defaults for MongoDict
        decoded_value = pickle.loads(str(stored_value['v']))
        self.assertEqual(decoded_value, 'value')

    def test_reading_an_existing_key_should_read_saved_information(self):
        encoded_value = Binary(
            pickle.dumps('value', protocol=pickle.HIGHEST_PROTOCOL))

        self.db.main.insert({
            '_id': 'id:{}:key'.format(self.fake_id),
            'v': encoded_value
        })

        self.assertEqual(self.document['key'], 'value')

    def test_deleting_an_existing_key_should_delete_saved_information(self):
        encoded_value = Binary(
            pickle.dumps('value', protocol=pickle.HIGHEST_PROTOCOL))

        self.db.main.insert({
            '_id': 'id:{}:key'.format(self.fake_id),
            'v': encoded_value
        })

        self.assertEqual(self.document['key'], 'value')
        del self.document['key']

        stored_value = self.db.main.find_one(
            {'_id': 'id:{}:key'.format(self.fake_id)})
        self.assertIsNone(stored_value)

    def test_iterating_through_keys_does_not_bring_keys_from_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3
        keys = [k for k in self.document]

        self.assertIn('key_1', keys)
        self.assertIn('key_2', keys)
        self.assertNotIn('key_3', keys)

        self.assertEquals(['key_1', 'key_2'], self.document.keys())

    def test_clear_should_not_remove_keys_for_other_docs(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.document.clear()

        with self.assertRaises(KeyError):
            self.document['key_1']
            self.document['key_2']

        self.assertEqual(other_document['other_key'], 3)

    def test_return_correct_length(self):
        self.document['key_1'] = 1
        self.document['key_2'] = 2
        other_document = MongoDictAdapter('other_id', database=self.db_name)
        other_document['other_key'] = 3

        self.assertEquals(len(self.document), 2)

    def test_contains(self):
        self.document['key'] = 1
        self.assertIn('key', self.document)
        self.assertNotIn('inexistent_key', self.document)

    def test_has_key(self):
        self.document['key'] = 1
        self.assertTrue(self.document.has_key('key'))
        self.assertFalse(self.document.has_key('inexistent_key'))