def setUp(self) -> None: from_templates = test_utils.get_many_login_forms(num=5) self.form_manager = FormManager(data_store=self.data_store) self.form_manager.form_cache.max_len = 3 for form_template in from_templates: self.form_manager.insert_form( form_template=form_template.to_dict())
def setUpClass(cls) -> None: client = MongoClient() cls.collection = client["test"]["form_test"] cls.data_store = MongoDataStore(cls.collection) cls.form_manager = FormManager(data_store=cls.data_store)
def test_init_wrong_form_parser(self): class UselessParser: pass with self.assertRaises(FormManagerException): client = MongoClient() FormManager(data_store=MongoDataStore( client["test_db"]["test_collection"]), format_parser=UselessParser())
def create_app(): app = Flask(__name__) config_app(app) # Restplus API app.config["ERROR_404_HELP"] = False con = connect( app.config["MONGODB_DB"], host=app.config["MONGODB_HOST"], port=app.config["MONGODB_PORT"], username=app.config["MONGODB_USERNAME"], password=app.config["MONGODB_PASSWORD"], ) # TODO: Find a better way to set the collection names from metadata_registration_api.model import Property, ControlledVocabulary, Form, User, Study # noinspection PyProtectedMember Property._meta["collection"] = app.config["MONGODB_COL_PROPERTY"] # noinspection PyProtectedMember ControlledVocabulary._meta["collection"] = app.config[ "MONGODB_COL_CTRL_VOC"] # noinspection PyProtectedMember Form._meta["collection"] = app.config["MONGODB_COL_FORM"] # noinspection PyProtectedMember User._meta["collection"] = app.config["MONGODB_COL_USER"] # noinspection PyProtectedMember Study._meta["collection"] = app.config["MONGODB_COL_STUDY"] api.init_app( app, title="Metadata Registration API", contact="Rafael Müller", contact_email="*****@*****.**", description="An API to register and manage study related metadata." "\n\n" "The code is available here: https://github.com/BEDApub/metadata-registration-api. " "Any issue reports or feature requests are appreciated.", ) # Initialize FormManager url = "http://" + os.environ["API_HOST"] + ":" + str(os.environ["PORT"]) app.config["URL"] = url data_store = ApiDataStore() app.form_manager = FormManager(data_store=data_store, initial_load=False) app.study_state_machine = context.Context() logger.info(f"Created Flask API and exposed {url}") return app
class TestFormManagerManyForms(unittest.TestCase): @classmethod def setUpClass(cls) -> None: client = MongoClient() cls.collection = client["test"]["form_test"] cls.collection.drop() cls.data_store = MongoDataStore(cls.collection) def setUp(self) -> None: from_templates = test_utils.get_many_login_forms(num=5) self.form_manager = FormManager(data_store=self.data_store) self.form_manager.form_cache.max_len = 3 for form_template in from_templates: self.form_manager.insert_form( form_template=form_template.to_dict()) def test_many_forms(self): names = self.form_manager.get_cached_form_names() self.assertEqual(len(names), 3) LoginForm_0 = self.form_manager.get_form_by_name("user_login_0") self.assertTrue(issubclass(LoginForm_0, wtforms.Form))
def setUp(self) -> None: self.form_manager = FormManager(data_store=self.data_store)
class TestFromFormManagerFetch(unittest.TestCase): @classmethod def setUpClass(cls) -> None: form_template = test_utils.get_login_form() client = MongoClient() cls.collection = client["test"]["form_test"] cls.collection.drop() cls.data_store = MongoDataStore(cls.collection) cls.data_store.insert_form(form_template=form_template.to_dict()) def setUp(self) -> None: self.form_manager = FormManager(data_store=self.data_store) def test_load_form_by_name_from_cache(self): LoginForm = self.form_manager.get_form_by_name("user_login") self.assertTrue(issubclass(LoginForm, wtforms.Form)) def test_load_form_by_name_expired_cache(self): """Manually reduce expiration time""" self.form_manager.set_max_cache_age(0.01) time.sleep(0.02) LoginForm = self.form_manager.get_form_by_name("user_login") self.assertTrue(issubclass(LoginForm, wtforms.Form)) def test_load_form_by_nonexisting_name(self): with self.assertRaises(FormManagerException): self.form_manager.get_form_by_name("nonexisting") def test_cached_form_names(self): names = self.form_manager.get_cached_form_names() self.assertListEqual(list(names), ["user_login"]) def test_update_form_cache(self): self.form_manager.form_cache.clear() self.assertEqual(len(self.form_manager.get_cached_form_names()), 0) self.form_manager.update_form_cache() self.assertEqual(len(self.form_manager.get_cached_form_names()), 1)
def test_init_without_initial_load(self): form_manager = FormManager(self.data_store, initial_load=False) self.assertEqual(len(form_manager.get_cached_form_names()), 0)
def test_init_wrong_data_store(self): class DataStore: pass with self.assertRaises(FormManagerException): FormManager(data_store=DataStore)