import pytest import numpy as np from app.scorer import cosine_similarity, DimensionalityMismatchError, ZeroVectorError, EmptyInputError @pytest.fixture def valid_input(): query_vector = np.array([[1, 0]]) corpus_vectors = np.array([[1, 0], [0, 1], [1, 1]]) return query_vector, corpus_vectors @pytest.fixture def zero_query_vector(): query_vector = np.array([[0, 0]]) corpus_vectors = np.array([[1, 0], [0, 1]]) return query_vector, corpus_vectors @pytest.fixture def corpus_with_zero_vector(): query_vector = np.array([[1, 1]]) corpus_vectors = np.array([[1, 0], [0, 1], [0, 0]]) return query_vector, corpus_vectors @pytest.fixture def dimensionality_mismatch(): query_vector = np.array([[1, 0]]) corpus_vectors = np.array([[1, 0, 0], [0, 1, 0]]) return query_vector, corpus_vectors @pytest.fixture def empty_input(): query_vector = np.array([[]]) corpus_vectors = np.array([[]]) return query_vector, corpus_vectors @pytest.mark.unit def test_cosine_similarity_valid_input(valid_input): query_vector, corpus_vectors = valid_input similarities = cosine_similarity(query_vector, corpus_vectors) assert isinstance(similarities, np.ndarray) assert similarities.shape == (3,) assert similarities[0] == pytest.approx(1.0) # Same direction assert similarities[1] == pytest.approx(0.0) # Orthogonal assert similarities[2] == pytest.approx(1 / np.sqrt(2)) # Diagonal similarity @pytest.mark.unit def test_cosine_similarity_zero_query_vector(zero_query_vector): query_vector, corpus_vectors = zero_query_vector with pytest.raises(ZeroVectorError): cosine_similarity(query_vector, corpus_vectors) @pytest.mark.unit def test_cosine_similarity_corpus_with_zero_vector(corpus_with_zero_vector): query_vector, corpus_vectors = corpus_with_zero_vector with pytest.raises(ZeroVectorError): cosine_similarity(query_vector, corpus_vectors) @pytest.mark.unit def test_cosine_similarity_dimensionality_mismatch(dimensionality_mismatch): query_vector, corpus_vectors = dimensionality_mismatch with pytest.raises(DimensionalityMismatchError): cosine_similarity(query_vector, corpus_vectors) @pytest.mark.unit def test_cosine_similarity_empty_inputs(empty_input): query_vector, corpus_vectors = empty_input with pytest.raises(EmptyInputError): cosine_similarity(query_vector, corpus_vectors) @pytest.mark.integration def test_cosine_similarity_output_range(valid_input): query_vector, corpus_vectors = valid_input similarities = cosine_similarity(query_vector, corpus_vectors) assert np.all(similarities >= -1) assert np.all(similarities <= 1)