Esempio n. 1
0
 def test_memcache_cache(self):
     settings.MongoStores["unittest"]["cachetype"] = CacheType.Volatile
     store = MongoStore("unittest")
     
     actual = self._memcache.get("unittest:test")
     self.assertEqual("X", actual)
     
     store.save({"_id": "test", "hello": "world"})
     actual = self._mongo.find_one("test")
     self.assertEqual({"_id": "test", "hello": "world"}, actual)
     
     actual = self._memcache.get("unittest:test")
     expected = serialize({"_id": "test", "hello": "world"})
     self.assertEqual(expected, actual)
     
     self._mongo.delete_one("test")
     actual = self._mongo.find_one("test")
     self.assertEqual(None, actual)
     
     actual = store.find_one("test")
     self.assertEqual({"_id": "test", "hello": "world"}, actual)
     
     store.save({"_id": "test", "hello": "world"})
     store.update({"_id": "test"}, {"$set":{"hello": "universe"}})
     actual = self._memcache.get("unittest:test")
     expected = serialize({"_id": "test", "hello": "universe"})
     self.assertEqual(expected, actual)
     
     store.delete_one("test")
     actual = store.find_one("test")
     self.assertEqual(None, actual)
     actual = self._memcache.get("unittest:test")
     self.assertEqual("X", actual)
Esempio n. 2
0
class TweetStore(object):
    """Mongodb datastore for holding tweets.

    """

    def __init__(self):
        self._store = MongoStore("TweetStore")

    def put(self, tweet):
        """Write a tweet to the store.
  
        Args:
	    tweet (twitterlize.tweet.Tweet) : Tweet object

        """
        doc = {"_id": tweet.original_data["id"]}
        doc.update(tweet.original_data)
        if tweet.is_retweet:
            doc["data_from_retweet"] = True
        self._store.save(doc)

    def get(self, tweet_id):
        doc = self._store.find_one(tweet_id)
        if doc:
            return Tweet(doc)
        return None
Esempio n. 3
0
    def test_no_cache(self):
        store = MongoStore("unittest")
        actual = store.save({"_id": "test", "hello": "world"})
        expected = "test"
        self.assertEqual(expected, actual)
        
        actual = store.find_one("test")
        expected = {"_id": "test", "hello": "world"}
        self.assertEqual(expected, actual)
        
        store.save({"_id": "test2", "goodbye": "world"})
        
        actual = list(store.find({}))
        expected = [
            {"_id": "test", "hello": "world"},
            {"_id": "test2", "goodbye": "world"}
        ]
        self.assertEqual(expected, actual)

        actual = list(store.find({"goodbye": {"$exists": True}}))
        expected = [{"_id": "test2", "goodbye": "world"}]
        self.assertEqual(expected, actual)
        
        actual = list(store.find().limit(1))
        expected = [{"_id": "test", "hello": "world"}]
        self.assertEqual(expected, actual)
        
        actual = store.count()
        self.assertEqual(2, actual)
        
        store.delete_one("test")
        actual = list(store.find())
        expected = [{"_id": "test2", "goodbye": "world"}]
        
        store.update({"_id": "test2"}, {"$set":{"goodbye": "universe"}})
        actual = store.find_one("test2")
        expected = {"_id": "test2", "goodbye": "universe"}
        self.assertEqual(expected, actual)
        
        store.update({"_id": "test3"}, {"$set":{"goodbye": "multiverse"}}, upsert=True)
        actual = store.find_one("test3")
        expected = {"_id": "test3", "goodbye": "multiverse"}
        self.assertEqual(expected, actual)