Exemplo n.º 1
0
 def testJoin(self):
   storage_one = Midgard.QueryStorage(dbclass = "midgard_person")
   storage_two = Midgard.QueryStorage(dbclass = "midgard_person")
   qs = Midgard.QuerySelect(connection = self.mgd, storage = storage_one)
   group = Midgard.QueryConstraintGroup(grouptype = "AND")
   constraint_one = Midgard.QueryConstraint(
     property = Midgard.QueryProperty(property = "firstname"),
     operator = "=",
     holder = Midgard.QueryValue.create_with_value("Alice")
   )
   constraint_two = Midgard.QueryConstraint(
     property = Midgard.QueryProperty(property = "firstname", storage = storage_two),
     operator = "=",
     holder = Midgard.QueryValue.create_with_value("John")
   )
   group.add_constraint(constraint_one)
   group.add_constraint(constraint_two)
   qs.set_constraint(group)
   qs.add_join(
     "LEFT",
     Midgard.QueryProperty(property = "lastname"),
     Midgard.QueryProperty(property = "lastname", storage = storage_two)
   )
   qs.execute()
   # We expect Alice person only
   self.assertEqual(qs.get_results_count(), 1)
   objects = qs.list_objects()
   self.assertEqual(objects[0].get_property("firstname"), "Alice")
Exemplo n.º 2
0
 def getQSForSmiths(self):
   st = Midgard.QueryStorage(dbclass = "midgard_person")
   qs = Midgard.QuerySelect(connection = self.mgd, storage = st)
   qs.set_constraint(
     Midgard.QueryConstraint(
       property = Midgard.QueryProperty(property = "lastname"),
       operator = "=",
       holder = Midgard.QueryValue.create_with_value("Smith")
     )
   )
   return qs
Exemplo n.º 3
0
 def findByName(mgd, name):
     storage = Midgard.QueryStorage(dbclass="gir_test_book_store")
     qs = Midgard.QuerySelect(connection=mgd, storage=storage)
     qs.toggle_read_only(False)
     qs.set_constraint(
         Midgard.QueryConstraint(
             property=Midgard.QueryProperty(property="name"),
             operator="=",
             holder=Midgard.QueryValue.create_with_value(name)))
     qs.execute()
     return qs.list_objects()
 def getBook(self):
     st = Midgard.QueryStorage(dbclass="gir_test_book_crud")
     qs = Midgard.QuerySelect(connection=self.mgd, storage=st)
     qs.set_constraint(
         Midgard.QueryConstraint(
             property=Midgard.QueryProperty(property="title"),
             operator="=",
             holder=Midgard.QueryValue.create_with_value(
                 "The Hoy Grail and Attachments")))
     qs.execute()
     res = qs.list_objects()
     return res[0]
Exemplo n.º 5
0
 def testSelectNothing(self):
   st = Midgard.QueryStorage(dbclass = "midgard_person")
   qs = Midgard.QuerySelect(connection = self.mgd, storage = st)
   qs.set_constraint(
     Midgard.QueryConstraint(
       property = Midgard.QueryProperty(property = "firstname"),
       operator = "=",
       holder = Midgard.QueryValue.create_with_value("Sir Lancelot")
     )
   )
   qs.execute()
   # Do not expect persons
   self.assertEqual(qs.get_results_count(), 0);
 def tearDownClass():
   mgd = TestConnection.openConnection()
   tr = Midgard.Transaction(connection = mgd)
   tr.begin()
   st = Midgard.QueryStorage(dbclass = "midgard_person")
   qs = Midgard.QuerySelect(connection = mgd, storage = st)
   qs.set_constraint(
     Midgard.QueryConstraint(
       property = Midgard.QueryProperty(property = "lastname"),
       operator = "=",
       holder = Midgard.QueryValue.create_with_value("Smith")
     )
   )
   qs.execute()
   for person in qs.list_objects(): 
     person.purge(False)
   tr.commit()
 def testExecuteInvalidConstraint(self):
     self.addColumns()
     # SqlQueryConstraint expected
     self.select.set_constraint(
         Midgard.QueryConstraint(
             property=Midgard.QueryProperty(property="name"),
             operator="=",
             holder=Midgard.QueryValue.create_with_value(
                 "InvalidConstraint")))
     try:
         self.select.execute()
     except GObject.GError as e:
         self.assertEqual(e.domain, "midgard-validation-error-quark")
         self.assertEqual(e.code, Midgard.ValidationError.TYPE_INVALID)
         self.assertEqual(
             e.message,
             "Invalid constraint type 'MidgardQueryConstraint'. Expected SqlQueryConstraint or SqlQueryConstraintGroup"
         )
Exemplo n.º 8
0
 def testConstraint(self):
   person_storage = Midgard.QueryStorage(dbclass = "midgard_person")
   prop = Midgard.QueryProperty(property = "firstname", storage = person_storage) 
   constraint = Midgard.QueryConstraint(
       storage = person_storage, 
       operator = "=", 
       property = prop
   )
   # test if we get the same 
   self.assertEqual(constraint.get_storage(), person_storage)
   self.assertEqual(constraint.get_operator(), "=")
   self.assertEqual(constraint.get_property(), prop)
   # then set new ones and test again
   new_storage = Midgard.QueryStorage(dbclass = "midgard_person")
   new_prop = Midgard.QueryProperty(property = "firstname", storage = person_storage) 
   new_operator = "<>"
   constraint.set_storage(new_storage)
   constraint.set_operator(new_operator)
   constraint.set_property(new_prop)
   self.assertEqual(constraint.get_storage(), new_storage)
   self.assertEqual(constraint.get_operator(), new_operator)
   self.assertEqual(constraint.get_property(), new_prop)
Exemplo n.º 9
0
  def testVeryLongStringConstraintWithIndexedColumn(self):
    # https://github.com/midgardproject/midgard-core/issues/198
    long_firstname = "*****@*****.**"
    person = Midgard.Object.factory(self.mgd, "midgard_person", None)
    person.set_property("firstname", long_firstname)
    self.assertTrue(person.create())

    st = Midgard.QueryStorage(dbclass = "midgard_person")
    qs = Midgard.QuerySelect(connection = self.mgd, storage = st)
    qs.set_constraint(
        Midgard.QueryConstraint(
          property = Midgard.QueryProperty(property = "firstname"),
          operator = "=",
          holder = Midgard.QueryValue.create_with_value(long_firstname)
          )
        )
    qs.execute()
    objects = qs.list_objects()
    self.assertEqual(len(objects), 1)
    p = objects[0]
    self.assertEqual(p.get_property("firstname"), long_firstname)

    person.purge(False)