def __str__(self): return self.color + " " + self.brand + " built in " + str(self.year) if __name__ == '__main__': c1 = Car("Red", "Ford", 1974) c2 = Car("Yellow", "Dodge", 2000) c3 = Car("Black", "Toyota", 2005) c4 = Car("Pink", "Chevy", 1985) c5 = Car("Red", "Ferrari", 1990) c6 = Car("Black", "Ford", 1990) searcher = Searcher() fordCars = searcher.fromClass("Car").where("brand = Ford") newCars = searcher.fromClass("Car").where("year >= 2000") blackFords = searcher.fromClass("Car").where("brand = Ford AND color = Black ") blackOrRedCars = searcher.fromClass("Car").where("color = Red OR color = Black") redOrFerrariCars = searcher.fromClass("Car").where("color = Red OR brand = Ferrari") print "====Ford Cars===" for car in fordCars: print car print "====Cars made in 2000 or later===" for car in newCars: print car print "====Black Fords===" for car in blackFords:
class TestSearcher(unittest.TestCase): def setUp(self): self.searcher = Searcher() self.mikeObj = Person("Mike", 15) self.mikeObj2 = Person("Mike", 99) self.mikeObj3 = Person("Mike", 15, "Michael") self.jouhan = Person("Jouhan", 22) self.jouhan1 = Person("Jouhan", 74) self.alex = Person("Alex", 15) self.tim = Person("Tim", 100) self.cat = Animal("Cat", "Red", True) self.fish1 = Animal("Fish", "Blue", False) self.fish2 = Animal("Fish", "Red", False) self.dog = Animal("Dog", "Black", True) self.human = Animal("Human", "White", True) def tearDown(self): #to destroy the database that was created for testing AKA ISOLATION! self.searcher.index.clear() def test_searcher_expecting_multiple_values(self): self.assertEquals(len(self.searcher.fromClass("Person").where("age = 15")), 3) def test_searcher_excepting_no_values(self): self.assertEquals(len(self.searcher.fromClass('Person').where("age = 20")), 0) def test_searcher_excepting_correct_result(self): person = self.searcher.fromClass("Person").where("age = 99")[0] self.assertTrue(self.mikeObj2.__hash__() == person.__hash__()) def test_searcher_equality_search(self): person = self.searcher.fromClass("Person").where("name = Alex")[0] self.assertTrue(person.__hash__(), self.alex.__hash__()) def test_searcher_greater_or_equal_to_search_correct_results(self): people = self.searcher.fromClass("Person").where("age >= 74") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj2.__hash__() in hashes and self.jouhan1.__hash__() in hashes and self.tim.__hash__() in hashes) self.assertEquals(len(people), 3) def test_searcher_greater_to_search_correct_results(self): people = self.searcher.fromClass("Person").where("age > 74") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj2.__hash__() in hashes and self.tim.__hash__() in hashes) self.assertEquals(len(people), 2) def test_searcher_between_search_correct_results(self): people = self.searcher.fromClass("Person").where("age BETWEEN 15 AND 74") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj.__hash__() in hashes and self.mikeObj3.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.alex.__hash__() in hashes and self.jouhan1.__hash__() in hashes) self.assertEquals(len(people), 5) def test_searcher_with_select_two_dicts(self): people = self.searcher.fromClass("Person").select("name, age").where("name = Mike") self.assertTrue({"name" : "Mike", "age" : 15} in people and {"name" : "Mike", "age" : 99} in people) def test_searcher_with_select_and_queries(self): people = self.searcher.fromClass("Person").select("name, age").where("name = Mike AND age = 99") self.assertTrue({"name" : "Mike", "age" : 99} in people) def test_searcher_with_select_two_dicts_no_space(self): people = self.searcher.fromClass("Person").select("name,age").where("name = Mike") self.assertTrue({"name" : "Mike", "age" : 15} in people and {"name" : "Mike", "age" : 99} in people) def test_searcher_with_select_and_queries_no_space(self): people = self.searcher.fromClass("Person").select("name,age").where("name = Mike AND age = 99") self.assertTrue({"name" : "Mike", "age" : 99} in people) self.assertEquals(len(people), 1) def test_searcher_with_select_or_queries(self): people = self.searcher.fromClass("Person").select("name, age").where("name = Alex OR name = Tim") self.assertTrue({"name" : "Alex", "age" : 15} in people and {"name" : "Tim", "age" : 100} in people) self.assertEquals(len(people), 2) def test_searcher_with_select_between_queries(self): people = self.searcher.fromClass("Person").select("name").where("age BETWEEN 70 AND 100") self.assertTrue({"name" : "Jouhan"} in people and {"name" : "Mike"} in people and {"name" : "Tim"} in people) self.assertEquals(len(people), 3) def test_searcher_with_true_value_query(self): animals = self.searcher.fromClass("Animal").where("mammal == True") hashes = [animal.__hash__() for animal in animals] self.assertTrue(self.human.__hash__() in hashes and self.cat.__hash__() in hashes and self.dog.__hash__() in hashes) self.assertEquals(len(animals), 3) def test_searcher_with_false_value_query(self): animals = self.searcher.fromClass("Animal").where("mammal == False") hashes = [animal.__hash__() for animal in animals] self.assertTrue(self.fish1.__hash__() in hashes and self.fish2.__hash__() in hashes) self.assertEquals(len(animals), 2) def test_searcher_change_attributes(self): people = self.searcher.fromClass("Person").where("age = 15") self.assertEquals(len(people), 3) self.alex.age = 16 self.assertTrue(len(self.searcher.fromClass("Person").where("age = 15")) == 2) def test_searcher_or_query(self): people = self.searcher.fromClass("Person").where("name = Jouhan OR name = Mike") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj.__hash__() in hashes and self.mikeObj2.__hash__() and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes) self.assertEquals(len(people), 5) def test_searcher_and_query(self): people = self.searcher.fromClass("Person").where("age = 15 AND name = Mike") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj.__hash__() in hashes and self.alex.__hash__() not in hashes) self.assertEquals(len(people), 2) def test_searcher_multiple_ands_query(self): people = self.searcher.fromClass("Person").where("age = 15 AND name = Mike AND firstName = Michael") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj3.__hash__() in hashes) self.assertEquals(len(people), 1) def test_searcher_multiple_or_query(self): people = self.searcher.fromClass("Person").where("age = 99 OR name = Jouhan OR name = Alex") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj2.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes and self.alex.__hash__() in hashes) self.assertEquals(len(people), 4) def test_searcher_all_query(self): people = self.searcher.fromClass("Person").all() hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj2.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes and self.alex.__hash__() in hashes and self.mikeObj.__hash__() in hashes and self.mikeObj3.__hash__() in hashes and self.tim.__hash__() in hashes) self.assertEquals(len(people), 7) def test_searcher_with_not_equals_query(self): people = self.searcher.fromClass("Person").where("name != Mike") hashes = [person.__hash__() for person in people] self.assertTrue(self.alex.__hash__() in hashes and self.tim.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes) self.assertEquals(len(people), 4) def test_searcher_with_not_equals_and_other_ops_query(self): people = self.searcher.fromClass("Person").where("name != Mike AND age > 15") hashes = [person.__hash__() for person in people] self.assertTrue(self.tim.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes) self.assertEquals(len(people), 3) def test_searcher_with_multiple_not_equals_query(self): people = self.searcher.fromClass("Person").where("name != Mike AND age != 15") hashes = [person.__hash__() for person in people] self.assertTrue(self.tim.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.jouhan1.__hash__() in hashes) self.assertEquals(len(people), 3) def test_searcher_with_multiple_not_equals_select_query(self): people = self.searcher.fromClass("Person").select('age').where("name != Mike AND age != 15") self.assertTrue({"age" : 22} in people and {"age" : 74} in people and {"age" : 100} in people) self.assertEquals(len(people), 3) def test_searcher_with_not_equals_select_query(self): people = self.searcher.fromClass("Person").select('age').where("name != Mike") self.assertTrue({"age" : 22} in people and {"age" : 74} in people and {"age" : 15} in people and {"age" : 100} in people) self.assertEquals(len(people), 4) def test_searcher_with_not_equals_false_value_query(self): animals = self.searcher.fromClass("Animal").where("mammal != False") hashes = [animal.__hash__() for animal in animals] self.assertTrue(self.human.__hash__() in hashes and self.cat.__hash__() in hashes and self.dog.__hash__() in hashes) self.assertEquals(len(animals), 3) def test_searcher_with_multiple_queries_same_class(self): temp = self.searcher.fromClass("Person").where("age BETWEEN 15 AND 74") people = self.searcher.fromClass("Person").where("age > 74") hashes = [person.__hash__() for person in people] self.assertTrue(self.mikeObj2.__hash__() in hashes and self.tim.__hash__() in hashes) self.assertEquals(len(people), 2) def test_searcher_with_mulitple_queries_same_class_select(self): t = self.searcher.fromClass("Person").select("name").where("age BETWEEN 70 AND 100") people = self.searcher.fromClass("Person").select("name, age").where("name = Alex OR name = Tim") self.assertTrue({"name" : "Alex", "age" : 15} in people and {"name" : "Tim", "age" : 100} in people) self.assertEquals(len(people), 2) def test_searcher_with_multiple_queries_different_class(self): temp = self.searcher.fromClass("Person").where("age BETWEEN 15 AND 74") animals = self.searcher.fromClass("Animal").where("mammal != False") hashes = [animal.__hash__() for animal in animals] self.assertTrue(self.human.__hash__() in hashes and self.cat.__hash__() in hashes and self.dog.__hash__() in hashes) self.assertEquals(len(animals), 3) def test_searcher_and_query_lessthan_name(self): people = self.searcher.fromClass("Person").where("name < Jouhan") self.assertTrue(len(people) == 1) def test_searcher_and_query_lessthanequal_name(self): people = self.searcher.fromClass("Person").where("name <= Jouhan") self.assertTrue(len(people) == 3) def test_searcher_and_query_between_swithed_names(self): people = self.searcher.fromClass("Person").where("name BETWEEN Tim AND Jouhan") self.assertTrue(len(people) == 6) def test_searcher_and_query_count(self): people = self.searcher.fromClass("Person").where("age = 15 AND name = Mike") self.assertTrue(len(people) == 2) def test_searcher_between_inverted_values(self): people = self.searcher.fromClass("Person").where("age BETWEEN 74 AND 15") self.assertTrue(len(people), 4)