sartoriuslib.testing¶
FakeTransport, canned_frames, fixture parsers, and script builders.
See Testing for usage patterns and
Design §8.2 for the fixture format.
sartoriuslib.testing ¶
First-class public testing support — :mod:sartoriuslib.testing.
Re-exports the test doubles and fixture helpers used to drive
:mod:sartoriuslib without real hardware. See design doc §8.2.
Exposed now:
- :class:
FakeTransport— scripted in-process :class:Transport. - :class:
ScriptedReply— the type alias for scripted-reply values. - :class:
CannedFrames— reference xBPI wire frames from real balances, available ascanned_frames. - :func:
build_identify_script— assemble a scripted{tx: rx}dict for :func:open_deviceidentify sequences (model / manufacturer / software / factory / SBN). - :func:
parse_xbpi_fixture— turn a text fixture file (> send/< replylines,#comments) into a scripted mapping ready for :class:FakeTransport. - :func:
parse_sbi_fixture— the SBI counterpart, accepting readable tokens likeESC P.
canned_frames
module-attribute
¶
Module-level singleton of :class:CannedFrames for ergonomic access
(from sartoriuslib.testing import canned_frames).
CannedFrames ¶
Reference xBPI wire frames from real balances.
Attribute values are TX/RX :class:bytes ready to drop into a
:class:FakeTransport script. RX frames use the correct checksum
(not the typo'd 0x55 from docs/protocol.md §3.3 — see
CHANGELOG.md).
FakeTransport ¶
Scripted :class:Transport for tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script
|
Mapping[bytes, ScriptedReply] | None
|
Mapping of |
None
|
label
|
str
|
Identifier used in errors. |
'fake://test'
|
latency_s
|
float
|
Per-operation artificial delay, useful for simulating a slow device. |
0.0
|
Source code in src/sartoriuslib/transport/fake.py
last_reopen_baud
property
¶
Baud rate requested by the most recent :meth:reopen, or None.
last_reopen_parity
property
¶
Parity requested by the most recent :meth:reopen, or None.
last_reopen_stopbits
property
¶
Stop bits requested by the most recent :meth:reopen, or None.
add_script ¶
feed ¶
Push unsolicited bytes into the read buffer.
Useful for simulating a device that was left in SBI autoprint mode, or garbage on the line that the session must drain on recovery.
Source code in src/sartoriuslib/transport/fake.py
force_read_timeout ¶
force_reopen_error ¶
force_write_timeout ¶
reopen
async
¶
Simulate a serial-setting change — close, record, reopen.
If force_reopen_error() has been called the reopen raises
:class:SartoriusConnectionError after the close, leaving the
transport closed. That is the "reopen wedged" path used by
:meth:Balance.configure_protocol tests for the BROKEN-state
transition.
Source code in src/sartoriuslib/transport/fake.py
build_identify_script ¶
build_identify_script(
*,
model="MSE1203S-100-DR",
manufacturer="Sartorius",
software=None,
factory_number=None,
sbn=0,
)
Build a scripted transport mapping for :meth:Balance.identify.
The returned dict covers every TX frame the identify sequence
sends (model, manufacturer, software, factory number, SBN) so a
:class:FakeTransport constructed with it can drive
:func:open_device from end to end without hardware.
Source code in src/sartoriuslib/testing.py
build_metrology_script ¶
Build a {tx: rx} script covering capacity / increment / counter.
Pair with :func:build_identify_script to script a full
open sequence (identity + metrology probe). Defaults match the
MSE1203S unit in docs/protocol.md.
Pass config_counter=None to omit the 0xBA reply entirely —
use that when simulating a balance that genuinely lacks
:attr:Capability.CONFIG_COUNTER (e.g. WZA). The new probe-driven
capability detection in :meth:Balance._probe_dispatch_capabilities
relies on a missing reply (not a falsy value) to decide the cap is
absent, so unfaithful "always reply" fixtures would silently
over-report the capability.
Source code in src/sartoriuslib/testing.py
build_parameter_read_script ¶
Build a one-entry script for a read_parameter(index) call.
Source code in src/sartoriuslib/testing.py
build_parameter_write_script ¶
Build a one-entry script for a write_parameter(index, value) call.
Source code in src/sartoriuslib/testing.py
build_sbi_identify_script ¶
Build a scripted SBI identity mapping for open_device(..., SBI).
Source code in src/sartoriuslib/testing.py
build_temperature_script ¶
Script the per-sensor replies for temperature(N) calls.
sensor_celsius maps sensor index → temperature in °C, or None
to script the 7f ff ff ff "reserved slot" sentinel for that
index. out_of_range_after adds an xBPI 0x04 (unknown opcode)
reply for index N (and the helper does NOT script higher
indices, mirroring the wire reality where the device stops
replying past the end). Useful for testing
:meth:Balance.discover_temperature_sensors against a sparse
layout (e.g. MSE: {0: 25.5, 1: 25.6, 2: None, 3: 36.7} +
out_of_range_after=4).
Source code in src/sartoriuslib/testing.py
parse_sbi_fixture ¶
Parse an SBI text fixture into a {tx_bytes: rx_bytes} mapping.
Format::
# SBI fixture: print
> ESC P
< + 0.00 g
Reply lines get \r\n appended when the fixture omits a terminator.
Multiple < lines after one > concatenate into a multi-line reply.
Source code in src/sartoriuslib/testing.py
parse_xbpi_fixture ¶
Parse an xBPI text fixture into a {tx_bytes: rx_bytes} mapping.
Format (design §8.2)::
# xBPI fixture: read net weight
> 04 01 09 1e 2c
< 0b 41 48 bb a3 d7 0a 3d 30 82 45 07
- Blank lines and
#-comment lines are ignored. >lines carry TX bytes (host→balance).<lines carry RX bytes (balance→host); they attach to the most recent>line.- Hex digits may be separated by whitespace in any form.
- Multiple
<lines following one>concatenate into one reply blob (useful for synthesising multi-frame responses).
Raises :class:SartoriusValidationError for malformed input (an
< line before any >, odd-length hex tokens, etc.).