@@ -55,6 +55,69 @@ MUST assert that the input float array is the same after encoding and decoding.
5555- if the canonical_bson field is present, raise an exception when attempting to deserialize it into the corresponding
5656 numeric values, as the field contains corrupted data.
5757
58+ ## Prose Tests
59+
60+ ### Treatment of non-zero ignored bits
61+
62+ All drivers MUST test encoding and decoding behavior according to their design and version. For drivers that haven't
63+ been completed, raise exceptions in both cases. For those that have, update to this behavior according to semantic
64+ versioning rules, and update tests accordingly.
65+
66+ In both cases, [ 255] , a single byte PACKED_BIT vector of length 1 (hence padding of 7) provides a good example to use,
67+ as all of its bits are ones.
68+
69+ #### 1. Encoding
70+
71+ - Test encoding with non-zero ignored bits. Use the driver API that validates vector metadata.
72+ - If the driver validates ignored bits are zero (preferred), expect an error. Otherwise expect the ignored bits are
73+ preserved.
74+
75+ ``` python
76+ with pytest.raises(ValueError ):
77+ Binary.from_vector([0b 11111111 ], BinaryVectorDtype.PACKED_BIT , padding = 7 )
78+ ```
79+
80+ ### 2. Decoding
81+
82+ - Test the behaviour of your driver when one attempts to decode from binary to vector.
83+ - e.g. As of pymongo 4.14, a warning is raised. From 5.0, it will be an exception.
84+
85+ ``` python
86+ b = Binary(b ' \x10\x07\xff ' , subtype = 9 )
87+ with pytest.warns():
88+ Binary.as_vector(b)
89+ ```
90+
91+ Drivers MAY skip this test if they choose not to implement a ` Vector ` type.
92+
93+ ### 3. Comparison
94+
95+ Once we can guarantee that all ignored bits are non-zero, then equality can be tested on the binary subtype. Until then,
96+ equality is ambiguous, and depends on whether one compares by bits (uint1), or uint8. Drivers SHOULD test equality
97+ behavior according to their design and version.
98+
99+ For example, in ` pymongo < 5.0 ` , we define equality of a BinaryVector by matching padding, dtype, and integer. This
100+ means that two single bit vectors in which 7 bits are ignored do not match unless all bits match. This mirrors what the
101+ server does.
102+
103+ ``` python
104+ b1 = Binary(b ' \x10\x07\x80 ' , subtype = 9 ) # 1-bit vector with all 0 ignored bits.
105+ b2 = Binary(b ' \x10\x07\xff ' , subtype = 9 ) # 1-bit vector with all 1 ignored bits.
106+ b3 = Binary.from_vector([0b 10000000 ], BinaryVectorDtype.PACKED_BIT , padding = 7 ) # Same data as b1.
107+
108+ v1 = Binary.as_vector(b1)
109+ v2 = Binary.as_vector(b2)
110+ v3 = Binary.as_vector(b3)
111+
112+ assert b1 != b2 # Unequal at naive Binary level
113+ assert v2 != v1 # Also chosen to be unequal at BinaryVector level as [255] != [128]
114+ assert b1 == b3 # Equal at naive Binary level
115+ assert v1 == v3 # Equal at the BinaryVector level
116+ ```
117+
118+ Drivers MAY skip this test if they choose not to implement a ` Vector ` type, or the type does not support comparison, or
119+ the type cannot be constructed with non-zero ignored bits.
120+
58121## FAQ
59122
60123- What MongoDB Server version does this apply to?
0 commit comments