Example #1
0
 def check_if_tasks_deleted(self, output):
     tasks_after_delete = len(self.execute('SELECT * FROM task'))
     if not tasks_after_delete < ToDoList.tasks_before_delete:
         return CheckResult.wrong(
             'There is should be less rows in the table after some task has been deleted!'
         )
     self.is_completed = True
     return '0'
    def check_inventory(self, output):
        inventory = [self.snack, self.weapon, self.tool]
        in_inventory = all([item in output.lower() for item in inventory])

        if "inventory" not in output.lower() or not in_inventory:
            return CheckResult.wrong("Your program didn't output correct inventory content.")
        else:
            return CheckResult.correct()
 def func7(self, output):
     if self.current_status == 'computer':
         if "invalid input. please try again." not in output.lower():
             return CheckResult.wrong("The player should be informed about tne incorrect move")
         return self.chosen_piece
     else:
         self.set_the_currents(output)
         return self.check_the_move(output)
Example #4
0
 def check_if_tasks_deleted(self, output):
     tasks_after_delete = len(self.execute('SELECT * FROM task'))
     if tasks_after_delete >= ToDoList.tasks_before_delete:
         return CheckResult.wrong(
             'Once a task has been deleted, there should be less rows in the table.'
         )
     self.is_completed = True
     return '0'
Example #5
0
    def test_output_existing_card(self, reply):
        if "already exists" not in reply.lower():
            return CheckResult.wrong(
                "Your program did not output an error message "
                "when the user tried to input a duplicate card. "
                "Instead, your program printed this line:\n{}".format(reply))

        return "dog\nmeow"
Example #6
0
 def check_not_200(self, reply, attach):
     try:
         status_code = requests.get(attach).status_code
     except requests.exceptions.SSLError:
         return CheckResult.wrong(
             "An error occurred when tests tried to connect to the Internet page.\n"
             "Please, try again.")
     if f"The URL returned" in reply and "Content saved" not in reply:
         if str(status_code) in reply:
             return CheckResult.correct()
         else:
             return CheckResult.wrong(
                 "The returned error doesn't match with the output message."
             )
     else:
         return CheckResult.wrong(
             "The link returned an error, but your program didn't.")
    def check(self, reply, attach):
        self.stop_server()

        if not self.connected:
            return CheckResult.wrong("You didn't connect to the server")

        if len(reply) == 0:
            return CheckResult.wrong('You did not print anything')
        if reply.split('\n')[0] != 'Wrong password!':
            return CheckResult.wrong(
                'The line you printed is not the one sent by server')
        if len(self.message) == 0:
            return CheckResult.wrong('You sent nothing to the server')
        if self.message != attach:
            return CheckResult.wrong(
                'You sent the wrong information to the server')
        return CheckResult.correct()
Example #8
0
 def check(self, reply: str, attach) -> CheckResult:
     ccoins_att, currs_att = attach
     reply_parsed = reply.strip().split('\n')
     if len(reply_parsed) != 5:
         return CheckResult.wrong("Your output differs from the example")
     for repl in reply_parsed:
         repl = repl.lower()
         repl_parsed = repl.strip().split()
         if len(repl_parsed) != 11:
             return CheckResult.wrong(
                 "Your output differs from the example")
         try:
             cur = float(repl_parsed[3])
             ccoins = float(repl_parsed[-2])
             key = repl_parsed[4]
         except (ValueError, KeyError):
             return CheckResult.wrong(
                 "Format your output according to the example")
         if ccoins != ccoins_att:
             return CheckResult.wrong("The amount of conicoins is wrong")
         try:
             amount_curr = currs_att[key]
         except KeyError:
             return CheckResult.wrong(
                 "The currency name in the output of your program seems to be wrong:\n"
                 "\"{0}\"".format(key.upper()))
         if abs(amount_curr - cur) > 0.2:
             return CheckResult.wrong(
                 f"The amount of {key.upper()} is wrong")
         if not ('i will get' in repl and 'from the sale of' in repl
                 and 'conicoins' in repl):
             return CheckResult.wrong(
                 "Format your output according to the example")
     return CheckResult.correct()
