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