def testDeleteOldRows(self):
        mocked_uploader = self._MockUploader()

        # Pretend spreadsheet has 2 rows, one in table and one not.
        ss_row1 = gdata_lib.SpreadsheetRow('OrigRow1', 2, self.SS_ROW1)
        ss_row2 = gdata_lib.SpreadsheetRow('OrigRow2', 3, self.SS_ROW2)
        ss_rows = (ss_row1, ss_row2)

        mocked_uploader._scomm.GetRows().AndReturn(ss_rows)
        # We expect ROW2 in spreadsheet to be deleted.
        mocked_uploader._scomm.DeleteRow('OrigRow2')
        self.mox.ReplayAll()

        # Verify
        with self.OutputCapturer():
            ups.Uploader._DeleteOldRows(mocked_uploader)
        self.mox.VerifyAll()
Exemple #2
0
    def testEmpty(self):
        row = gdata_lib.SpreadsheetRow(self.SS_ROW_OBJ, self.SS_ROW_NUM)

        self.assertEquals(0, len(row))
        self.assertEquals(self.SS_ROW_OBJ, row.ss_row_obj)
        self.assertEquals(self.SS_ROW_NUM, row.ss_row_num)

        self.assertRaises(TypeError, row, '__setitem__', 'abc', 'xyz')
        self.assertEquals(0, len(row))
        self.assertFalse('abc' in row)
Exemple #3
0
    def testInit(self):
        starting_vals = {'abc': 'xyz', 'foo': 'bar'}
        row = gdata_lib.SpreadsheetRow(self.SS_ROW_OBJ, self.SS_ROW_NUM,
                                       starting_vals)

        self.assertEquals(len(starting_vals), len(row))
        self.assertEquals(starting_vals, row)
        self.assertEquals(row['abc'], 'xyz')
        self.assertTrue('abc' in row)
        self.assertEquals(row['foo'], 'bar')
        self.assertTrue('foo' in row)

        self.assertEquals(self.SS_ROW_OBJ, row.ss_row_obj)
        self.assertEquals(self.SS_ROW_NUM, row.ss_row_num)

        self.assertRaises(TypeError, row, '__delitem__', 'abc')
        self.assertEquals(len(starting_vals), len(row))
        self.assertTrue('abc' in row)
  def testGetRows(self):
    mocked_scomm = self.MockScomm()
    rows = []
    for row_ix, row_dict in enumerate(self.ROWS):
      rows.append(gdata_lib.SpreadsheetRow('SSRowObj%d' % (row_ix + 2),
                                           (row_ix + 2), row_dict))
    mocked_scomm.rows = tuple(rows)

    # This is the replay script for the test.
    self.mox.ReplayAll()

    # This is the test verification.
    result = gdata_lib.SpreadsheetComm.GetRows(mocked_scomm)
    self.mox.VerifyAll()
    self.assertEquals(self.ROWS, result)
    for row_ix in xrange(len(self.ROWS)):
      self.assertEquals(row_ix + 2, result[row_ix].ss_row_num)
      self.assertEquals('SSRowObj%d' % (row_ix + 2), result[row_ix].ss_row_obj)
    def _CreateRowCache(self, table):
        """Recreate the expected row cache (by pkg) from |table|."""
        if not table:
            return None

        row_cache = {}
        for rowIx, row in enumerate(table):
            pkg = row[self.COL_PKG]

            # Translate column names now.
            ss_row_dict = {}
            for col in row:
                ss_row_dict[gdata_lib.PrepColNameForSS(col)] = row[col]

            ss_row = gdata_lib.SpreadsheetRow('OrigRow%d' % (rowIx + 2),
                                              rowIx + 2, ss_row_dict)
            entry = row_cache.get(pkg)
            if not entry:
                row_cache[pkg] = ss_row
            elif type(entry) == list:
                row_cache[pkg] = entry + [ss_row]
            else:
                row_cache[pkg] = [entry, ss_row]
        return row_cache
    def testUploadChangedRows(self):
        table = self._CreateTableWithRows(self.COLS,
                                          [self.ROW0, self.ROW1, self.ROW2])
        mocked_uploader = self._MockUploader(table=table)

        def RowVerifier(row_delta, golden_col_set, golden_row):
            if golden_col_set != set(row_delta.keys()):
                return False

            for col in row_delta:
                val = row_delta[col]
                if val != golden_row[col]:
                    return False

            return True

        # First Row.
        # Pretend first row does not exist already in online spreadsheet
        # by returning (None, None) from _GetSSRowForPackage.
        #
        row0_pkg = self.ROW0[self.COL_PKG]
        mocked_uploader._GetSSRowForPackage(row0_pkg).AndReturn(None)
        mocked_uploader._scomm.InsertRow(mox.IgnoreArg())

        # Second Row.
        # Pretend second row does already exist in online spreadsheet, and
        # pretend that it has a different value that needs to be changed
        # by an upload.
        row1_pkg = self.ROW1[self.COL_PKG]
        row1_reverse_delta = {self.SS_COL_VER: '1.2.3'}
        ss_row1 = dict(self.SS_ROW1)
        for col in row1_reverse_delta:
            ss_row1[col] = row1_reverse_delta[col]
        ss_row1 = gdata_lib.SpreadsheetRow('OrigRow1', 3, ss_row1)
        mocked_uploader._GetSSRowForPackage(row1_pkg).AndReturn(ss_row1)
        # Prepare verfication for row.
        g_col_set1 = set(row1_reverse_delta.keys())
        g_row1 = gdata_lib.PrepRowForSS(self.SS_ROW1)
        row1_verifier = lambda rdelta: RowVerifier(rdelta, g_col_set1, g_row1)
        mocked_uploader._scomm.UpdateRowCellByCell(3, mox.Func(row1_verifier))

        # Third Row.
        # Pretend third row does already exist in online spreadsheet, and
        # pretend that several values need to be changed by an upload.
        row2_pkg = self.ROW2[self.COL_PKG]
        row2_reverse_delta = {
            self.SS_COL_STATUS: 'needs upgrade',
            self.SS_COL_VER: '0.5',
            self.SS_COL_TARGET: 'chromeos-foo',
        }
        ss_row2 = dict(self.SS_ROW2)
        for col in row2_reverse_delta:
            ss_row2[col] = row2_reverse_delta[col]
        ss_row2 = gdata_lib.SpreadsheetRow('OrigRow2', 4, ss_row2)
        mocked_uploader._GetSSRowForPackage(row2_pkg).AndReturn(ss_row2)
        # Prepare verification for row.
        g_col_set2 = set(row2_reverse_delta.keys())
        g_row2 = gdata_lib.PrepRowForSS(self.SS_ROW2)
        row2_verifier = lambda rdelta: RowVerifier(rdelta, g_col_set2, g_row2)
        mocked_uploader._scomm.UpdateRowCellByCell(4, mox.Func(row2_verifier))

        self.mox.ReplayAll()

        # Verify
        with self.OutputCapturer():
            ups.Uploader._UploadChangedRows(mocked_uploader)
        self.mox.VerifyAll()