def testSanity(self): '''Tests the sanity of Reference Property''' from homer.core.commons import String print "######## Creating Models ################" @key("name") class Person(Model): name = String(required = True) @key("name") class Book(Model): name = String(required = True, indexed = True) author = Reference(Person) print "Persisting Person" person = Person(name = "sasuke") self.db.save(person) print "Persisting Book" book = Book(name = "Pride", author = person) self.db.save(book) print "Checking Conversion Routine" k = eval(Book.author.convert(person)) self.assertTrue(k == Key(Settings.default(),"Person","sasuke")) with self.assertRaises(BadValueError): print "Checks if Reference accepts other kinds" book.author = Key(Settings.default(), "Book", "House") print "Checking Automatic Reference Read" id = Key(Settings.default(),"Book","Pride") # id.columns = ["name", "author"] found = self.db.read(id, FetchMode.Property) self.assertTrue(found.author.name == "sasuke") self.assertTrue(found.author == person)
def testCountWithFilters(self): '''Show that counts with filters work''' 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", bookmarks={}) profile.bookmarks["google"] = "http://google.com" profile.bookmarks["twitter"] = "http://twitter.com" l.append(profile) start = time.time() with Level.All: self.db.saveMany(Settings.default(),*l) self.assertTrue(Profile.count(fullname="Iroiso") == 500)
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 optionsFor(namespace): '''Returns configuration for a namespace or the default''' found = None namespaces = Settings.namespaces() if namespace not in namespaces: default = Settings.default() found = Settings.namespaces()[default] else: found = Settings.namespaces()[namespace] return found
def Put(cls, namespace, model, key): """Stores Meta Information for a particular class""" from homer.options import Settings if not namespace: namespace = Settings.default() kind = model.__name__ if not namespace in cls.schema: cls.schema[namespace] = WeakValueDictionary() if kind not in cls.schema[namespace]: cls.schema[namespace][kind] = model cls.keys[id(model)] = (namespace, kind, key, ) else: raise NamespaceCollisionError("Model: %s already \ exists in the Namespace: %s" % (model, namespace))
def testReadMode(self): '''Tests if FetchMode works in Reads''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) isbn = String(indexed = True) book = Book(name="Lord of the Rings", author="J.R.R Tolkein", isbn="12345") for n in xrange(500): book[str(n)] = n book.save() print "Book len:" , len(book) k = Key(Settings.default(), "Book", "Lord of the Rings") #k.columns = ["name", "author", "isbn", "titles"] #We'll specify the columns manually for now b = self.db.read(k, FetchMode.All) assert isinstance(b, Book) print "Book len:" , len(b) assert len(b) == len(book) assert b == book
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 testRead(self): '''Tests if Lisa.read() behaves as usual''' @key("name") class Book(Model): name = String(required = True, indexed = True) author = String(indexed = True) isbn = String(indexed = True) book = Book(name="Lord of the Rings", author="J.R.R Tolkein", isbn="12345") self.db.save(book) k = Key(Settings.default(), "Book", "Lord of the Rings") #k.columns = ["name", "author", "isbn", "titles"] #We'll specify the columns manually for now b = self.db.read(k, FetchMode.Property) assert isinstance(b, Book) print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" print b.name print b.author print b.isbn self.assertTrue(b == book) self.assertTrue(b.name == "Lord of the Rings") self.assertTrue(b.author == "J.R.R Tolkein") self.assertTrue(b.isbn == "12345") print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
def setUp(self): default = Settings.default() self.pool = RoundRobinPool(Settings.namespaces().get(default))
def setUp(self): '''Create the Pool''' print "Creating a Pool with the default connections" default = Settings.default() self.pool = RoundRobinPool(Settings.namespaces().get(default))