mirror of
https://github.com/Doctorado-ML/benchmark.git
synced 2025-08-17 08:25:53 +00:00
Add no .env exception
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import sys
|
||||
import argparse
|
||||
from .Experiments import Models
|
||||
from .Utils import Files
|
||||
from .Utils import Files, NO_ENV
|
||||
|
||||
ALL_METRICS = (
|
||||
"accuracy",
|
||||
@@ -15,12 +16,17 @@ class EnvData:
|
||||
@staticmethod
|
||||
def load():
|
||||
args = {}
|
||||
try:
|
||||
with open(Files.dot_env) as f:
|
||||
for line in f.read().splitlines():
|
||||
if line == "" or line.startswith("#"):
|
||||
continue
|
||||
key, value = line.split("=")
|
||||
args[key] = value
|
||||
except FileNotFoundError:
|
||||
print(NO_ENV, file=sys.stderr)
|
||||
exit(1)
|
||||
else:
|
||||
return args
|
||||
|
||||
|
||||
|
@@ -3,6 +3,7 @@ import subprocess
|
||||
|
||||
BEST_ACCURACY_STREE = 40.282203
|
||||
NO_RESULTS = "** No results found **"
|
||||
NO_ENV = "File .env not found"
|
||||
|
||||
|
||||
class Folders:
|
||||
|
@@ -1,7 +1,8 @@
|
||||
import os
|
||||
from io import StringIO
|
||||
from unittest.mock import patch
|
||||
from .TestBase import TestBase
|
||||
from ..Arguments import Arguments, ALL_METRICS
|
||||
from ..Arguments import Arguments, ALL_METRICS, NO_ENV
|
||||
|
||||
|
||||
class ArgumentsTest(TestBase):
|
||||
@@ -65,23 +66,35 @@ class ArgumentsTest(TestBase):
|
||||
self.assertEqual(args.key, "metric")
|
||||
|
||||
@patch("sys.stderr", new_callable=StringIO)
|
||||
def test_xset_mandatory(self, mock_stderr):
|
||||
def test_xset_mandatory(self, stderr):
|
||||
arguments = self.build_args()
|
||||
test_args = ["-n", "3", "-k", "date"]
|
||||
with self.assertRaises(SystemExit):
|
||||
arguments.parse(test_args)
|
||||
self.assertRegexpMatches(
|
||||
mock_stderr.getvalue(),
|
||||
stderr.getvalue(),
|
||||
r"error: the following arguments are required: -m/--model",
|
||||
)
|
||||
|
||||
@patch("sys.stderr", new_callable=StringIO)
|
||||
def test_xset_required(self, mock_stderr):
|
||||
def test_xset_required(self, stderr):
|
||||
arguments = self.build_args()
|
||||
test_args = ["-n", "3", "-m", "SVC"]
|
||||
with self.assertRaises(SystemExit):
|
||||
arguments.parse(test_args)
|
||||
self.assertRegexpMatches(
|
||||
mock_stderr.getvalue(),
|
||||
stderr.getvalue(),
|
||||
r"error: the following arguments are required: -k/--key",
|
||||
)
|
||||
|
||||
@patch("sys.stderr", new_callable=StringIO)
|
||||
def test_no_env(self, stderr):
|
||||
path = os.getcwd()
|
||||
os.chdir("..")
|
||||
try:
|
||||
self.build_args()
|
||||
except SystemExit:
|
||||
pass
|
||||
finally:
|
||||
os.chdir(path)
|
||||
self.assertEqual(stderr.getvalue(), f"{NO_ENV}\n")
|
||||
|
@@ -2,7 +2,6 @@ from io import StringIO
|
||||
from unittest.mock import patch
|
||||
from .TestBase import TestBase
|
||||
from ..Results import Summary
|
||||
from ..Utils import NO_RESULTS
|
||||
|
||||
|
||||
class SummaryTest(TestBase):
|
||||
@@ -229,4 +228,4 @@ class SummaryTest(TestBase):
|
||||
report.acquire()
|
||||
with self.assertRaises(ValueError) as msg:
|
||||
report.list_results(score="f1-macro", model="STree")
|
||||
self.assertEqual(str(msg.exception), NO_RESULTS)
|
||||
self.assertEqual(str(msg.exception), "** No results found **")
|
||||
|
@@ -44,3 +44,15 @@ class BeListTest(TestBase):
|
||||
stdout, stderr = self.execute_script("be_list", ["--nan", "1"])
|
||||
self.assertEqual(stderr.getvalue(), "")
|
||||
self.check_output_file(stdout, "be_list_no_nan")
|
||||
|
||||
def test_be_no_env(self):
|
||||
path = os.getcwd()
|
||||
os.chdir("..")
|
||||
stderr = None
|
||||
try:
|
||||
_, stderr = self.execute_script("be_list", [])
|
||||
except SystemExit as e:
|
||||
self.assertEqual(e.code, 1)
|
||||
finally:
|
||||
os.chdir(path)
|
||||
self.assertIsNone(stderr)
|
||||
|
Reference in New Issue
Block a user