|
| 1 | +import time |
| 2 | + |
| 3 | +import osw.model.entity as model |
| 4 | +from osw.auth import CredentialManager |
| 5 | + |
| 6 | + |
| 7 | +def test_fetch_and_load(wiki_domain, wiki_username, wiki_password, mocker): |
| 8 | + # create a credential file on the default path for osw express |
| 9 | + cm = CredentialManager() # cred_filepath = Path.cwd() / "accounts.pwd.yaml") |
| 10 | + cm.add_credential( |
| 11 | + CredentialManager.UserPwdCredential( |
| 12 | + iri=wiki_domain, username=wiki_username, password=wiki_password |
| 13 | + ) |
| 14 | + ) |
| 15 | + # cm.save_credentials_to_file() |
| 16 | + # wtsite = WtSite(WtSite.WtSiteConfig(iri=wiki_domain, cred_mngr=cm)) |
| 17 | + # osw_obj = OSW(site=wtsite) |
| 18 | + |
| 19 | + # Here the initial connection to the wiki is mocked (passing domain, username and |
| 20 | + # password) |
| 21 | + mocked_input = mocker.patch("builtins.input") |
| 22 | + mocked_getpass = mocker.patch("getpass.getpass") |
| 23 | + mocked_input.side_effect = [wiki_domain, wiki_username] |
| 24 | + mocked_getpass.return_value = wiki_password |
| 25 | + # This import will trigger the install_dependencies method call on the first run |
| 26 | + import osw.express |
| 27 | + |
| 28 | + osw_express = osw.express.OswExpress(domain=wiki_domain, cred_mngr=cm) |
| 29 | + |
| 30 | + # Measure the time taken to store and load an entity |
| 31 | + start_time = time.time() |
| 32 | + |
| 33 | + # Load Tutorial Schema on demand |
| 34 | + # if not hasattr(model, "Tutorial"): |
| 35 | + DEPENDENCIES = {"Tutorial": "Category:OSW494f660e6a714a1a9681c517bbb975da"} |
| 36 | + osw_express.install_dependencies(DEPENDENCIES, policy="force") |
| 37 | + |
| 38 | + end_time = time.time() |
| 39 | + print(f"Time taken to load Tutorial Schema: {end_time - start_time}") |
| 40 | + assert hasattr(model, "Tutorial") |
| 41 | + assert end_time - start_time < 15 # typically takes 8 seconds |
| 42 | + |
| 43 | + start_time = time.time() |
| 44 | + osw_express.install_dependencies(DEPENDENCIES, policy="force") |
| 45 | + end_time = time.time() |
| 46 | + print( |
| 47 | + f"Time taken to reload Tutorial Schema with policy 'force': {end_time - start_time}" |
| 48 | + ) |
| 49 | + assert end_time - start_time < 2 # typically takes 1 seconds using memory cache |
| 50 | + |
| 51 | + start_time = time.time() |
| 52 | + osw_express.install_dependencies(DEPENDENCIES, policy="if-missing") |
| 53 | + end_time = time.time() |
| 54 | + print( |
| 55 | + "Time taken to reload Tutorial Schema with policy ", |
| 56 | + "'if-missing': {end_time - start_time}", |
| 57 | + ) |
| 58 | + assert end_time - start_time < 0.1 # typically takes 0 seconds |
0 commit comments