def testBatchPut(self): '''Tests if puts in batches actually works''' import time import uuid @key("id") class Profile(Model): id = String(required = True, indexed = True) fullname = String(indexed = True) bookmarks = Map(String, URL) profile = Profile(id = str(uuid.uuid4()), fullname = "Iroiso Ikpokonte", bookmarks={}) profile.save() l = [] for i in range(500): profile = Profile(id = str(i), fullname = "Iroiso Ikpokonte", bookmarks={}) profile.bookmarks["google"] = "http://google.com" profile.bookmarks["twitter"] = "http://twitter.com" l.append(profile) start = time.time() print '' print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" with Level.All: self.db.saveMany(Settings.default(),*l) print "Time Taken to put 500 Profiles: %s secs" % (time.time() - start) print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" cursor = self.connection cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT COUNT(*) FROM Profile;") count = cursor.fetchone()[0] print "Count: ", count self.assertTrue(count == 501)
def tearDown(self): '''Release resources that have been allocated''' try: self.db.clear() Schema.Clear() self.connection.execute("DROP KEYSPACE %s" % Settings.keyspace()) self.connection.close() except Exception as e: print e
def testCountForNonExistentModel(self): '''Show that counts work for Models that have not been saved''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) query = CqlQuery(Book, "SELECT COUNT(*) FROM Book;") result = query.fetchone() self.connection.execute("USE %s" % Settings.keyspace()) self.connection.execute("SELECT COUNT(*) FROM Book;") correct = self.connection.fetchone()[0] self.assertTrue(result == correct)
def testOtherCommonTypeKeyWork(self): '''Shows that keys of other common types work''' @key("id") class Message(Model): id = Integer(indexed = True) message = String(indexed = True) cursor = self.connection self.db.save(Message(id=1, message="Something broke damn")) cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT id, message FROM Message WHERE KEY='1'") self.assertTrue(cursor.rowcount == 1) row = cursor.fetchone() print(row) self.assertTrue(row[0] == '1' and row[1] == "Something broke damn")
def testTTL(self): '''Tests if put() supports ttl in columns''' import time @key("id") class House(Model): id = String(required = True, indexed = True) fullname = String(indexed = True, ttl = 2) cursor = self.connection profile = House(id = "1234", fullname = "Iroiso Ikpokonte") self.db.save(profile) time.sleep(3) #=> Sleep for 3 secs and see if you can still find it in the datastore cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT fullname FROM House WHERE KEY=1234;") row = cursor.fetchone() self.assertTrue(row[0] == None)
def testPut(self): '''Tests if Lisa.put() actually stores the model to Cassandra''' @key("id") class Profile(Model): id = String(required = True, indexed = True) fullname = String(indexed = True) cursor = self.connection profile = Profile(id = "1234", fullname = "Iroiso Ikpokonte") self.db.save(profile) cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT id, fullname FROM Profile WHERE KEY=1234;") self.assertTrue(cursor.rowcount == 1) row = cursor.fetchone() self.assertTrue(row[0] == "1234" and row[1] == "Iroiso Ikpokonte") assert profile.key().complete() == True # Make sure the object has a complete Key assert profile.key().saved
def testCount(self): '''Shows that count based queries work''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) for i in range(500): book = Book(name = i, author="Anne Rice") book.save() query = CqlQuery(Book, "SELECT COUNT(*) FROM Book;") result = query.fetchone() self.connection.execute("USE %s" % Settings.keyspace()) self.connection.execute("SELECT COUNT(*) FROM Book;") correct = self.connection.fetchone()[0] print "Results: ", (result, correct) self.assertTrue(result == correct)
def testDelete(self): '''Shows that deletes work as expected''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) book = Book(name = "Pride", author="Anne Rice") book.save() cursor = self.connection cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT name, author FROM Book WHERE KEY=Pride") print cursor.description row = cursor.fetchone() print "Row Contents: ", row[0] self.assertTrue(row[0] == "Pride") Book.delete('Pride') cursor.execute("SELECT name FROM Book WHERE KEY=Pride") row = cursor.fetchone() print "Deleted row: %s" % row self.assertTrue(row[0] == None)
def testSave(self): '''Shows that save works''' @key("id") class Profile(Model): id = String(required = True, indexed = True) fullname = String(indexed = True) bookmarks = Map(String, URL) cursor = self.connection profile = Profile(id = "1234", fullname = "Iroiso Ikpokonte", bookmarks={}) profile.bookmarks["google"] = "http://google.com" profile.bookmarks["twitter"] = "http://twitter.com" profile.save() # Save to the datastore cursor.execute("Use %s" % Settings.keyspace()) cursor.execute("SELECT id, fullname FROM Profile WHERE KEY=1234;") self.assertTrue(cursor.rowcount == 1) row = cursor.fetchone() print "Row Contents: ", row self.assertTrue(row[0] == u"1234" and row[1] == u"Iroiso Ikpokonte") assert profile.key().complete() == True # Make sure the object has a complete Key assert profile.key().saved == True
def testDelete(self): '''Tests if Lisa.delete() works well''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) book = Book(name = "Pride", author="Anne Rice") self.db.save(book) cursor = self.connection cursor.execute("USE %s" % Settings.keyspace()) cursor.execute("SELECT name, author FROM Book WHERE KEY=Pride") #print cursor.description row = cursor.fetchone() self.assertTrue(row[0] == "Pride") k = Key(Settings.default(), 'Book', 'Pride') self.db.delete(k) cursor.execute("SELECT name FROM Book WHERE KEY=Pride") row = cursor.fetchone() print "Deleted row: %s" % row self.assertTrue(row[0] == None) # Make sure that Reads for Lisa return null too. results = self.db.read(k, FetchMode.Property) self.assertFalse(results)
def testCreate(self): '''Tests if Lisa.create() actually creates a Keyspace and ColumnFamily in Cassandra''' @key("name") class Person(Model): name = String("Homer Lisa", indexed = True) twitter = URL("http://twitter.com/homer", indexed = True) self.db.create(Person()); #=> Quantum Leap; This was the first time I tested my assumptions on Homer self.assertRaises(Exception, lambda : self.connection.execute("CREATE KEYSPACE %s" % Settings.keyspace())) self.assertRaises(Exception, lambda : self.connection.execute("CREATE COLUMNFAMILY Person;")) self.assertRaises(Exception, lambda : self.connection.execute("CREATE INDEX ON Person(twitter);")) self.assertRaises(Exception, lambda : self.connection.execute("CREATE INDEX ON Person(name);"))