|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import tiledb |
| 7 | +import tiledb.libtiledb as lt |
| 8 | + |
| 9 | +from .common import DiskTestCase |
| 10 | + |
| 11 | +if not (lt.version()[0] == 2 and lt.version()[1] >= 29): |
| 12 | + pytest.skip( |
| 13 | + "Profile is only available in TileDB 2.29 and later", |
| 14 | + allow_module_level=True, |
| 15 | + ) |
| 16 | + |
| 17 | +""" |
| 18 | +Due to the nature of Profiles, they are touching the filesystem, |
| 19 | +so we need to be careful and not affect the user's Profiles. |
| 20 | +Thus we use DiskTestCase to create temporary directories. |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +class ProfileTestCase(DiskTestCase): |
| 25 | + def setup_method(self): |
| 26 | + super().setup_method() |
| 27 | + self.profile1 = tiledb.Profile( |
| 28 | + dir=self.path("profile1_dir") |
| 29 | + ) # profile with custom directory |
| 30 | + self.profile2 = tiledb.Profile( |
| 31 | + "profile2_name", self.path("profile2_dir") |
| 32 | + ) # named profile with custom directory |
| 33 | + |
| 34 | + |
| 35 | +class ProfileTest(ProfileTestCase): |
| 36 | + def test_profile_name(self): |
| 37 | + assert self.profile1.name == "default" |
| 38 | + assert self.profile2.name == "profile2_name" |
| 39 | + |
| 40 | + def test_profile_dir(self): |
| 41 | + assert Path(self.profile1.dir) == Path(self.path("profile1_dir")) |
| 42 | + assert Path(self.profile2.dir) == Path(self.path("profile2_dir")) |
| 43 | + |
| 44 | + def test_profile_set_get_param(self): |
| 45 | + username = "my_username" |
| 46 | + server_address = "https://my.address" |
| 47 | + |
| 48 | + self.profile1["rest.username"] = username |
| 49 | + assert self.profile1["rest.username"] == username |
| 50 | + |
| 51 | + self.profile1["rest.server_address"] = server_address |
| 52 | + assert self.profile1["rest.server_address"] == server_address |
| 53 | + |
| 54 | + def test_profile_repr(self): |
| 55 | + password = "testing_the_password" |
| 56 | + payer_namespace = "testing_the_namespace" |
| 57 | + server_address = "https://testing_the_address.com" |
| 58 | + token = "testing_the_token" |
| 59 | + username = "testing_the_username" |
| 60 | + |
| 61 | + self.profile1["rest.password"] = password |
| 62 | + self.profile1["rest.payer_namespace"] = payer_namespace |
| 63 | + self.profile1["rest.server_address"] = server_address |
| 64 | + self.profile1["rest.token"] = token |
| 65 | + self.profile1["rest.username"] = username |
| 66 | + |
| 67 | + goal_dict = { |
| 68 | + "default": { |
| 69 | + "rest.password": password, |
| 70 | + "rest.payer_namespace": payer_namespace, |
| 71 | + "rest.server_address": server_address, |
| 72 | + "rest.token": token, |
| 73 | + "rest.username": username, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assert goal_dict == json.loads(repr(self.profile1)) |
| 78 | + |
| 79 | + def test_profile_save_load_remove(self): |
| 80 | + token = "testing_the_token_for_profile2" |
| 81 | + payer_namespace = "testing_the_namespace_for_profile2" |
| 82 | + default_server_address = "https://api.tiledb.com" |
| 83 | + |
| 84 | + self.profile2["rest.token"] = token |
| 85 | + self.profile2["rest.payer_namespace"] = payer_namespace |
| 86 | + |
| 87 | + # save the profile |
| 88 | + self.profile2.save() |
| 89 | + |
| 90 | + # load the profile |
| 91 | + loaded_profile = tiledb.Profile.load("profile2_name", self.path("profile2_dir")) |
| 92 | + |
| 93 | + # check that the loaded profile has the same parameters |
| 94 | + assert loaded_profile.name == "profile2_name" |
| 95 | + assert Path(loaded_profile.dir) == Path(self.path("profile2_dir")) |
| 96 | + assert loaded_profile["rest.username"] == "" |
| 97 | + assert loaded_profile["rest.password"] == "" |
| 98 | + assert loaded_profile["rest.server_address"] == default_server_address |
| 99 | + assert loaded_profile["rest.token"] == token |
| 100 | + assert loaded_profile["rest.payer_namespace"] == payer_namespace |
| 101 | + |
| 102 | + # remove the profile |
| 103 | + tiledb.Profile.remove("profile2_name", self.path("profile2_dir")) |
| 104 | + |
| 105 | + |
| 106 | +class ConfigWithProfileTest(ProfileTestCase): |
| 107 | + def test_config_with_profile(self): |
| 108 | + username = "username_coming_from_profile" |
| 109 | + password = "password_coming_from_profile" |
| 110 | + server_address = "https://profile_address.com" |
| 111 | + |
| 112 | + # Create a profile and set some parameters |
| 113 | + profile = tiledb.Profile(dir=self.path("profile_with_config_dir")) |
| 114 | + profile["rest.username"] = username |
| 115 | + profile["rest.password"] = password |
| 116 | + profile["rest.server_address"] = server_address |
| 117 | + |
| 118 | + # Save the profile |
| 119 | + profile.save() |
| 120 | + |
| 121 | + # ----- |
| 122 | + # The above is done only once, so we can use the same profile later |
| 123 | + # ----- |
| 124 | + |
| 125 | + # Create a config and set the profile directory |
| 126 | + config = tiledb.Config() |
| 127 | + config["profile_dir"] = self.path("profile_with_config_dir") |
| 128 | + # Test that the config parameters are set correctly |
| 129 | + assert config["rest.username"] == username |
| 130 | + assert config["rest.password"] == password |
| 131 | + assert config["rest.server_address"] == server_address |
| 132 | + |
| 133 | + # Alternatively, we can set the profile details directly in the Config constructor |
| 134 | + config2 = tiledb.Config({"profile_dir": self.path("profile_with_config_dir")}) |
| 135 | + # Test that the config parameters are set correctly |
| 136 | + assert config2["rest.username"] == username |
| 137 | + assert config2["rest.password"] == password |
| 138 | + assert config2["rest.server_address"] == server_address |
0 commit comments