Skip to content

Commit d9bbc99

Browse files
committed
Do not need to avoid doing import_module multiple times. Add a test for reload method and move tests for Zarr class into a separate file
1 parent 3f218c9 commit d9bbc99

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

Zarr.m

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@
3434

3535
function zarrPy = ZarrPy()
3636
% Get ZarrPy Python module
37-
38-
sys = py.importlib.import_module('sys');
39-
loadedModules = sys.modules;
40-
zarrPy = loadedModules.get("ZarrPy", 0);
4137

42-
% if the zarrPy module is not loaded
43-
if isnumeric(zarrPy)
44-
zarrPy = py.importlib.import_module('ZarrPy');
45-
end
38+
% Python will compile and cache the module after the first call
39+
% to import_module, so there is no harm in making this call
40+
% multiple times.
41+
zarrPy = py.importlib.import_module('ZarrPy');
4642
end
4743

4844
function pyReloadInProcess()
@@ -51,7 +47,7 @@ function pyReloadInProcess()
5147
% this call. For Out-of-Process Python, can just use
5248
% `terminate(pyenv)` instead.
5349

54-
py.importlib.reload(Zarr.ZarrPy)
50+
py.importlib.reload(Zarr.ZarrPy);
5551
end
5652

5753
function isZarray = isZarrArray(path)

test/tZarr.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
classdef tZarr < SharedZarrTestSetup
2+
% Tests for Zarr class methods
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
methods(Test)
7+
8+
function verifySupportedCloudPatterns(testcase)
9+
% Verify that the bucket name and the array path can be
10+
% extracted successfully if a cloud path is used as an input.
11+
12+
% This list contains path pattern currently supported by Zarr
13+
% in MATLAB. Any invalid path not matching any of these
14+
% patterns will result in an error.
15+
inpPath = {'https://mybucket.s3.us-west-2.amazonaws.com/path/to/myZarrFile', ...
16+
'https://mybucket.s3.amazonaws.com/path/to/myZarrFile', ...
17+
'https://mybucket.s3.custom-endpoint.org/path/to/myZarrFile', ...
18+
'https://s3.amazonaws.com/mybucket/path/to/myZarrFile', ...
19+
'https://s3.eu-central-1.example.edu/mybucket/path/to/myZarrFile', ...
20+
's3://mybucket/path/to/myZarrFile'};
21+
22+
for i = 1:length(inpPath)
23+
[bucketName, objectPath] = Zarr.extractS3BucketNameAndPath(inpPath{i});
24+
testcase.verifyEqual(bucketName, 'mybucket', ['Bucket name extraction failed for ' inpPath{i}]);
25+
testcase.verifyEqual(objectPath, 'path/to/myZarrFile', ['Object path extraction failed for ' inpPath{i}]);
26+
end
27+
end
28+
29+
function verifyReload(testcase)
30+
% Verify that calling reload method does not cause any issues
31+
32+
Zarr.pyReloadInProcess();
33+
zarrPyModule = Zarr.ZarrPy;
34+
testcase.verifyTrue(isa(zarrPyModule, 'py.module'))
35+
36+
end
37+
38+
39+
end
40+
end

test/tZarrCreate.m

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ function createArrayRelativePath(testcase)
6262
testcase.verifyEqual(arrInfo.node_type,'array','Unexpected Zarr array node type');
6363
end
6464

65-
function verifySupportedCloudPatterns(testcase)
66-
% Verify that the bucket name and the array path can be
67-
% extracted successfully if a cloud path is used as an input.
68-
69-
% This list contains path pattern currently supported by Zarr
70-
% in MATLAB. Any invalid path not matching any of these
71-
% patterns will result in an error.
72-
inpPath = {'https://mybucket.s3.us-west-2.amazonaws.com/path/to/myZarrFile', ...
73-
'https://mybucket.s3.amazonaws.com/path/to/myZarrFile', ...
74-
'https://mybucket.s3.custom-endpoint.org/path/to/myZarrFile', ...
75-
'https://s3.amazonaws.com/mybucket/path/to/myZarrFile', ...
76-
'https://s3.eu-central-1.example.edu/mybucket/path/to/myZarrFile', ...
77-
's3://mybucket/path/to/myZarrFile'};
78-
79-
for i = 1:length(inpPath)
80-
[bucketName, objectPath] = Zarr.extractS3BucketNameAndPath(inpPath{i});
81-
testcase.verifyEqual(bucketName, 'mybucket', ['Bucket name extraction failed for ' inpPath{i}]);
82-
testcase.verifyEqual(objectPath, 'path/to/myZarrFile', ['Object path extraction failed for ' inpPath{i}]);
83-
end
84-
end
85-
8665
function invalidFilePath(testcase)
8766
% Verify error when an invalid file path is used as an input to
8867
% zarrcreate function.

0 commit comments

Comments
 (0)