Example #9
0
    def check_sys_import_export(reply, attach):
        reply = reply.lower()
        if "cards have been loaded" not in reply:
            return CheckResult.wrong("The user has provided the --import_from command-line argument. \n"
                                     "So, in the beginning of the game, \n"
                                     "your program should load the cards from the file specified in the command-line argument\n."
                                     "After that, a message about the number of cards that have been loaded should be printed by your program.\n"
                                     "However, this message was not found.")
        if "3 cards have been loaded" not in reply:
            return CheckResult.wrong("Seems like your program incorrectly printed the number of cards "
                                     "imported from the file in the beginning of the game.")

        if right_keyword not in reply and (wrong_keyword not in reply and "but your definition is correct for"):
            return CheckResult.wrong("Your program did not respond correctly to the user's answer on the question.\n"
                                     "Make sure you've imported cards from the file specified in the --import_from command-line argument.")
        if "cards have been saved" not in reply.lower():
            return CheckResult.wrong("The user has provided the --export_to command-line argument. \n"
                                     "So, after the user inputs the command \"exit\", \n"
                                     "your program should save the cards to the file specified in the command-line argument\n."
                                     "After that, a message about the number of cards that have been saved should be printed by your program.\n"
                                     "However, this message was not found.")
        if "3 cards have been saved" not in reply.lower():
            return CheckResult.wrong("Seems like your program incorrectly printed the number of cards "
                                     "exported to file after \"exit\" command.")
        if not os.path.exists(attach):
            return CheckResult.wrong("The user has provided the --export_to command-line argument. \n"
                                     "However, the file where the cards should have been exported after \"exit\" was not found. "
                                     "Make sure you named the file with exported cards "
                                     "as was required in --export_to command-line argument.")
        try:
            os.remove(attach)
        except PermissionError:
            return CheckResult.wrong("Impossible to remove the file with the exported cards. "
                                     "Perhaps you haven't closed this file?")
        return CheckResult.correct()
Example #10
0
 def test_log(self, reply):
     if not os.path.exists(log_filepath):
         return CheckResult.wrong(
             "The file with the log has not been found. "
             "Make sure that you correctly saved the log file.")
     if "log has been saved" not in reply.lower():
         return CheckResult.wrong(
             "When the log is saved, "
             "the program should output a message \"The log has been saved.\""
         )
     try:
         os.remove(log_filepath)
     except PermissionError:
         return CheckResult.wrong(
             "Impossible to remove the file with the log. "
             "Perhaps you haven't closed this file?")
     self.is_completed = True
     return "exit"
Example #11
0
 def check_200(self, reply, attach):
     try:
         test_content = requests.get(attach).content
     except requests.exceptions.SSLError:
         return CheckResult.wrong(
             "An error occurred when tests tried to connect to the Internet page.\n"
             "Please, try again.")
     try:
         with open("source.html", "rb") as f:
             file_content = f.read()
             if file_content == test_content:
                 return CheckResult.correct() if "Content saved" in reply and "The URL returned" not in reply \
                     else CheckResult.wrong("Did you notify the user you've saved the content?")
             else:
                 return CheckResult.wrong(
                     "The content of the file is not correct!")
     except FileNotFoundError:
         return CheckResult.wrong("Couldn't find the source.html file")
Example #12
0
 def test_ask_2(self, reply):
     if "dog" not in reply.lower() and "horse" not in reply.lower():
         return CheckResult.wrong(
             "Your program did not ask for the definition of any of the existing cards."
         )
     if "dog" in reply.lower():
         return "woof"
     else:
         return "neigh"
Example #13
0
 def generate(self) -> List[TestCase]:
     return [
         TestCase(stdin=[(-1, lambda x: CheckResult.correct())],
                  attach="1\n2\n2\n"),
         TestCase(stdin=[
             lambda x: CheckResult.correct()
             if x == "" else CheckResult.wrong("WA TEST 2"),
             (2, lambda x: CheckResult.correct()
              if x == "3\n" else CheckResult.wrong("WA TEST 2"))
         ],
                  attach="3\n3\n3\n"),
         TestCase(stdin=[
             lambda x: "3" if x == "" else CheckResult.wrong("WA TEST 3"),
             (-1, lambda x: "4"
              if x == "3\n" else CheckResult.wrong("WA TEST 3"))
         ],
                  attach="3\n3\n4\n"),
     ]
Example #14
0
 def check_finish(self, reply: str, attach: Any) -> CheckResult:
     for line in reply.lower().split("\n")[-2:-1]:
         if "tour" in line:
             break
     else:
         return CheckResult.wrong(
             "End of game message missing.\n"
             "Expected output: 'What a great tour! Congratulations!'")
     return CheckResult.correct()
