From e2781b14f18e5b295eaabd4169892369fa0486d7 Mon Sep 17 00:00:00 2001 From: Martijn Stegeman Date: Thu, 26 Oct 2023 09:40:34 +0200 Subject: [PATCH] replace filenames in student code --- tests/bigdata/autoritTest.py | 94 +++++++++++------- tests/bigdata/temperatuurTest.py | 158 ++++++++++++++++++------------- tests/notAllowedCode.py | 2 +- 3 files changed, 150 insertions(+), 104 deletions(-) diff --git a/tests/bigdata/autoritTest.py b/tests/bigdata/autoritTest.py index baa7dbe..62cf2aa 100644 --- a/tests/bigdata/autoritTest.py +++ b/tests/bigdata/autoritTest.py @@ -3,51 +3,73 @@ import checkpy.assertlib as assertlib import importlib +import re + from checkpy import * include("autorit.py") download("AutoRitData.csv", "http://www.nikhef.nl/~ivov/Python/SensorData/AutoRitData.csv") def before(): - try: - import warnings - warnings.filterwarnings("ignore") - import matplotlib - matplotlib.use("Agg") - matplotlib.use = lambda x, warn=None, force=None: x - import matplotlib.pyplot as plt - plt.switch_backend("Agg") - lib.neutralizeFunction(plt.pause) - lib.neutralizeFunction(plt.subplot) - lib.neutralizeFunction(plt.show) - # lib.neutralizeFunction(matplotlib.use) - except ImportError: - pass + try: + import warnings + warnings.filterwarnings("ignore") + import matplotlib + matplotlib.use("Agg") + matplotlib.use = lambda x, warn=None, force=None: x + import matplotlib.pyplot as plt + plt.switch_backend("Agg") + lib.neutralizeFunction(plt.pause) + lib.neutralizeFunction(plt.subplot) + lib.neutralizeFunction(plt.show) + # lib.neutralizeFunction(matplotlib.use) + except ImportError: + pass def after(): - try: - import matplotlib.pyplot as plt - # plt.switch_backend("TkAgg") - importlib.reload(plt) - except ImportError: - pass + try: + import matplotlib.pyplot as plt + # plt.switch_backend("TkAgg") + importlib.reload(plt) + except ImportError: + pass + +# @t.test(10) +# TODO this test is not registered because it would change the # of tests used for assigning points +def checkDatafile(test): + """bestand is in orde""" + global _fileName + + with open(_fileName, 'r') as f: + file_contents = f.read() + new_file_contents = re.sub(r'with\s+open\s*\(\s*[\'"][^\'"]*[\'"]', 'with open("AutoRitData.csv"', file_contents) + + tempfile = f"_{_fileName}.tmp" + with open(tempfile, 'w') as f: + f.write(new_file_contents) -@t.test(0) + _fileName = tempfile + + return True + +@t.test(20) def correctDistance(test): - def testMethod(): - output = lib.outputOf( - test.fileName, - overwriteAttributes = [("__name__", "__main__")] - ) - line = lib.getLine(output, 0) - correctKm = assertlib.numberOnLine(10.86, line, deviation = 0.02) - correctM = assertlib.numberOnLine(10860, line, deviation = 20) - return correctKm or correctM - test.test = testMethod - test.description = lambda : "print de afgelegde afstand" - -@t.test(1) + checkDatafile(test) + + def testMethod(): + output = lib.outputOf( + _fileName, + overwriteAttributes = [("__name__", "__main__")] + ) + line = lib.getLine(output, 0) + correctKm = assertlib.numberOnLine(10.86, line, deviation = 0.02) + correctM = assertlib.numberOnLine(10860, line, deviation = 20) + return correctKm or correctM + test.test = testMethod + test.description = lambda : "print de afgelegde afstand" + +@t.test(30) def showsGraph(test): - test.test = lambda : assertlib.fileContainsFunctionCalls(_fileName, "savefig") or assertlib.fileContainsFunctionCalls(_fileName, "show") - test.description = lambda : "slaat een grafiek op, of laat een grafiek zien" + test.test = lambda : assertlib.fileContainsFunctionCalls(_fileName, "savefig") or assertlib.fileContainsFunctionCalls(_fileName, "show") + test.description = lambda : "slaat een grafiek op, of laat een grafiek zien" diff --git a/tests/bigdata/temperatuurTest.py b/tests/bigdata/temperatuurTest.py index e6e4540..830aa53 100644 --- a/tests/bigdata/temperatuurTest.py +++ b/tests/bigdata/temperatuurTest.py @@ -12,97 +12,121 @@ download("DeBiltTempMinSUMMER2023.txt", "https://raw.githubusercontent.com/spcourse/progns/2023/bigdata/data/DeBiltTempMinSUMMER2023.txt") def before(): - try: - import warnings - warnings.filterwarnings("ignore") - import matplotlib - matplotlib.use("Agg") - matplotlib.use = lambda x, warn=None, force=None: x - import matplotlib.pyplot as plt - plt.switch_backend("Agg") - lib.neutralizeFunction(plt.pause) - lib.neutralizeFunction(plt.subplot) - lib.neutralizeFunction(plt.show) - # lib.neutralizeFunction(matplotlib.use) - except ImportError: - pass + try: + import warnings + warnings.filterwarnings("ignore") + import matplotlib + matplotlib.use("Agg") + matplotlib.use = lambda x, warn=None, force=None: x + import matplotlib.pyplot as plt + plt.switch_backend("Agg") + lib.neutralizeFunction(plt.pause) + lib.neutralizeFunction(plt.subplot) + lib.neutralizeFunction(plt.show) + # lib.neutralizeFunction(matplotlib.use) + except ImportError: + pass def after(): - try: - # import matplotlib - import matplotlib.pyplot as plt - # plt.switch_backend("TkAgg") - # importlib.reload(matplotlib) - importlib.reload(plt) - except ImportError: - pass + try: + # import matplotlib + import matplotlib.pyplot as plt + # plt.switch_backend("TkAgg") + # importlib.reload(matplotlib) + importlib.reload(plt) + except ImportError: + pass # Thanks to Vera Schild! -@t.test(10) +# TODO this test is not registered because it would change the # of tests used for assigning points +# @t.test(10) +def checkDatafile(test): + """bestand is in orde""" + global _fileName + + with open(_fileName, 'r') as f: + file_contents = f.read() + + new_file_contents = re.sub(r'with\s+open\s*\(\s*[\'"][^\'"]*[mM][aA][xX][^\'"]*[\'"]', 'with open("DeBiltTempMaxSUMMER2023.txt"', file_contents) + new_file_contents = re.sub(r'with\s+open\s*\(\s*[\'"][^\'"]*[mM][iI][nN][^\'"]*[\'"]', 'with open("DeBiltTempMinSUMMER2023.txt"', new_file_contents) + + tempfile = f"_{_fileName}.tmp" + with open(tempfile, 'w') as f: + f.write(new_file_contents) + + test.description = new_file_contents + + _fileName = tempfile + + return True + +@t.test(20) def correctHighestTemp(test): - def testMethod(): + checkDatafile(test) + + def testMethod(): - # Max temp - correctAnswerMax = [37.5, 25, 2019] - if helpers.isHardcodedIn(correctAnswerMax[0], test.fileName): - test.success = lambda info : "let op: deze output is hardcoded. {} staat in de source code!".format(correctAnswerMax) + # Max temp + correctAnswerMax = [37.5, 25, 2019] + if helpers.isHardcodedIn(correctAnswerMax[0], _fileName): + test.success = lambda info : "let op: deze output is hardcoded. {} staat in de source code!".format(correctAnswerMax) - line1 = lib.getLine(lib.outputOf(_fileName), 0) - numbersMax = lib.getNumbersFromString(line1) + line1 = lib.getLine(lib.outputOf(_fileName), 0) + numbersMax = lib.getNumbersFromString(line1) - correctMonthMax = any([assertlib.contains(line1.lower(), month) for month in ["juli", "July", "jul", "Jul"]]) + correctMonthMax = any([assertlib.contains(line1.lower(), month) for month in ["juli", "July", "jul", "Jul"]]) - # Min temp - correctAnswerMin = [-24.7, 27, 1942] + # Min temp + correctAnswerMin = [-24.7, 27, 1942] - line2 = lib.getLine(lib.outputOf(_fileName), 1) - numbersMin = lib.getNumbersFromString(line2) + line2 = lib.getLine(lib.outputOf(_fileName), 1) + numbersMin = lib.getNumbersFromString(line2) - correctMonthMin = any([assertlib.contains(line2.lower(), month) for month in ["januari", "January", "jan", "Jan"]]) - - # Total check - if sum(1 for n in numbersMax if n in correctAnswerMax) != 3 or not correctMonthMax or sum(1 for n in numbersMin if n in correctAnswerMin) != 3 or not correctMonthMin: - return False - return True + correctMonthMin = any([assertlib.contains(line2.lower(), month) for month in ["januari", "January", "jan", "Jan"]]) - test.test = testMethod - test.description = lambda : "print hoogste en laagste temperatuur ooit met correcte datum" + # Total check + if sum(1 for n in numbersMax if n in correctAnswerMax) != 3 or not correctMonthMax or sum(1 for n in numbersMin if n in correctAnswerMin) != 3 or not correctMonthMin: + return False + return True + + test.test = testMethod + test.description = lambda : "print hoogste en laagste temperatuur ooit met correcte datum" @t.passed(correctHighestTemp) -@t.test(11) +@t.test(30) def correctIjstijd(test): - def testMethod(): - if helpers.isHardcodedIn(1947, test.fileName): - test.success = lambda info : "let op: deze output is gehardcode. 1947 staat in de source code!" + def testMethod(): + if helpers.isHardcodedIn(1947, _fileName): + test.success = lambda info : "let op: deze output is gehardcode. 1947 staat in de source code!" + + correctAnswerIceAge = [21, 24, 1947] - correctAnswerIceAge = [21, 24, 1947] + line3 = lib.getLine(lib.outputOf(_fileName), 2) + numbersIceAge = lib.getNumbersFromString(line3) - line3 = lib.getLine(lib.outputOf(_fileName), 2) - numbersIceAge = lib.getNumbersFromString(line3) + correctMonthIceAge = any([assertlib.contains(line3.lower(), month) for month in ["februari", "February", "feb", "Feb"]]) - correctMonthIceAge = any([assertlib.contains(line3.lower(), month) for month in ["februari", "February", "feb", "Feb"]]) - - # Total check - if sum(1 for n in numbersIceAge if n in correctAnswerIceAge) != 3 or not correctMonthIceAge: - return False - return True + # Total check + if sum(1 for n in numbersIceAge if n in correctAnswerIceAge) != 3 or not correctMonthIceAge: + return False + return True - test.test = testMethod - test.description = lambda : "print correcte duur en einddatum (dag, maand en jaar) van de kleine ijstijd" + test.test = testMethod + test.description = lambda : "print correcte duur en einddatum (dag, maand en jaar) van de kleine ijstijd" @t.passed(correctIjstijd) -@t.test(21) +@t.test(50) def correctDateLowestTemp(test): - def testMethod(): - correctYear = 1911 + def testMethod(): + correctYear = 1911 - line = lib.getLine(lib.outputOf(_fileName), 3) - numbers = lib.getNumbersFromString(line) + line = lib.getLine(lib.outputOf(_fileName), 3) + numbers = lib.getNumbersFromString(line) - check = correctYear in numbers + check = correctYear in numbers - return check + return check - test.test = testMethod - test.description = lambda : "print het eerste jaar waarin een hittegolf voorkwam" + test.test = testMethod + test.description = lambda : "print het eerste jaar waarin een hittegolf voorkwam" diff --git a/tests/notAllowedCode.py b/tests/notAllowedCode.py index 47b9053..5d552bb 100644 --- a/tests/notAllowedCode.py +++ b/tests/notAllowedCode.py @@ -14,6 +14,6 @@ def notAllowedCode(test, source, notAllowed): if len(abc) > 0: - warning = "Let op dat je geen " + ", ".join(abc) + " gebruikt" + warning = "let op dat je geen " + ", ".join(abc) + " gebruikt" test.fail = lambda info : warning test.success = lambda info : warning