Skip to content

Commit 98beca6

Browse files
committed
Fix setting none as StreamCompression
Add test for stream and consumer metadata Signed-off-by: Waldemar Quevedo <[email protected]>
1 parent d1f2187 commit 98beca6

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

nats/js/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ class StoreCompression(str, Enum):
233233
If stream is file-based and a compression algorithm is specified,
234234
the stream data will be compressed on disk.
235235
236-
Valid options are nothing (empty string) or s2 for Snappy compression.
236+
Valid options are none or s2 for Snappy compression.
237237
Introduced in nats-server 2.10.0.
238238
"""
239239

240-
NONE = ""
240+
NONE = "none"
241241
S2 = "s2"
242242

243243

@@ -327,8 +327,10 @@ def as_dict(self) -> Dict[str, object]:
327327
result['max_age'] = self._to_nanoseconds(self.max_age)
328328
if self.sources:
329329
result['sources'] = [src.as_dict() for src in self.sources]
330-
if self.compression and self.compression != StoreCompression.NONE and self.compression != StoreCompression.S2:
330+
if self.compression and (self.compression != StoreCompression.NONE and self.compression != StoreCompression.S2):
331331
raise ValueError("nats: invalid store compression type: %s" % self.compression)
332+
if self.metadata and not isinstance(self.metadata, dict):
333+
raise ValueError("nats: invalid metadata format")
332334
return result
333335

334336

tests/test_js.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,4 +3896,54 @@ async def test_stream_compression(self):
38963896
compression="s3",
38973897
)
38983898
assert str(err.value) == 'nats: invalid store compression type: s3'
3899+
3900+
# An empty string means not setting compression, but to be explicit
3901+
# can also use
3902+
js = nc.jetstream()
3903+
await js.add_stream(
3904+
name="NONE",
3905+
subjects=["bar"],
3906+
compression="none",
3907+
)
3908+
sinfo = await js.stream_info("NONE")
3909+
assert sinfo.config.compression == nats.js.api.StoreCompression.NONE
3910+
3911+
# By default it should be using 'none' as the configured compression value.
3912+
js = nc.jetstream()
3913+
await js.add_stream(
3914+
name="NONE2",
3915+
subjects=["quux"],
3916+
)
3917+
sinfo = await js.stream_info("NONE2")
3918+
assert sinfo.config.compression == nats.js.api.StoreCompression.NONE
3919+
await nc.close()
3920+
3921+
@async_test
3922+
async def test_stream_consumer_metadata(self):
3923+
nc = await nats.connect()
3924+
3925+
js = nc.jetstream()
3926+
await js.add_stream(
3927+
name="META",
3928+
subjects=["test", "foo"],
3929+
metadata={'foo':'bar'},
3930+
)
3931+
sinfo = await js.stream_info("META")
3932+
assert sinfo.config.metadata['foo'] == 'bar'
3933+
3934+
with pytest.raises(ValueError) as err:
3935+
await js.add_stream(
3936+
name="META2",
3937+
subjects=["test", "foo"],
3938+
metadata=["hello", "world"],
3939+
)
3940+
assert str(err.value) == 'nats: invalid metadata format'
3941+
3942+
await js.add_consumer("META", config=nats.js.api.ConsumerConfig(
3943+
durable_name="b",
3944+
metadata={'hello':'world'}
3945+
))
3946+
cinfo = await js.consumer_info("META", "b")
3947+
assert cinfo.config.metadata['hello'] == 'world'
3948+
38993949
await nc.close()

0 commit comments

Comments
 (0)