Пример #1
0
class Storage:
    def __init__(self):
        self.disconnect()

    @reporter(
        "connect as user %s with password %s to database (host %s , port %n , database %s , collection %s )",
        defaults=["", "", "localhost", 27017, "snapmesh", "snapmesh"])
    def connect(self, username, password, host, port, database, collection):
        try:
            self.db = MongoDict(host,
                                port,
                                database,
                                collection,
                                auth=(username, password))
            return "ok"
        except:
            return "error: " + str(sys.exc_info()[1])

    @command("disconnect")
    def disconnect(self):
        self.db = None

    @predicate("connected?")
    def is_connected(self):
        return self.db is not None

    @reporter("%s from the collection")
    def get(self, key):
        if self.db is None:
            return "not connected"

        if key in self.db:
            return self.db[key]
        else:
            return ""

    @command("%s as %s into the collection")
    def put(self, value, key):
        if self.db is None:
            return "not connected"

        self.db[key] = value

    @reporter("contents of the collection")
    def list(self):
        if self.db is None:
            return "not connected"

        return "\n".join(self.db.keys())
Пример #2
0
 def test_iter(self):
     for counter in range(10):
         self.collection.insert({'_id': 'test-' + str(counter),
                                 'value': counter})
     my_dict = MongoDict(**self.config)
     keys = []
     for key in my_dict:
         keys.append(key)
     self.assertEquals(len(keys), 10)
     expected_keys = ['test-' + str(counter) for counter in range(10)]
     self.assertEquals(set(keys), set(expected_keys))
     self.assertEquals(set(my_dict.keys()), set(expected_keys))
     results = []
     for key, value in my_dict.iteritems():
         results.append((key, value))
     values = [x[1] for x in results]
     expected_values = list(range(10))
     self.assertEquals(set(values), set(expected_values))
     self.assertEquals(set(my_dict.values()), set(expected_values))
Пример #3
0
    def test_keys_method_should_not_raises_exception_if_more_than_16MB(self):
        '''Should not raise exception if sum of keys is greater 16MB

        Bug reported by @andrebco:
            <https://github.com/turicas/mongodict/issues/10>
        '''
        my_dict = MongoDict(**self.config)
        key_template = ('python-rules' * 100000) + '{}'
        key_byte_count = 0
        key_count = 0
        keys = set()
        while key_byte_count < 20 * 1024 * 1024:  # 20MB > 16MB
            new_key = key_template.format(key_count)
            my_dict[new_key] = 'some value'
            key_byte_count += len(new_key)
            key_count += 1
            keys.add(new_key)
        dict_keys = my_dict.keys()
        self.assertEquals(len(keys), len(dict_keys))
        self.assertTrue(keys == set(dict_keys))
Пример #4
0
    def test_keys_method_should_not_raises_exception_if_more_than_16MB(self):
        '''Should not raise exception if sum of keys is greater 16MB

        Bug reported by @andrebco:
            <https://github.com/turicas/mongodict/issues/10>
        '''
        my_dict = MongoDict(**self.config)
        key_template = ('python-rules' * 100000) + '{}'
        key_byte_count = 0
        key_count = 0
        keys = set()
        while key_byte_count < 20 * 1024 * 1024: # 20MB > 16MB
            new_key = key_template.format(key_count)
            my_dict[new_key] = 'some value'
            key_byte_count += len(new_key)
            key_count += 1
            keys.add(new_key)
        dict_keys = my_dict.keys()
        self.assertEquals(len(keys), len(dict_keys))
        self.assertTrue(keys == set(dict_keys))
Пример #5
0
 def test_iter(self):
     for counter in range(10):
         self.collection.insert({
             '_id': 'test-' + str(counter),
             'value': counter
         })
     my_dict = MongoDict(**self.config)
     keys = []
     for key in my_dict:
         keys.append(key)
     self.assertEquals(len(keys), 10)
     expected_keys = ['test-' + str(counter) for counter in range(10)]
     self.assertEquals(set(keys), set(expected_keys))
     self.assertEquals(set(my_dict.keys()), set(expected_keys))
     results = []
     for key, value in my_dict.iteritems():
         results.append((key, value))
     values = [x[1] for x in results]
     expected_values = list(range(10))
     self.assertEquals(set(values), set(expected_values))
     self.assertEquals(set(my_dict.values()), set(expected_values))