예제 #1
0
  def test_obtain_release_lock(self):
    if not CONFIG.is_pg:
      return
    da0 = get_data_access().open()
    da1 = get_data_access().open()

    key = 1

    # Get lock with first connection
    self.assertTrue(obtain_lock(da0, key, LockMode.skip))

    # Try to get lock with second connection, fails
    self.assertFalse(obtain_lock(da1, key, LockMode.skip))

    # Try to release lock with second connection, fails
    self.assertFalse(release_lock(da1, key))

    # Release lock wth first connection
    self.assertTrue(release_lock(da0, key))

    # Get lock with second connection
    self.assertTrue(obtain_lock(da1, key, LockMode.skip))

    # Release lock with second connection
    self.assertTrue(release_lock(da1, key))
예제 #2
0
  def test_advisory_lock(self):
    if not CONFIG.is_pg:
      return

    da0 = get_data_access().open()
    da1 = get_data_access().open()
    key = 1

    da0_got = False
    da1_got = False

    with advisory_lock(da0, key, LockMode.skip) as got_lock0:
      self.assertTrue(got_lock0)
      with advisory_lock(da1, key, LockMode.skip) as got_lock1:
        self.assertFalse(got_lock1)

    # The lock should already be released, so trying to release again should
    # return false
    self.assertFalse(release_lock(da0, key))

    with advisory_lock(da0, key, LockMode.skip) as got_lock2:
      self.assertTrue(got_lock2)
      with self.assertRaises(Exception):
        with advisory_lock(da1, key, LockMode.error):
          pass
예제 #3
0
    def test_autocommit(self):
        da1 = get_data_access().open()
        da2 = get_data_access().open()

        da1.autocommit = True
        cnst1 = dict(first_name=rand(), last_name=rand(), age=1)
        da1.insert("people", cnst1)
        self.assertEqual(da2.find("people", cnst1).first_name, cnst1["first_name"])

        da1.autocommit = False
        cnst2 = dict(first_name=rand(), last_name=rand(), age=2)
        da1.insert("people", cnst2)
        self.assertIsNotNone(da1.find("people", cnst2))
        self.assertIsNone(da2.find("people", cnst2))

        da1.rollback()
        da1.delete("people", cnst1)
        da1.commit()
        self.assertEqual(da1.count("people"), 11)
예제 #4
0
 def test_configure_connection(self):
     if not CONFIG.is_pg:
         return
     da = get_data_access().open(autocommit=True)
     get_setting = lambda name: da.find("pg_settings", dict(name=name), columns="setting")
     new_setting = ", ".join(["test_schema_1", "test_schema_2", "public"])
     setting0 = get_setting("search_path").setting
     self.assertNotEqual(setting0, new_setting)
     da._configure_connection("search_path", new_setting)
     setting1 = get_setting("search_path").setting
     self.assertEqual(setting1, new_setting)
예제 #5
0
 def test_autocommit_contextmanager(self):
     da = get_data_access().open(autocommit=False)
     cnst = dict(first_name=rand(), last_name=rand(), age=3)
     with transaction(da):
         with autocommit(da):
             da.insert("people", cnst)
             # Rollback should have no effect
             da.rollback()
     self.assertEqual(da.find("people", cnst).first_name, cnst["first_name"])
     da.delete("people", cnst)
     da.commit()
예제 #6
0
 def test_transaction_contextmanager(self):
     da1 = get_data_access().open()
     da2 = get_data_access().open()
     da1.autocommit = False
     cnst = dict(first_name=rand(), last_name=rand(), age=3)
     with transaction(da1) as da:
         self.assertIsInstance(da, DataAccess)
     with transaction(da1):
         da1.insert("people", cnst)
     self.assertEqual(da2.find("people", cnst).first_name, cnst["first_name"])
     with transaction(da1):
         da1.delete("people", cnst)
     da1.autocommit = True
     self.assertEqual(da1.count("people"), 11)
     try:
         with transaction(da1):
             da1.insert("people", cnst)
             raise Exception()
     except Exception as ex:
         pass
     finally:
         self.assertIsNone(da1.find("people", cnst))
예제 #7
0
 def setUpClass(cls):
     cls.dab = get_data_access()
     cls.dab.open()