def read_list(self, username=False, language=False): """Reads the CSV file and returns found words. Args: username: name of the user, not mandatory. language: language of the words the method returns, not mandatory. Returns: words: list of Word objects""" words = [] with open(self.address) as file: for row in file: row = row.replace('\n', '') parts = row.split(',') user = parts[0] word = parts[1] transl = parts[2] lang = parts[3] if (not username) and (not language): words.append(Word(user, word, transl, lang)) elif username and (not language): if user == username: words.append(Word(user, word, transl, lang)) elif username and language: if user == username and lang == language: words.append(Word(user, word, transl, lang)) return words
def test_reading_list_by_user(self): self.wordlist.add_word(self.new_word) self.wordlist.add_word( Word('TestUser', 'TestWord2', 'TestTranslation2', 'TestLanguage')) self.wordlist.add_word( Word('TestUser', 'TestWord3', 'TestTranslation3', 'TestLanguage2')) self.wordlist.add_word(Word('User2', 'Word2', 'Transl2', 'Lang2')) self.assertEqual(len(self.wordlist.read_list('TestUser')), 3)
def test_language_list_no_duplicates(self): self.wordlist.add_word(self.new_word) self.wordlist.add_word( Word('TestUser', 'TestWord2', 'TestTranslation2', 'TestLanguage')) self.wordlist.add_word( Word('TestUser', 'TestWord3', 'TestTranslation3', 'TestLanguage2')) languages = self.wordlist.get_languages('TestUser') self.assertEqual(len(languages), 2) self.assertEqual(languages, ['TestLanguage2', 'TestLanguage'])
def test_reading_list_by_language(self): self.wordlist.add_word(self.new_word) self.wordlist.add_word( Word('TestUser', 'TestWord2', 'TestTranslation2', 'TestLanguage')) self.wordlist.add_word( Word('TestUser', 'TestWord3', 'TestTranslation3', 'TestLanguage2')) result = self.wordlist.read_list('TestUser', 'TestLanguage') self.assertEqual(len(result), 2) self.assertEqual(result[0].language, 'TestLanguage') self.assertEqual(result[1].language, 'TestLanguage')
def test_deleting_words_by_user(self): self.wordlist.add_word(self.new_word) self.wordlist.add_word(Word('1', '2,', '3,', '4')) self.wordlist.delete_words_user('TestUser') words = self.wordlist.read_list() self.assertEqual(len(words), 2) self.assertEqual(words[1].user, '1')
def add_word(self, user, word, translation, language): """Creates a Word object and adds it to the CSV file. Args: user: name of the user adding the word. word: word string. translation: translation for the word. language: language of the translation.""" word = Word(user, word, translation, language) self.wordlist_repository.add_word(word)
def setUp(self): self.new_word = Word('TestUser', 'TestWord', 'TestTranslation', 'TestLanguage') self.address = 'test_words.csv'