Example #1
0
  def move_file_and_test(self, src_f, targ_f):
    """Helper function that performs a dupe file move and tests it."""
    # Initial state
    self.cr_load("dupes-initial")
    sm = SettingsManager(self.loc_in_cr('kpawebgen.json'))
    db_path = os.path.join(sm.abspath('kpa_dir'), shadow)
    index_p = self.loc_in_cr("KPA/index.xml")
    kpa_p = self.loc_in_cr("KPA")
    self.cr_nexist(db_path)

    # Create shadow DB
    u = Updater(sm.abspath('kpa_dir'), shadow)
    did = u.update()
    self.assertTrue(did)

    conn = sqlite3.connect(db_path)
    def getall(q, args=()):
      return conn.execute(q, args).fetchall()
    def count_images():
      return getall("""SELECT COUNT(*) FROM image_id""")[0][0]

    (srcID, srcMD5) = getall(
      """SELECT imageID, md5 FROM image_id WHERE path = ?""",
      (src_f,))[0]
    oldCount = count_images()

    # Move a file (in filesystem and XML)
    with open(index_p, 'r') as f:
      xml = f.read()
    xml = xml.replace(src_f, targ_f)
    with open(index_p, 'w') as f:
      f.write(xml)
    os.rename(os.path.join(kpa_p, src_f),
              os.path.join(kpa_p, targ_f))

    # Update shadow DB
    u = Updater(sm.abspath('kpa_dir'), shadow)
    did = u.update()
    self.assertTrue(did)

    # Verify move in shadow DB
    self.assertEqual(getall("""
      SELECT action, oldPath, newPath
      FROM changelog
      WHERE changelog.logID = (SELECT MAX(logID) FROM changelog);"""),
      [('move', src_f, targ_f)])
    (targID, targMD5) = getall(
        """SELECT imageID, md5 FROM image_id WHERE path = ?""",
        (targ_f,))[0]
    self.assertEqual(targID, srcID)
    self.assertEqual(targMD5, srcMD5)
    self.assertEqual(count_images(), oldCount)

    conn.close()
Example #2
0
  def test_firstrun(self):
    """Must create a DB3 if none exists."""
    self.cr_load("firstrun")
    sm = SettingsManager(self.loc_in_cr('kpawebgen.json'))
    db_path = os.path.join(sm.abspath('kpa_dir'), shadow)
    u = Updater(sm.abspath('kpa_dir'), shadow)
    self.cr_nexist(db_path)
    did = u.update()
    self.assertTrue(did)

    self.cr_exist(db_path)
    conn = sqlite3.connect(db_path)

    def getall(q):
      return conn.execute(q).fetchall()
    
    self.assertNotEqual(getall('SELECT xmlChecksum FROM metadata')[0][0], None)
    self.assertEqual(getall('SELECT nextImageID FROM metadata'), [(7,)])
    self.assertEqual(getall('SELECT count(*) FROM image_id'), [(7,)])
    self.assertEqual(getall('SELECT min(imageID) FROM image_id'), [(0,)])
    self.assertEqual(getall('SELECT max(imageID) FROM image_id'), [(6,)])
    self.assertEqual(getall('SELECT count(*) FROM image_int'), [(7,)])
    self.assertEqual(getall('SELECT count(*) FROM image_ext'), [(7,)])
    self.assertEqual(getall('SELECT count(*) FROM changelog'), [(7,)])
    self.assertEqual(getall('SELECT DISTINCT oldPath, oldMD5 FROM changelog'),
                      [(None,None)])
    hornetID = getall("""SELECT imageID FROM image_id
                         WHERE path='IMG_9610.JPG'""")[0][0]
    hornetInt = conn.execute("SELECT * FROM image_int WHERE imageID=?",
                             (hornetID,)).fetchall()[0]
    conn.close()

    # Create the baseline for several changes
    self.dump_db(db_path, 'baseline.dump')
    db_backup = self.loc_in_cr('shadow.db3.firstrun')
    subprocess.call(['cp', db_path, db_backup])

    # No changes yet
    u = Updater(sm.abspath('kpa_dir'), shadow)
    did = u.update()
    self.assertFalse(did) # Don't update if nothing has changed

    # Alter index: Delete and move
    self.patch("KPA/index.xml", "patches/delete+move.index.xml.patch")
    u = Updater(sm.abspath('kpa_dir'), shadow)
    did = u.update()
    self.assertTrue(did)
    self.dump_db(db_path, 'delete+move.dump')
    self.validate_diff("baseline.dump", "patches/delete+move.dump.patch",
                       "delete+move.dump")
    self.patch("KPA/index.xml", "patches/delete+move.index.xml.patch",
               undo=True)
    subprocess.call(['rm', db_path])
    subprocess.call(['cp', db_backup, db_path])

    # Alter index: Create and edit
    self.patch("KPA/index.xml", "patches/create+edit.index.xml.patch")
    u = Updater(sm.abspath('kpa_dir'), shadow)
    did = u.update()
    self.assertTrue(did)
    self.dump_db(db_path, 'create+edit.dump')
    self.validate_diff("baseline.dump", "patches/create+edit.dump.patch",
                       "create+edit.dump")
    self.patch("KPA/index.xml", "patches/create+edit.index.xml.patch",
               undo=True)
    subprocess.call(['rm', db_path])
    subprocess.call(['cp', db_backup, db_path])