|
| 1 | +import numpy as np |
| 2 | +from numpy.testing import assert_array_equal |
| 3 | +from datajoint.external import ExternalTable |
| 4 | +from datajoint.blob import pack, unpack |
| 5 | +import datajoint as dj |
| 6 | +from .schema_external import SimpleRemote, Simple |
| 7 | +import os |
| 8 | + |
| 9 | + |
| 10 | +def test_external_put(schema_ext, mock_stores, mock_cache): |
| 11 | + """ |
| 12 | + external storage put and get and remove |
| 13 | + """ |
| 14 | + ext = ExternalTable( |
| 15 | + schema_ext.connection, store="raw", database=schema_ext.database |
| 16 | + ) |
| 17 | + initial_length = len(ext) |
| 18 | + input_ = np.random.randn(3, 7, 8) |
| 19 | + count = 7 |
| 20 | + extra = 3 |
| 21 | + for i in range(count): |
| 22 | + hash1 = ext.put(pack(input_)) |
| 23 | + for i in range(extra): |
| 24 | + hash2 = ext.put(pack(np.random.randn(4, 3, 2))) |
| 25 | + |
| 26 | + fetched_hashes = ext.fetch("hash") |
| 27 | + assert all(hash in fetched_hashes for hash in (hash1, hash2)) |
| 28 | + assert len(ext) == initial_length + 1 + extra |
| 29 | + |
| 30 | + output_ = unpack(ext.get(hash1)) |
| 31 | + assert_array_equal(input_, output_) |
| 32 | + |
| 33 | + |
| 34 | +class TestLeadingSlash: |
| 35 | + def test_s3_leading_slash(self, schema_ext, mock_stores, mock_cache, minio_client): |
| 36 | + """ |
| 37 | + s3 external storage configured with leading slash |
| 38 | + """ |
| 39 | + self._leading_slash(schema_ext, index=100, store="share") |
| 40 | + |
| 41 | + def test_file_leading_slash( |
| 42 | + self, schema_ext, mock_stores, mock_cache, minio_client |
| 43 | + ): |
| 44 | + """ |
| 45 | + File external storage configured with leading slash |
| 46 | + """ |
| 47 | + self._leading_slash(schema_ext, index=200, store="local") |
| 48 | + |
| 49 | + def _leading_slash(self, schema_ext, index, store): |
| 50 | + oldConfig = dj.config["stores"][store]["location"] |
| 51 | + value = np.array([1, 2, 3]) |
| 52 | + |
| 53 | + id = index |
| 54 | + dj.config["stores"][store]["location"] = "leading/slash/test" |
| 55 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 56 | + assert np.array_equal( |
| 57 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 58 | + ) |
| 59 | + |
| 60 | + id = index + 1 |
| 61 | + dj.config["stores"][store]["location"] = "/leading/slash/test" |
| 62 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 63 | + assert np.array_equal( |
| 64 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 65 | + ) |
| 66 | + |
| 67 | + id = index + 2 |
| 68 | + dj.config["stores"][store]["location"] = "leading\\slash\\test" |
| 69 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 70 | + assert np.array_equal( |
| 71 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 72 | + ) |
| 73 | + |
| 74 | + id = index + 3 |
| 75 | + dj.config["stores"][store]["location"] = "f:\\leading\\slash\\test" |
| 76 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 77 | + assert np.array_equal( |
| 78 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 79 | + ) |
| 80 | + |
| 81 | + id = index + 4 |
| 82 | + dj.config["stores"][store]["location"] = "f:\\leading/slash\\test" |
| 83 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 84 | + assert np.array_equal( |
| 85 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 86 | + ) |
| 87 | + |
| 88 | + id = index + 5 |
| 89 | + dj.config["stores"][store]["location"] = "/" |
| 90 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 91 | + assert np.array_equal( |
| 92 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 93 | + ) |
| 94 | + |
| 95 | + id = index + 6 |
| 96 | + dj.config["stores"][store]["location"] = "C:\\" |
| 97 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 98 | + assert np.array_equal( |
| 99 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 100 | + ) |
| 101 | + |
| 102 | + id = index + 7 |
| 103 | + dj.config["stores"][store]["location"] = "" |
| 104 | + SimpleRemote.insert([{"simple": id, "item": value}]) |
| 105 | + assert np.array_equal( |
| 106 | + value, (SimpleRemote & "simple={}".format(id)).fetch1("item") |
| 107 | + ) |
| 108 | + |
| 109 | + dj.config["stores"][store]["location"] = oldConfig |
| 110 | + |
| 111 | + |
| 112 | +def test_remove_fail(schema_ext, mock_stores, mock_cache, minio_client): |
| 113 | + """ |
| 114 | + https://github.com/datajoint/datajoint-python/issues/953 |
| 115 | + """ |
| 116 | + assert dj.config["stores"]["local"]["location"] |
| 117 | + |
| 118 | + data = dict(simple=2, item=[1, 2, 3]) |
| 119 | + Simple.insert1(data) |
| 120 | + path1 = dj.config["stores"]["local"]["location"] + "/djtest_extern/4/c/" |
| 121 | + currentMode = int(oct(os.stat(path1).st_mode), 8) |
| 122 | + os.chmod(path1, 0o40555) |
| 123 | + (Simple & "simple=2").delete() |
| 124 | + listOfErrors = schema_ext.external["local"].delete(delete_external_files=True) |
| 125 | + |
| 126 | + assert ( |
| 127 | + len(schema_ext.external["local"] & dict(hash=listOfErrors[0][0])) == 1 |
| 128 | + ), "unexpected number of rows in external table" |
| 129 | + # ---------------------CLEAN UP-------------------- |
| 130 | + os.chmod(path1, currentMode) |
| 131 | + listOfErrors = schema_ext.external["local"].delete(delete_external_files=True) |
0 commit comments