Exemplo n.º 1
0
    def test_connect_to_mongo_with_invalid_host(self):
        connection = Connection("localhostgggg", 27080)

        error_occurred = False
        try:
            connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT)
        except ConnectionError:
            error_occurred = True

        self.assertTrue(error_occurred)
Exemplo n.º 2
0

# CONFIG
# SERVER = 'http://averrin.meteor.com'
ORLANGUR_SERVER = 'en.averr.in'
ORLANGUR_PORT = 27080
ORLANGUR_USER = '******'
ORLANGUR_PASWD = 'aqwersdf'
# API_ROOT = 'collectionapi'
# API = '%s/%s/' % (SERVER, API_ROOT)
# TOKEN = '3d714fb7-a389-4748-a781-2f9329fbc280'
url = 'http://%s:%s/orlangur/_authenticate' % (ORLANGUR_SERVER, ORLANGUR_PORT)
data = {'username': ORLANGUR_USER, 'password': ORLANGUR_PASWD}
req = urllib2.Request(url, urllib.urlencode(data))
urllib2.urlopen(req).read()
connection = Connection(ORLANGUR_SERVER, ORLANGUR_PORT)
# connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT)
db = connection.orlangur


def make_request(collection):
    url = API + collection
    action('Get %s...' % collection)
    req = urllib2.Request(url)
    req.add_header('X-Auth-Token', TOKEN)
    return urllib2.urlopen(req)


get_projects = lambda: db.en_projects.find()
get_servers = lambda: db.en_servers.find()
get_keys = lambda: db.en_keys.find()
Exemplo n.º 3
0
 def test_connection_initialized_properly(self):
     connection = Connection(SLEEPY_HOST, SLEEPY_PORT)
     self.assertEqual(SLEEPY_PORT, connection.get_port())
     self.assertEqual(SLEEPY_HOST, connection.get_host())
Exemplo n.º 4
0
 def test_connect_to_mongo_with_valid_info(self):
     connection = Connection(SLEEPY_HOST, SLEEPY_PORT)
     self.assertTrue(connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT))
Exemplo n.º 5
0
 def setUp(self):
     self.connection = Connection(SLEEPY_HOST, SLEEPY_PORT)
     self.connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT)
     self.db = self.connection.mongate_test_db
     self.collection = self.db.test_collection
     self._reset_testing_db()
Exemplo n.º 6
0
class TestCollection(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(SLEEPY_HOST, SLEEPY_PORT)
        self.connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT)
        self.db = self.connection.mongate_test_db
        self.collection = self.db.test_collection
        self._reset_testing_db()

    def _reset_testing_db(self):
        self.collection.remove()

    def test_collection_insert(self):
        oid = self._insert_data()
        self.assertTrue(oid)

    def test_collection_insert_with_invalid_characters(self):
        oid = self.collection.insert({"name": "Benjamin & Company=bar", "profession": "Software Developer?"})
        self.assertTrue(oid)

    def _insert_data(self):
        self.collection.insert({"name": "Benjamin", "age": 27, "profession": "Software Developer"})

        return self.collection.insert({"apple": "tasty", "banana": "phallic"})

    def test_collection_find_by_oid(self):
        oid = self._insert_data()

        retrieved_collection = self.collection.find({"_id": oid})

        self.assertEqual("tasty", retrieved_collection[0]["apple"])

    def test_find_with_no_results(self):
        self.assertFalse(self.collection.find_one({"Elvis": True}))

    def test_find_with_invalid_characters(self):
        self.assertFalse(self.collection.find({"Elvis & Company": True}))
        self.assertFalse(self.collection.find_one({"Elvis & Company": True}))

    def test_collection_find_by_key(self):
        self._insert_data()

        retrieved_collection = self.collection.find({"apple": "tasty"})

        self.assertEqual("tasty", retrieved_collection[0]["apple"])

    def test_collection_find_with_invalid_characters(self):
        oid = self.collection.insert({"name": "Benjamin & Company", "profession": "Software Developer?"})

        retrieved_collection = self.collection.find({"name": "Benjamin & Company"})

        self.assertEqual("Software Developer?", retrieved_collection[0]["profession"])

    def test_collection_find_with_nonoid_id(self):
        self.collection.insert({"_id": 100, "name": "Benjamin", "profession": "Software Developer"})

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        self.assertEqual(100, retrieved_collection[0]["_id"])

    def test_collection_find_key_that_doesnt_exist(self):
        self._insert_data()

        retrieved_collection = self.collection.find({"apple": "foobar"})

        self.assertFalse(retrieved_collection)

    def test_remove_collection(self):
        self._insert_data()

        self.collection.remove({"apple": "tasty"})

        retrieved_collection = self.collection.find({"apple": "tasty"})

        self.assertFalse(retrieved_collection)

    def test_remove_only_removes_the_right_things(self):
        self._insert_data()

        self.collection.remove({"apple": "tasty"})

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        self.assertTrue(retrieved_collection)

    def test_remove_with_no_criteria_removes_whole_collection(self):
        self._insert_data()

        self.collection.remove()

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        self.assertFalse(retrieved_collection)

    def test_update_with_key(self):
        self._insert_data()

        self.collection.update({"name": "Benjamin"}, {"$inc": {"age": 1}})

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        self.assertEqual(28, retrieved_collection[0]["age"])
        self.assertEqual(1, len(retrieved_collection))

    def test_update_with_invalid_characters(self):
        self._insert_data()

        self.collection.update({"name": "Benjamin", "invalid": "&&&"}, {"$inc": {"age": 1}})

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        self.assertEqual(27, retrieved_collection[0]["age"])

    def test_save_updates_document_if_it_already_exists(self):
        self._insert_data()

        retrieved_collection = self.collection.find({"name": "Benjamin"})

        retrieved_collection[0]["name"] = "Ben"
        self.collection.save(retrieved_collection[0])

        retrieved_collection = self.collection.find({"age": 27})

        self.assertEqual("Ben", retrieved_collection[0]["name"])
        self.assertEqual(1, len(retrieved_collection))

    def test_save_creates_document_if_it_does_not_exist(self):
        self.collection.save({"name": "Bob", "age": "old"})

        retrieved_collection = self.collection.find_one({"age": "old"})

        self.assertEqual("Bob", retrieved_collection["name"])

    def test_count_with_zero_results(self):
        count = self.collection.count({"foo": "bar"})
        self.assertEqual(0, count)

    def test_count_with_zero_results(self):
        self.collection.save({"name": "Bob", "age": "old"})
        self.collection.save({"name": "Bob", "age": "old"})
        self.collection.save({"name": "Bob", "age": "old"})

        count = self.collection.count({"name": "Bob"})
        self.assertEqual(3, count)
