-
Notifications
You must be signed in to change notification settings - Fork 92
PLAT-154: Migrate test_external #1127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
A-Baji
merged 5 commits into
datajoint:dev-tests
from
ethho:dev-tests-plat-154-external
Dec 12, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import numpy as np | ||
| from numpy.testing import assert_array_equal | ||
| from datajoint.external import ExternalTable | ||
| from datajoint.blob import pack, unpack | ||
| import datajoint as dj | ||
| from .schema_external import SimpleRemote, Simple | ||
| import os | ||
|
|
||
|
|
||
| def test_external_put(schema_ext, mock_stores, mock_cache): | ||
| """ | ||
| external storage put and get and remove | ||
| """ | ||
| ext = ExternalTable( | ||
| schema_ext.connection, store="raw", database=schema_ext.database | ||
| ) | ||
| initial_length = len(ext) | ||
| input_ = np.random.randn(3, 7, 8) | ||
| count = 7 | ||
| extra = 3 | ||
| for i in range(count): | ||
| hash1 = ext.put(pack(input_)) | ||
| for i in range(extra): | ||
| hash2 = ext.put(pack(np.random.randn(4, 3, 2))) | ||
|
|
||
| fetched_hashes = ext.fetch("hash") | ||
| assert all(hash in fetched_hashes for hash in (hash1, hash2)) | ||
| assert len(ext) == initial_length + 1 + extra | ||
|
|
||
| output_ = unpack(ext.get(hash1)) | ||
| assert_array_equal(input_, output_) | ||
|
|
||
|
|
||
| class TestLeadingSlash: | ||
| def test_s3_leading_slash(self, schema_ext, mock_stores, mock_cache, minio_client): | ||
| """ | ||
| s3 external storage configured with leading slash | ||
| """ | ||
| self._leading_slash(schema_ext, index=100, store="share") | ||
|
|
||
| def test_file_leading_slash( | ||
| self, schema_ext, mock_stores, mock_cache, minio_client | ||
| ): | ||
| """ | ||
| File external storage configured with leading slash | ||
| """ | ||
| self._leading_slash(schema_ext, index=200, store="local") | ||
|
|
||
| def _leading_slash(self, schema_ext, index, store): | ||
| oldConfig = dj.config["stores"][store]["location"] | ||
| value = np.array([1, 2, 3]) | ||
|
|
||
| id = index | ||
| dj.config["stores"][store]["location"] = "leading/slash/test" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 1 | ||
| dj.config["stores"][store]["location"] = "/leading/slash/test" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 2 | ||
| dj.config["stores"][store]["location"] = "leading\\slash\\test" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 3 | ||
| dj.config["stores"][store]["location"] = "f:\\leading\\slash\\test" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 4 | ||
| dj.config["stores"][store]["location"] = "f:\\leading/slash\\test" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 5 | ||
| dj.config["stores"][store]["location"] = "/" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 6 | ||
| dj.config["stores"][store]["location"] = "C:\\" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| id = index + 7 | ||
| dj.config["stores"][store]["location"] = "" | ||
| SimpleRemote.insert([{"simple": id, "item": value}]) | ||
| assert np.array_equal( | ||
| value, (SimpleRemote & "simple={}".format(id)).fetch1("item") | ||
| ) | ||
|
|
||
| dj.config["stores"][store]["location"] = oldConfig | ||
|
|
||
|
|
||
| def test_remove_fail(schema_ext, mock_stores, mock_cache, minio_client): | ||
| """ | ||
| https://github.com/datajoint/datajoint-python/issues/953 | ||
| """ | ||
| assert dj.config["stores"]["local"]["location"] | ||
|
|
||
| data = dict(simple=2, item=[1, 2, 3]) | ||
| Simple.insert1(data) | ||
| path1 = dj.config["stores"]["local"]["location"] + "/djtest_extern/4/c/" | ||
| currentMode = int(oct(os.stat(path1).st_mode), 8) | ||
| os.chmod(path1, 0o40555) | ||
| (Simple & "simple=2").delete() | ||
| listOfErrors = schema_ext.external["local"].delete(delete_external_files=True) | ||
|
|
||
| assert ( | ||
| len(schema_ext.external["local"] & dict(hash=listOfErrors[0][0])) == 1 | ||
| ), "unexpected number of rows in external table" | ||
| # ---------------------CLEAN UP-------------------- | ||
| os.chmod(path1, currentMode) | ||
| listOfErrors = schema_ext.external["local"].delete(delete_external_files=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain
tmpdir_factoryfor me?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's similar to the builtin
tmpdir, except that it's a fixture that will ensure that the temp dirs for different tests have different base directories. Not as important in this case, since the fixture is session-scoped, but if we changed the scope tofunctionthis would be important.https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-factory-fixture