Example #15
0
 def check_no_soln2(self, reply: str, attach: Any) -> CheckResult:
     for line in reply.lower().split("\n")[-2:-1]:
         if "exist" in line:
             break
     else:
         return CheckResult.wrong(
             "No solution should exist, you need to check if there are solutions for the board"
         )
     return CheckResult.correct()
Example #16
0
    def test_hardest_after_import(self, reply):
        try:
            os.remove(filepath_2)
        except PermissionError:
            return CheckResult.wrong("Impossible to remove the file with the exported cards. "
                                     "Perhaps you haven't closed this file?")
        except FileNotFoundError:
            return CheckResult.wrong("A file from which the cards should have been imported is not found. "
                                     "Make sure you did not delete the file after importing the cards.")

        reply = reply.lower()
        if "the hardest card is" not in reply or "errors" not in reply or "answering it" not in reply:
            return CheckResult.wrong("The line naming the hardest card and stating the number of mistakes "
                                     "made for this card was expected in the output of your program.\n"
                                     "However, it was not found. \n"
                                     "Make sure that your program saves the number of mistakes for each card while exporting them,\n"
                                     "and loads the cards and the number of mistakes for them correctly during the import.")
        return "reset stats"
    def check_weeks_tasks(self, output):
        for day in weekdays:
            if day not in output.lower():
                return CheckResult.wrong(
                    'There is no {} in the output.\nIn week\'s task you should output all the tasks for 7 days.'.format(
                        day.title()))

        today = datetime.today().date()
        return '5\nFirst task\n{}\n5\nSecond task\n{}\n1'.format(today, today)
    def check(self, reply, attach):
        """
        Every fake-page contains magic word. Bloomberg fake-page must contain
        'Bloomberg' and New York Times fake page must contain 'New York Times'

        These test cases check assertions above.
        """
        right_word, wrong_word = attach

        if wrong_word in reply:
            return CheckResult.wrong(
                'It seems like you printed wrong variable')

        if right_word in reply:
            return CheckResult.correct()

        return CheckResult.wrong(
            'You printed neither bloomberg_com nor nytimes_com')
Example #19
0
 def test_zero_mistakes(self, reply):
     if 'no cards with errors' not in reply:
         return CheckResult.wrong(
             "The user ask to output the hardest card. Since no questions were asked yet, "
             "the output \"There are no cards with errors.\" was expected. \n"
             "However, it was not found. \n"
             "Make sure your program correctly counts the number of mistakes that were made."
         )
     return "add"
    def check(self, reply, attach):
        numbers = re.findall(r'[-+]?(\d*\.\d+|\d+)', reply)
        if len(numbers) == 0:
            return CheckResult.wrong('No numbers in the answer', )

        if isinstance(attach, tuple):
            for i in numbers:
                if abs(attach[0] - float(i)) < 2:
                    return CheckResult.correct()
            output = 'Numbers in your answer: ' + ' '.join(numbers)
            output += 'But correct principal is {0}'.format(attach)
            return CheckResult.wrong(output)

        if isinstance(attach, list):
            # to exclude answers like 'it takes 2.01 years'
            # but 'it takes 2.0 years' let it be OK.
            epsilon = 0.00001
            numbers = [
                int(float(x)) for x in numbers
                if abs(int(float(x)) - float(x)) < epsilon
            ]
            if attach[1] == 0:
                if 'year' in reply.lower() and attach[0] in numbers:
                    return CheckResult.correct()

                output = 'Correct result: {0} years, but you output "{1}"'
                return CheckResult.wrong(output.format(attach[0], reply), )
            else:
                if attach[0] in numbers and 'year' in reply.lower():
                    if attach[1] in numbers and 'month' in reply.lower():
                        return CheckResult.correct()

                output = ('Correct result: {0} years {1} months, '
                          'but you output "{2}"')
                return CheckResult.wrong(
                    output.format(attach[0], attach[1], reply), )

        if str(attach) not in reply.lower():
            output = (
                'Correct annuity payment is {0} but you output numbers: {1}')
            figures = ' '.join(numbers)
            return CheckResult.wrong(output.format(attach, figures), )

        return CheckResult.correct()
