MCPcopy Create free account
hub / github.com/apache/arrow / test_map

Function test_map

python/pyarrow/tests/test_scalars.py:788–830  ·  view source on GitHub ↗
(pickle_module)

Source from the content-addressed store, hash-verified

786
787
788def test_map(pickle_module):
789 ty = pa.map_(pa.string(), pa.int8())
790 v = [('a', 1), ('b', 2)]
791 s = pa.scalar(v, type=ty)
792
793 assert len(s) == 2
794 assert isinstance(s, pa.MapScalar)
795 assert isinstance(s.values, pa.Array)
796 assert repr(s) == "<pyarrow.MapScalar: [('a', 1), ('b', 2)]>"
797 assert s.values.to_pylist() == [
798 {'key': 'a', 'value': 1},
799 {'key': 'b', 'value': 2}
800 ]
801
802 # test iteration
803 for i, j in zip(s, v):
804 assert i == j
805
806 # test iteration with missing values
807 for _ in pa.scalar(None, type=ty):
808 pass
809
810 assert s.as_py() == v
811 assert s[1] == (
812 pa.scalar('b', type=pa.string()),
813 pa.scalar(2, type=pa.int8())
814 )
815 assert s[-1] == s[1]
816 assert s[-2] == s[0]
817 assert s['b'] == pa.scalar(2, type=pa.int8())
818 with pytest.raises(IndexError):
819 s[-3]
820 with pytest.raises(IndexError):
821 s[2]
822 with pytest.raises(KeyError):
823 s['fake_key']
824
825 restored = pickle_module.loads(pickle_module.dumps(s))
826 assert restored.equals(s)
827
828 assert s.as_py(maps_as_pydicts="strict") == {'a': 1, 'b': 2}
829
830 assert isinstance(s, Mapping)
831
832
833def test_map_duplicate_fields():

Callers

nothing calls this directly

Calls 7

lenFunction · 0.85
as_pyMethod · 0.80
loadsMethod · 0.80
dumpsMethod · 0.80
equalsMethod · 0.80
stringMethod · 0.45
scalarMethod · 0.45

Tested by

no test coverage detected