Exemplo n.º 7
0
class TestCollection(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(SLEEPY_HOST, SLEEPY_PORT)
        self.connection.connect_to_mongo(host=MONGO_HOST, port=MONGO_PORT)
        self.db = self.connection.mongate_test_db
        self.collection = self.db.test_collection
        self._reset_testing_db()

    def _reset_testing_db(self):
        self.collection.remove()
        
    def test_batch_insert(self):
        
        self._perform_batch_insertion()
        
        retrieved_document_1, retrieved_document_2 = self._retrieve_documents()
        
        self.assertEqual(2, retrieved_document_1['bar']) 
        self.assertEqual('apple', retrieved_document_2['banana'])
        
    def _perform_batch_insertion(self):
        batch = Batch(self.collection, self.connection)
        
        batch.add_insert({
            'batch_insert_1': 3,
            'bar': 2
        })
        
        batch.add_insert({
            'batch_insert_2': 'banana',
            'banana': 'apple'
        })
        
        batch.add_insert({
            'batch_insert_3': 29,
            'bar': 2
        })
        
        batch.execute()
        return batch
        
    def _retrieve_documents(self):
        retrieved_document_1 = self.collection.find_one({'batch_insert_1': 3})
        retrieved_document_2 = self.collection.find_one({'batch_insert_2': 'banana'})
        return retrieved_document_1, retrieved_document_2
        
    def test_batch_insert_with_invalid_characters(self):
        batch = Batch(self.collection, self.connection)
        
        batch.add_insert({
            'batch_insert_5': 'Ben & Company',
            'bar': 2
        })
        
        batch.execute()
        self.assertTrue(self.collection.find_one({'batch_insert_5': 'Ben & Company'}))
        
    def test_batch_update(self):
        self._perform_batch_insertion()
        
        batch = Batch(self.collection, self.connection)
        
        batch.add_update(
            {
                'batch_insert_1': 3
            },
            {
                "$inc": {
                    "bar": 1
                }
            }
        )
        
        batch.add_update(
            {
                'batch_insert_2': 'banana'
            },
            {
                '$set': {
                    'banana': 'tasty'
                }
            }
        )
        
        batch.execute()
        
        retrieved_document_1, retrieved_document_2 = self._retrieve_documents()
        
        self.assertEqual(3, retrieved_document_1['bar']) 
        self.assertEqual('tasty', retrieved_document_2['banana'])
        
    def test_batch_update_with_invalid_characters(self):
        self._perform_batch_insertion()
        
        batch = Batch(self.collection, self.connection)
        
        batch.add_update(
            {
                'batch_insert_1': 3
            },
            {
                "$inc": {
                    "bar": 1
                }
            }
        )
        
        exception = False
        try:
            batch.execute()
        except Exception:
            exception = True
            
        self.assertFalse(exception)
        
    def test_batch_find(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)
        
        batch.add_find({
            'bar': 2
        })
        
        batch.add_find({
            'batch_insert_2': 'banana'
        })
        
        result = batch.find()
        self.assertEqual(2, len(result))
        self.assertEqual(2, len(result[0]))
        self.assertEqual('apple', result[1][0]['banana'])
        
    def test_batch_find_with_invalid_characters(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)
        
        batch.add_find({
            'bar': 'ben & company'
        })
        
        self.assertEqual(0, len(batch.find()[0]))
        
    def test_batch_find_with_no_results(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)
        
        batch.add_find({
            'bar': 23434
        })
        
        batch.add_find({
            'batch_insert_2': 'abananasdf'
        })
        
        result = batch.find()
        self.assertFalse(result[0])
        self.assertFalse(result[1])
        
    def test_batch_find_with_no_jobs(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)        
        result = batch.find()
        self.assertFalse(result)
        
    def test_batch_one_remove(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)
        batch.add_remove({
            'batch_insert_1': 3
        })
        batch.execute()
        self.assertFalse(self.collection.find({'batch_insert_1': 3}))
        self.assertTrue(self.collection.find({'batch_insert_3': 29}))
        
    def test_batch_multiple_removes(self):
        self._perform_batch_insertion()
        batch = Batch(self.collection, self.connection)
        batch.add_remove({
            'batch_insert_1': 3
        })
        batch.add_remove({
            'batch_insert_3': 29
        })
        batch.execute()
        self.assertFalse(self.collection.find({'batch_insert_1': 3}))
        self.assertFalse(self.collection.find({'batch_insert_3': 29}))
        self.assertTrue(self.collection.find({'batch_insert_2': 'banana'}))