Example #21
0
    def check(self, reply, attach=None):
        n_pages, article_type = attach
        scraper = NatureScraper()
        for i in range(1, n_pages + 1):
            dirname = f"Page_{i}"
            dirname = os.path.abspath(dirname)
            if not os.path.exists(dirname):
                return CheckResult.wrong(f"Impossible to find directory {dirname}")
            os.chdir(dirname)
            txt_files = glob.glob("*.txt")
            url = furl("https://www.nature.com/nature/articles").add({"page": str(i)})
            article_links = scraper.get_article_links_of_type(url, article_type=article_type)
            if len(txt_files) != len(article_links):
                return CheckResult.wrong("A wrong number of files with articles was found in the directory {0}. \n"
                                         "{1} files were found, {2} files were expected.".format(dirname,
                                                                                                 len(txt_files),
                                                                                                 len(article_links)))
            if article_links:
                random_val = random.randint(0, len(article_links)-1)
                title, content = scraper.get_article_title_and_content(article_links[random_val])
                content = content.strip()
                title = f"{title.translate(str.maketrans('', '', string.punctuation)).replace(' ', '_')}.txt"
                title = os.path.abspath(title)
                if not os.path.exists(title):
                    return CheckResult.wrong("A file with the title {0} was expected, but was not found.".format(title))
                with open(title, "rb") as f:
                    try:
                        file_content = f.read().decode('utf-8').strip()
                    except UnicodeDecodeError:
                        return CheckResult.wrong("An error occurred when tests tried to read the file \"{0}\"\n"
                                                 "Please, make sure you save your file in binary format \n"
                                                 "and encode the saved data using utf-8 encoding.".format(title))

                file_content = re.sub('[\r\n]', '', file_content)
                content = re.sub('[\r\n]', '', content)
                if file_content != content:
                    return CheckResult.wrong("Some of the files do not contain the expected article's body. \n"
                                             "The tests expected the following article:\n"
                                             f"\"{content}\"\n"
                                             f"However, the following text was found in the file {title}:\n"
                                             f"\"{file_content}\"")
            os.chdir("..")
            shutil.rmtree(dirname)
        return CheckResult.correct()
Example #22
0
    def check(self, reply, attach):
        lines = reply.split('\n')
        while ("" in lines):
            lines.remove("")
        headers = lines[::2]
        text = lines[1::2]
        news_text = []
        for row in text:
            row = row.split(' ')
            while ("" in row):
                row.remove("")
            news_text.append(row)
        news = {}
        if len(news_text) != len(headers):
            return CheckResult.wrong(feedback="The number of headers should be equal "
                                              "to the number of lines with keywords.\n"
                                              "Please check the output of your program.")
        for i in range(len(headers)):
            news[headers[i]] = news_text[i]
        wrong_news = 0
        wrong_head = 0
        ans = list(answer.items())
        new = list(news.items())
        for i in range(len(ans)):
            # лучше добавить ту же проверку, что и в первой стадии
            if len(answer) != len(news):
                return CheckResult.wrong(
                    feedback="Something is wrong with output. Probably, you have forgotten to print some news? Try again")
            if set(ans[i][1]) != set(new[i][1]):
                wrong_news += 1
            if ans[i][0] != new[i][0]:
                wrong_head += 1
        if len(answer) != len(news):
            return CheckResult.wrong(
                feedback="Something is wrong with output. Probably, you have forgotten to print some news? Try again")
        elif wrong_head != 0:
            return CheckResult.wrong(
                feedback='Incorrect. Probably, something is wrong with the headers? Try again!'.format(wrong_head,
                                                                                                       len(news)))
        elif wrong_news != 0:
            return CheckResult.wrong(
                feedback='Keywords found incorrectly in {} of {} news texts. Try again!'.format(wrong_news, len(news)))

        return CheckResult.correct()
Example #23
0
    def check_duplicate_first_line(self):
        main = TestedProgram()
        output = main.start(root_dir_path).lower()
        output = main.execute("").lower()
        output = main.execute("2").lower()
        output = main.execute("yes").lower().split('\n')
        output = [val for val in output if val]

        if not output:
            return CheckResult.wrong(
                "Looks like your output is empty after entering 'Yes'")

        if len(output) < 1 or 'byte' not in output[0]:
            return CheckResult.wrong(
                f"The first line of group of files should contain files size")
        if len(output) < 2 or 'hash' not in output[1]:
            return CheckResult.wrong(
                f"The second line of group of files should contain hash value")
        return CheckResult.correct()
Example #24
0
    def check_server(self):
        if self.port == '0':
            return CheckResult.wrong(
                f'Please free one of the ports: {", ".join(self.tryout_ports)}'
            )

        for _ in range(15):
            try:
                urlopen(
                    f'http://localhost:{self.port}/not-existing-link-by-default'
                )
                return CheckResult.correct()
            except URLError as err:
                if isinstance(err, HTTPError):
                    return CheckResult.correct()
                sleep(1)
        else:
            return CheckResult.wrong(
                "Django server hasn't started within 15 seconds time limit")
