def searchRows(self, keyColumn, key):
     """
         Searches rows that have the same string value in 'keyColumn' as 'key'.
     Returns a CSVFileEngine object, representing search results.
         'keyColumn' - zero-based column index
         'key' - string-convertable object to search
     """
     keyColumn = self._validateColumn(keyColumn)
     orderFile = self._createOrderFileIfNeeded(keyColumn)
     orderFile.setPosition(self._searchFieldPositionInOrderFile(keyColumn, key))
     result = []
     while not orderFile.endOfFile():
         rowIndex = orderFile.readInteger()
         rowData = self.rowData(rowIndex)
         if rowData[keyColumn + 1] == key:
             result.append(rowData)
         else:
             break
     temporaryFile = AutoResizeableFile()
     string = ""
     for i in xrange(0, self._columnsCount):
         string += self._titles[i]
         if i != self._columnsCount - 1:
             string += ";"
     temporaryFile.writeLine(string)
     result.sort(lambda x, y: x[0] - y[0])
     for item in result:
         string = ""
         for i in xrange(1, len(item)):
             string += item[i]
             if i != len(item) - 1:
                 string += ";"
         temporaryFile.writeLine(string)
     return CSVFileEngine(temporaryFile)
class CSVFileEngine_text(unittest.TestCase):
    def setUp(self):
        self._file = AutoResizeableFile()
        self._file.writeLine("T1;T02;T003")
        self._engine = CSVFileEngine(self._file)


    def tearDown(self):
        del self._file
        del self._engine


    def testEngineOperations(self):
        self.assertEqual(self._engine.columnCount(), 3, ".columnCount() return error")
        self.assertEqual(self._engine.rowCount(), 0, ".rowCount() return error")
        self.assertEqual(tuple(self._engine.titles()), ("T1", "T02", "T003"), ".titles() return error")
        self._engine.insertRow(0, ("a", "aaa", "aa"))
        self._engine.insertRow(1, ("bb", "aaa", "bbb"))
        self._engine.insertRow(0, ("ccc", "c", "bbb"))
        self._engine.insertRow(3, ("dddd", "eeeee", "aa"))
        self.assertEqual(self._engine.rowData(0), (0, "ccc", "c", "bbb"), ".rowData() return error")
        self.assertEqual(self._engine.rowData(1), (1, "a", "aaa", "aa"), ".rowData() return error")
        self.assertEqual(self._engine.rowData(2), (2, "bb", "aaa", "bbb"), ".rowData() return error")
        self.assertEqual(self._engine.rowData(3), (3, "dddd", "eeeee", "aa"), ".rowData() return error")
        self.assertEqual(self._engine.rowCount(), 4, ".rowCount() return error")
        self._engine.removeRowAction(1)
        self._engine.removeRowAction(2)
        self.assertEqual(self._engine.rowCount(), 2, ".rowCount() return error")
        self.assertEqual(self._engine.rowData(0), (0, "ccc", "c", "bbb"), ".rowData() return error")
        self.assertEqual(self._engine.rowData(1), (1, "bb", "aaa", "bbb"), ".rowData() return error")
        searchResult = self._engine.searchRows(2, "bbb")
        self.assertEqual(searchResult.rowCount(), 2, "Search result length error")
        self._engine.insertRow(0, ("a", "aaa", ""))
        self._engine.insertRow(1, ("bb", "aaa", "bbb"))
        self._engine.insertRow(2, ("ccc", "c", "bbb"))
        self._engine.insertRow(3, ("dddd", "eeeee", "aa"))
        self.assertEqual(self._engine.rowCount(), 6, ".rowCount() return error")
        self._engine.changeFieldData(0, 2, "CHANGED")
        self._engine.changeFieldData(2, 2, "0")
        self._engine.changeFieldData(5, 2, "GO")
        self._engine.changeFieldData(4, 0, "")
        self.assertEqual(self._engine.fieldData(0, 2), "CHANGED", ".fieldData() return error")
        self.assertEqual(self._engine.fieldData(2, 2), "0", ".fieldData() return error")
        self.assertEqual(self._engine.fieldData(5, 2), "GO", ".fieldData() return error")
        self.assertEqual(self._engine.fieldData(4, 0), "", ".fieldData() return error")
        searchResult = self._engine.searchRows(1, "aaa")
        self.assertEqual(searchResult.rowCount(), 3, "Search result length error")
        self._engine.insertRow(0, ("", "", ""))
        self._engine.changeFieldData(0, 0, "HI")
        self._engine.changeFieldData(0, 1, "HELLO")
        self._engine.changeFieldData(0, 2, "BYE")
        self.assertEqual(tuple(self._engine.rowData(0)), (0, "HI", "HELLO", "BYE"), ".rowData() return error")
class UsersList_test(unittest.TestCase):
    def setUp(self):
        self._file = AutoResizeableFile()
        self._file.writeLine("staand-081194:add, remove, modify")
        self._file.writeLine("chess_master-chess:add, modify")
        self._file.writeLine("stamax-314159:")
        self._file.writeLine("dsz-dsz:remove")
        self._file.flush()
        
        
    def tearDown(self):
        self._file.close()


    def testParsing(self):
        users = UsersList(self._file.path())
        staand = users.get("staand")
        stamax = users.get("stamax")
        chess_master = users.get("chess_master")
        dsz = users.get("dsz")
        none = users.get("any_other")
        self.assertNotEqual(staand, None, "'staand' user is 'None'")
        self.assertNotEqual(stamax, None, "'stamax' user is 'None'")
        self.assertNotEqual(chess_master, None, "'chess_master' user is 'None'")
        self.assertNotEqual(dsz, None, "'dsz' user is 'None'")
        self.assertEqual(none, None, "'none' user is NOT 'None'")
        self.assertTrue(staand.login() == "staand" and staand.password() == "081194", "'staand' attributes error")
        self.assertTrue(chess_master.login() == "chess_master" and chess_master.password() == "chess", "'chess_master' attributes error")
        self.assertTrue(stamax.login() == "stamax" and stamax.password() == "314159", "'stamax' attributes error")
        self.assertTrue(dsz.login() == "dsz" and dsz.password() == "dsz", "'dsz' attributes error")
        rights = staand.rights()
        self.assertTrue(rights.fieldsModifyingPermited() and rights.rowsAddingPermited() and rights.rowsRemovingPermited(), 
                        "'staand' rights error")
        rights = stamax.rights()
        self.assertFalse(rights.fieldsModifyingPermited() or rights.rowsAddingPermited() or rights.rowsRemovingPermited(), 
                        "'stamax' rights error")
        rights = chess_master.rights()
        self.assertTrue(rights.fieldsModifyingPermited() and rights.rowsAddingPermited() and not rights.rowsRemovingPermited(), 
                        "'chess_master' rights error")
        rights = dsz.rights()
        self.assertTrue(not rights.fieldsModifyingPermited() and not rights.rowsAddingPermited() and rights.rowsRemovingPermited(), 
                        "'dsz' rights error")