File size: 1,359 Bytes
1f22b14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np
import pandas as pd

from buster.docparser import generate_embeddings, read_documents, write_documents


def test_generate_embeddings(tmp_path, monkeypatch):
    # Patch the get_embedding function to return a fixed embedding
    monkeypatch.setattr("buster.docparser.get_embedding", lambda x, engine: [-0.005, 0.0018])

    # Create fake data
    data = pd.DataFrame.from_dict({"title": ["test"], "url": ["http://url.com"], "content": ["cool text"]})

    # Write the data to a file
    filepath = tmp_path / "test_document.csv"
    write_documents(filepath=filepath, documents_df=data, source="test")

    # Generate embeddings, store in a file
    output_file = tmp_path / "test_document_embeddings.tar.gz"
    df = generate_embeddings(filepath=filepath, output_file=output_file, source="test")

    # Read the embeddings from the file
    read_df = read_documents(output_file, "test")

    # Check all the values are correct across the files
    assert df["title"].iloc[0] == data["title"].iloc[0] == read_df["title"].iloc[0]
    assert df["url"].iloc[0] == data["url"].iloc[0] == read_df["url"].iloc[0]
    assert df["content"].iloc[0] == data["content"].iloc[0] == read_df["content"].iloc[0]
    assert np.allclose(df["embedding"].iloc[0], read_df["embedding"].iloc[0])
    assert df["n_tokens"].iloc[0] == read_df["n_tokens"].iloc[0]