diff options
| -rw-r--r-- | src/lib.rs | 11 | ||||
| -rw-r--r-- | tests/test_engine.py | 29 |
2 files changed, 38 insertions, 2 deletions
| @@ -17,6 +17,7 @@ use adblock::engine::Engine as RustEngine; | |||
| 17 | use pyo3::class::PyObjectProtocol; | 17 | use pyo3::class::PyObjectProtocol; |
| 18 | use pyo3::exceptions::ValueError as PyValueError; | 18 | use pyo3::exceptions::ValueError as PyValueError; |
| 19 | use pyo3::prelude::*; | 19 | use pyo3::prelude::*; |
| 20 | use pyo3::types::PyBytes; | ||
| 20 | use pyo3::PyErr; | 21 | use pyo3::PyErr; |
| 21 | 22 | ||
| 22 | use std::collections::HashMap; | 23 | use std::collections::HashMap; |
| @@ -348,7 +349,13 @@ impl Engine { | |||
| 348 | /// Serialize this blocking engine to bytes. They can then be deserialized | 349 | /// Serialize this blocking engine to bytes. They can then be deserialized |
| 349 | /// using `deserialize()` to get the same engine again. | 350 | /// using `deserialize()` to get the same engine again. |
| 350 | #[text_signature = "($self)"] | 351 | #[text_signature = "($self)"] |
| 351 | pub fn serialize(&mut self) -> PyResult<Vec<u8>> { | 352 | pub fn serialize<'p>(&mut self, py: Python<'p>) -> PyResult<&'p PyBytes> { |
| 353 | let bytes = self.serialize_inner()?; | ||
| 354 | let py_bytes = PyBytes::new(py, &bytes); | ||
| 355 | Ok(py_bytes) | ||
| 356 | } | ||
| 357 | |||
| 358 | fn serialize_inner(&mut self) -> PyResult<Vec<u8>> { | ||
| 352 | let result = self.engine.serialize(); | 359 | let result = self.engine.serialize(); |
| 353 | match result { | 360 | match result { |
| 354 | Ok(x) => Ok(x), | 361 | Ok(x) => Ok(x), |
| @@ -364,7 +371,7 @@ impl Engine { | |||
| 364 | /// again. | 371 | /// again. |
| 365 | #[text_signature = "($self, file)"] | 372 | #[text_signature = "($self, file)"] |
| 366 | pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> { | 373 | pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> { |
| 367 | let data = self.serialize()?; | 374 | let data = self.serialize_inner()?; |
| 368 | let mut fd = fs::OpenOptions::new() | 375 | let mut fd = fs::OpenOptions::new() |
| 369 | .create(true) | 376 | .create(true) |
| 370 | .truncate(true) | 377 | .truncate(true) |
diff --git a/tests/test_engine.py b/tests/test_engine.py index af15b6d..28e25e6 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | import adblock | 1 | import adblock |
| 2 | import pytest | ||
| 2 | 3 | ||
| 3 | 4 | ||
| 4 | def test_engine_arguments(): | 5 | def test_engine_arguments(): |
| @@ -9,3 +10,31 @@ def test_engine_arguments(): | |||
| 9 | adblock.Engine(network_filters=[]) | 10 | adblock.Engine(network_filters=[]) |
| 10 | adblock.Engine(load_network=False, load_cosmetic=True, debug=False) | 11 | adblock.Engine(load_network=False, load_cosmetic=True, debug=False) |
| 11 | adblock.Engine(debug=True) | 12 | adblock.Engine(debug=True) |
| 13 | |||
| 14 | |||
| 15 | def test_serde_file(tmpdir): | ||
| 16 | path = str(tmpdir / "cache.dat") | ||
| 17 | |||
| 18 | engine0 = adblock.Engine() | ||
| 19 | with pytest.raises(FileNotFoundError): | ||
| 20 | # We haven't created the cache.dat file, so we should get an exception | ||
| 21 | # when attempting to deserialize. | ||
| 22 | engine0.deserialize_from_file(path) | ||
| 23 | |||
| 24 | engine1 = adblock.Engine() | ||
| 25 | serialization_result = engine1.serialize_to_file(path) | ||
| 26 | assert serialization_result is None | ||
| 27 | |||
| 28 | engine2 = adblock.Engine() | ||
| 29 | deserialization_result = engine2.deserialize_from_file(path) | ||
| 30 | assert deserialization_result is None | ||
| 31 | |||
| 32 | |||
| 33 | def test_serde(): | ||
| 34 | engine = adblock.Engine() | ||
| 35 | serialization_result = engine.serialize() | ||
| 36 | assert isinstance(serialization_result, bytes) | ||
| 37 | |||
| 38 | engine2 = adblock.Engine() | ||
| 39 | deserialization_result = engine2.deserialize(serialization_result) | ||
| 40 | assert deserialization_result is None | ||
