|
6 | 6 | from copy import deepcopy |
7 | 7 | from dataclasses import replace |
8 | 8 |
|
| 9 | +import numpy |
9 | 10 | import pytest |
10 | 11 |
|
11 | 12 | from haystack import Document |
@@ -63,47 +64,47 @@ def test_init_with_parameters(): |
63 | 64 | assert doc.sparse_embedding == sparse_embedding |
64 | 65 |
|
65 | 66 |
|
66 | | -def test_init_with_legacy_fields(): |
67 | | - doc = Document( |
68 | | - content="test text", |
69 | | - content_type="text", |
70 | | - id_hash_keys=["content"], |
71 | | - dataframe="placeholder", |
72 | | - score=0.812, |
73 | | - embedding=[0.1, 0.2, 0.3], # type: ignore |
74 | | - ) |
75 | | - assert doc.id == "18fc2c114825872321cf5009827ca162f54d3be50ab9e9ffa027824b6ec223af" |
76 | | - assert doc.content == "test text" |
77 | | - assert doc.blob is None |
78 | | - assert doc.meta == {} |
79 | | - assert doc.score == 0.812 |
80 | | - assert doc.embedding == [0.1, 0.2, 0.3] |
81 | | - assert doc.sparse_embedding is None |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "legacy_fields,meta,expected_id", |
| 69 | + [ |
| 70 | + ( |
| 71 | + {"content_type": "text", "id_hash_keys": ["content"], "dataframe": "placeholder"}, |
| 72 | + {}, |
| 73 | + "18fc2c114825872321cf5009827ca162f54d3be50ab9e9ffa027824b6ec223af", |
| 74 | + ), |
| 75 | + ( |
| 76 | + {"content_type": "text", "id_hash_keys": ["content"]}, |
| 77 | + {"date": "10-10-2023", "type": "article"}, |
| 78 | + "dcd4914f727544e89ce8082f6f2e298d244dd0803a4dc167f19d24e7d43b28ac", |
| 79 | + ), |
| 80 | + ], |
| 81 | +) |
| 82 | +def test_init_with_legacy_fields(legacy_fields, meta, expected_id): |
| 83 | + doc = Document(content="test text", score=0.812, embedding=[0.1, 0.2, 0.3], meta=meta, **legacy_fields) |
82 | 84 |
|
| 85 | + # legacy fields are dropped, so the Document is the same as if they were not passed at all |
| 86 | + assert doc == Document(content="test text", score=0.812, embedding=[0.1, 0.2, 0.3], meta=meta) |
| 87 | + assert doc.id == expected_id |
83 | 88 | assert doc.content_type == "text" # this is a property now |
84 | | - |
85 | 89 | assert not hasattr(doc, "id_hash_keys") |
86 | 90 | assert not hasattr(doc, "dataframe") |
87 | 91 |
|
88 | 92 |
|
89 | | -def test_init_with_legacy_field(): |
90 | | - doc = Document( |
91 | | - content="test text", |
92 | | - content_type="text", # type: ignore |
93 | | - id_hash_keys=["content"], |
94 | | - score=0.812, |
95 | | - embedding=[0.1, 0.2, 0.3], |
96 | | - meta={"date": "10-10-2023", "type": "article"}, |
97 | | - ) |
98 | | - assert doc.id == "dcd4914f727544e89ce8082f6f2e298d244dd0803a4dc167f19d24e7d43b28ac" |
99 | | - assert doc.content == "test text" |
100 | | - assert doc.meta == {"date": "10-10-2023", "type": "article"} |
101 | | - assert doc.score == 0.812 |
102 | | - assert doc.embedding == [0.1, 0.2, 0.3] |
103 | | - assert doc.sparse_embedding is None |
| 93 | +@pytest.mark.parametrize("content", [123, 0, []]) |
| 94 | +def test_init_with_non_string_content(content): |
| 95 | + with pytest.raises(ValueError, match="must be a string or None"): |
| 96 | + Document(content=content) |
104 | 97 |
|
105 | | - assert doc.content_type == "text" # this is a property now |
106 | | - assert not hasattr(doc, "id_hash_keys") |
| 98 | + with pytest.raises(ValueError, match="must be a string or None"): |
| 99 | + Document("", content) |
| 100 | + |
| 101 | + |
| 102 | +def test_init_with_numpy_embedding(): |
| 103 | + expected = Document(content="test text", embedding=[0.1, 0.2, 0.3]) |
| 104 | + |
| 105 | + assert Document(content="test text", embedding=numpy.array([0.1, 0.2, 0.3])) == expected # type: ignore[arg-type] |
| 106 | + |
| 107 | + assert Document("", "test text", None, {}, None, numpy.array([0.1, 0.2, 0.3])) == expected # type: ignore[arg-type] |
107 | 108 |
|
108 | 109 |
|
109 | 110 | def test_basic_equality_type_mismatch(): |
|
0 commit comments