File size: 3,955 Bytes
cdb73a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# tests/test_recommender.py

import unittest

import numpy as np
import pandas as pd

import src.book_recommender.core.config as config  # Import config for EMBEDDING_DIMENSION
from src.book_recommender.ml.recommender import BookRecommender


class TestRecommender(unittest.TestCase):

    def setUp(self):
        # Create dummy data directly in memory
        self.book_data = pd.DataFrame(
            {
                "id": ["1", "2", "3", "4", "5"],
                "title": ["Alpha Book", "Beta Book", "Gamma Book", "Delta Book", "Epsilon Book"],
                "title_lower": ["alpha book", "beta book", "gamma book", "delta book", "epsilon book"],
                "authors": ["Auth1", "Auth2", "Auth3", "Auth1", "Auth5"],
                "authors_lower": ["auth1", "auth2", "auth3", "auth1", "auth5"],
                "description": ["Desc1", "Desc2", "Desc3", "Desc1", "Desc5"],
                "genres": ["Fiction", "Sci-Fi", "History", "Fiction", "Fantasy"],
                "tags": ["tag1", "tag2", "tag3", "tag4", "tag5"],
                "rating": [4.5, 4.0, 4.8, 4.3, 4.6],
                "combined_text": ["alpha", "beta", "gamma", "delta", "epsilon"],
            }
        )

        # Create dummy embeddings (simple, not from a real model)
        # Make 'alpha book' very similar to 'delta book'
        # Make 'beta book' very similar to 'epsilon book'
        self.embeddings = np.array(
            [
                [0.9, 0.1, 0.1, 0.1, 0.1],  # alpha
                [0.1, 0.9, 0.1, 0.1, 0.1],  # beta
                [0.1, 0.1, 0.9, 0.1, 0.1],  # gamma
                [0.85, 0.15, 0.1, 0.1, 0.1],  # delta (similar to alpha)
                [0.1, 0.8, 0.1, 0.1, 0.1],  # epsilon (similar to beta)
            ]
        )
        # Pad embeddings to match expected dimension if necessary
        if self.embeddings.shape[1] < config.EMBEDDING_DIMENSION:
            padding = np.zeros((self.embeddings.shape[0], config.EMBEDDING_DIMENSION - self.embeddings.shape[1]))
            self.embeddings = np.hstack((self.embeddings, padding))

        # Initialize the recommender with in-memory data
        self.recommender = BookRecommender(book_data=self.book_data, embeddings=self.embeddings)

    def test_get_recommendations(self):
        # Test with a book that exists
        recs = self.recommender.get_recommendations("alpha book", top_k=1, similarity_threshold=0.0)

        # 1. Should return 1 recommendation
        self.assertEqual(len(recs), 1)

        # 2. The input book itself should not be in the recommendations
        rec_titles = [r["title"] for r in recs]
        self.assertNotIn("alpha book", rec_titles)

        # 3. The most similar book ('delta book') should be first
        self.assertEqual(recs[0]["title"], "Delta Book")

        # 4. Check the structure of the output
        self.assertIsInstance(recs[0], dict)
        self.assertIn("title", recs[0])
        self.assertIn("authors", recs[0])
        self.assertIn("description", recs[0])
        self.assertIn("genres", recs[0])
        self.assertIn("tags", recs[0])
        self.assertIn("rating", recs[0])
        self.assertIn("similarity", recs[0])

    def test_get_recommendations_non_existent_title(self):
        # Test with a book title that does not exist
        recs = self.recommender.get_recommendations("non existent book")
        self.assertEqual(len(recs), 0)

    def test_recommender_accuracy(self):
        """
        Tests if the recommender returns expected similar books based on dummy embeddings.
        """
        # Test for 'beta book', expecting 'epsilon book' as the most similar
        recs = self.recommender.get_recommendations("beta book", top_k=1)
        self.assertEqual(len(recs), 1)
        self.assertEqual(recs[0]["title"], "Epsilon Book")
        self.assertTrue(recs[0]["similarity"] > 0.7)  # Expect a high similarity for a known similar book


if __name__ == "__main__":
    unittest.main()