-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(document-search): change the type in from_source method to S…
…ource (#156)
- Loading branch information
1 parent
f07ae21
commit a60cdfe
Showing
6 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/ragbits-document-search/tests/unit/test_source_discriminator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from pathlib import Path | ||
from typing import Annotated | ||
|
||
import pydantic | ||
import pytest | ||
|
||
from ragbits.document_search.documents.sources import LocalFileSource, Source, SourceDiscriminator | ||
|
||
|
||
class ModelWithSource(pydantic.BaseModel): | ||
source: Annotated[Source, SourceDiscriminator()] | ||
foo: str | ||
|
||
|
||
def test_serialization(): | ||
source = LocalFileSource(path=Path("test")) | ||
model = ModelWithSource(source=source, foo="bar") | ||
assert model.model_dump() == { | ||
"source": { | ||
"source_type": "local_file_source", | ||
"path": Path("test"), | ||
}, | ||
"foo": "bar", | ||
} | ||
assert model.model_dump_json() == '{"source":{"path":"test","source_type":"local_file_source"},"foo":"bar"}' | ||
|
||
|
||
def test_deserialization_from_json(): | ||
json = '{"source":{"path":"test","source_type":"local_file_source"},"foo":"bar"}' | ||
model = ModelWithSource.model_validate_json(json) | ||
assert isinstance(model.source, LocalFileSource) | ||
assert model.source.path == Path("test") | ||
assert model.foo == "bar" | ||
|
||
|
||
def test_deserialization_from_dict(): | ||
dict = { | ||
"source": { | ||
"source_type": "local_file_source", | ||
"path": Path("test"), | ||
}, | ||
"foo": "bar", | ||
} | ||
model = ModelWithSource.model_validate(dict) | ||
assert isinstance(model.source, LocalFileSource) | ||
assert model.source.path == Path("test") | ||
assert model.foo == "bar" | ||
|
||
|
||
def test_deserialization_from_dict_with_invalid_source(): | ||
dict = { | ||
"source": { | ||
"source_type": "invalid_source", | ||
"path": Path("test"), | ||
}, | ||
"foo": "bar", | ||
} | ||
with pytest.raises(pydantic.ValidationError) as e: | ||
ModelWithSource.model_validate(dict) | ||
assert e.match("source") | ||
|
||
|
||
def test_deserialization_from_dict_with_missing_source_type(): | ||
dict = { | ||
"source": { | ||
"path": Path("test"), | ||
}, | ||
"foo": "bar", | ||
} | ||
with pytest.raises(pydantic.ValidationError) as e: | ||
ModelWithSource.model_validate(dict) | ||
assert e.match("source") |