Example #25
0
    def check(self, reply, attach):
        punct = {".", "?", "!"}
        corpus = preprocess()
        trigrams = {" ".join(corpus[i:i + 3]) for i in range(len(corpus) - 2)}
        sentences = [
            sentence for sentence in reply.split('\n') if len(sentence)
        ]

        if len(sentences) != 10:
            return CheckResult.wrong(
                "You should output exactly 10 sentences! Every sentence should be in a new line."
            )

        for sentence in sentences:
            sent = sentence.split()
            if len(sent) < 5:
                return CheckResult.wrong(
                    "A pseudo-sentence should not be shorter than 5 tokens.")
            if len(set(sent)) == 1:
                return CheckResult.wrong(
                    "Invalid output. All words of a sentence are identical.")
            if not sent[0][0].isupper():
                return CheckResult.wrong(
                    "Every pseudo-sentence should start with a capitalized word."
                )
            if sent[0][-1] in punct:
                return CheckResult.wrong(
                    "The first token of a pseudo-sentence should not end with sentence-ending punctuation."
                )
            if sent[-1][-1] not in punct:
                return CheckResult.wrong(
                    "Every pseudo-sentence should end with a sentence-ending punctuation mark."
                )
            for i, token in enumerate(sent):
                if token not in corpus:
                    return CheckResult.wrong(
                        "Sentences should contain only words from the corpus!")
                if token[-1] in punct and 4 < i + 1 < len(sent):
                    return CheckResult.wrong(
                        "If a sentence is longer than 5 tokens, it should end at the first sentence ending punctuation."
                    )
            for i in range(len(sent) - 2):
                trigram = " ".join(sent[i:i + 3])
                if trigram not in trigrams:
                    return CheckResult.wrong(
                        "Pseudo-sentences should entirely consist of trigrams from the corpus."
                    )
        return CheckResult.correct()
Example #26
0
 def check_path(self):
     main = TestedProgram()
     output = main.start(root_dir_path).lower()
     output = main.execute("").lower()
     output = main.execute("2").lower().split('\n')
     for val in output:
         if '.' in val:
             if not os.path.exists(val):
                 return CheckResult.wrong(f"{val} path does not exist")
     return CheckResult.correct()
Example #27
0
 def test_ask(self, reply):
     lines_split = reply.strip().split('\n')
     if "many times to ask" not in reply.lower():
         return CheckResult.wrong(
             "Before asking the definitions of cards, your program should"
             "ask: \"How many times to ask?\". These words were not found"
             "in the output of your program:\n"
             "{0}".format(
                 lines_split[-1] if len(lines_split) > 0 else reply))
     return "2"
Example #28
0
    def check_duplicate_order_desc(self):
        main = TestedProgram()
        output = main.start(root_dir_path).lower()
        output = main.execute("").lower()
        output = main.execute("1").lower()
        output = main.execute("yes").lower().split('\n')

        sizes = []
        for val in output:
            if 'byte' in val:
                for i in val.split():
                    if i.isdigit():
                        sizes.append(int(i))

        if len(sizes) != 3:
            return CheckResult.wrong(f"Wrong number of groups of files")
        if sizes[2] == 32 and sizes[1] == 34 and sizes[0] == 35:
            return CheckResult.correct()
        return CheckResult.wrong(f"Wrong sorting order of files")
    def check_server(self):
        if self.port == '0':
            return CheckResult.wrong(
                f'Please free one of the ports: {", ".join(self.tryout_ports)}'
            )

        for _ in range(15):
            try:
                urlopen(
                    f'http://localhost:{self.port}/not-existing-link-by-default'
                )
                return CheckResult.correct()
            except URLError as err:
                if isinstance(err, HTTPError):
                    return CheckResult.correct()
                sleep(1)
        else:
            return CheckResult.wrong(
                'Cannot start the ./manage.py runserver for 15 seconds')
Example #30
0
 def test_hardest_after_reset(self, reply):
     if "no cards with errors" not in reply.lower():
         return CheckResult.wrong(
             "After the reset of stats, "
             "the line \"There are no cards with errors.\" is expected\n"
             "when the program is asked about the hardest card.\n"
             "However, your program does not seem to respond to this command correctly."
         )
     self.is_completed = True
     return "exit"