|
| 1 | +from datetime import datetime as dt |
| 2 | + |
| 3 | +import pytest |
| 4 | +from peewee import SqliteDatabase |
| 5 | + |
| 6 | +from modules.persistance import ( |
| 7 | + LibraryEntry, |
| 8 | + Transcript, |
| 9 | + Video, |
| 10 | + get_or_create_video, |
| 11 | + save_library_entry, |
| 12 | +) |
| 13 | + |
| 14 | +# Use an in-memory database for testing |
| 15 | +test_db = SqliteDatabase(":memory:") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def setup_test_db(): |
| 20 | + """Set up a test database before each test.""" |
| 21 | + # Bind models to test database |
| 22 | + test_db.bind([Video, Transcript, LibraryEntry]) |
| 23 | + test_db.connect() |
| 24 | + test_db.create_tables([Video, Transcript, LibraryEntry]) |
| 25 | + |
| 26 | + yield test_db |
| 27 | + |
| 28 | + # Clean up after test |
| 29 | + test_db.drop_tables([Video, Transcript, LibraryEntry]) |
| 30 | + test_db.close() |
| 31 | + |
| 32 | + |
| 33 | +def test_get_or_create_video_creates_new(setup_test_db): |
| 34 | + """Test that get_or_create_video creates a new video when it doesn't exist.""" |
| 35 | + yt_video_id = "test_video_123" |
| 36 | + link = "https://www.youtube.com/watch?v=test_video_123" |
| 37 | + title = "Test Video" |
| 38 | + channel = "Test Channel" |
| 39 | + saved_on = dt.now() |
| 40 | + |
| 41 | + video, created = get_or_create_video( |
| 42 | + yt_video_id=yt_video_id, |
| 43 | + link=link, |
| 44 | + title=title, |
| 45 | + channel=channel, |
| 46 | + saved_on=saved_on, |
| 47 | + ) |
| 48 | + |
| 49 | + assert created is True |
| 50 | + assert video.yt_video_id == yt_video_id |
| 51 | + assert video.title == title |
| 52 | + assert video.link == link |
| 53 | + assert video.channel == channel |
| 54 | + |
| 55 | + |
| 56 | +def test_get_or_create_video_gets_existing(setup_test_db): |
| 57 | + """Test that get_or_create_video returns existing video instead of creating duplicate.""" |
| 58 | + yt_video_id = "test_video_456" |
| 59 | + link = "https://www.youtube.com/watch?v=test_video_456" |
| 60 | + title = "Test Video 2" |
| 61 | + channel = "Test Channel 2" |
| 62 | + saved_on = dt.now() |
| 63 | + |
| 64 | + # Create video first time |
| 65 | + video1, created1 = get_or_create_video( |
| 66 | + yt_video_id=yt_video_id, |
| 67 | + link=link, |
| 68 | + title=title, |
| 69 | + channel=channel, |
| 70 | + saved_on=saved_on, |
| 71 | + ) |
| 72 | + |
| 73 | + assert created1 is True |
| 74 | + |
| 75 | + # Try to create same video again |
| 76 | + video2, created2 = get_or_create_video( |
| 77 | + yt_video_id=yt_video_id, |
| 78 | + link=link, |
| 79 | + title=title, |
| 80 | + channel=channel, |
| 81 | + saved_on=saved_on, |
| 82 | + ) |
| 83 | + |
| 84 | + assert created2 is False |
| 85 | + assert video1.id == video2.id |
| 86 | + assert video1.yt_video_id == video2.yt_video_id |
| 87 | + |
| 88 | + |
| 89 | +def test_save_summary_with_existing_video(setup_test_db): |
| 90 | + """Test that saving a summary works when video already exists.""" |
| 91 | + yt_video_id = "test_video_789" |
| 92 | + link = "https://www.youtube.com/watch?v=test_video_789" |
| 93 | + title = "Test Video 3" |
| 94 | + channel = "Test Channel 3" |
| 95 | + saved_on = dt.now() |
| 96 | + |
| 97 | + # Create video first (simulating summary.py saving a video) |
| 98 | + video, created = get_or_create_video( |
| 99 | + yt_video_id=yt_video_id, |
| 100 | + link=link, |
| 101 | + title=title, |
| 102 | + channel=channel, |
| 103 | + saved_on=saved_on, |
| 104 | + ) |
| 105 | + |
| 106 | + # Save a summary |
| 107 | + save_library_entry( |
| 108 | + entry_type="S", |
| 109 | + question_text=None, |
| 110 | + response_text="This is a test summary", |
| 111 | + video=video, |
| 112 | + ) |
| 113 | + |
| 114 | + # Now try to get the same video again (simulating chat.py) |
| 115 | + video2, created2 = get_or_create_video( |
| 116 | + yt_video_id=yt_video_id, |
| 117 | + link=link, |
| 118 | + title=title, |
| 119 | + channel=channel, |
| 120 | + saved_on=saved_on, |
| 121 | + ) |
| 122 | + |
| 123 | + # Should not create a new video |
| 124 | + assert created2 is False |
| 125 | + assert video.id == video2.id |
| 126 | + |
| 127 | + # Verify we can create a transcript for this video |
| 128 | + transcript = Transcript.create( |
| 129 | + video=video2, |
| 130 | + original_token_num=1000, |
| 131 | + ) |
| 132 | + |
| 133 | + assert transcript.video.id == video.id |
0 commit comments