|
| 1 | +package datastore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSummariesDAO_SaveAndGet(t *testing.T) { |
| 14 | + if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok { |
| 15 | + t.Skip("ENABLE_MONGO_TESTS env variable is not set") |
| 16 | + } |
| 17 | + |
| 18 | + mdb, err := New("mongodb://localhost:27017", "test_ureadability", 0) |
| 19 | + require.NoError(t, err) |
| 20 | + |
| 21 | + // Create a unique collection for this test to avoid conflicts |
| 22 | + collection := mdb.client.Database(mdb.dbName).Collection("summaries_test") |
| 23 | + defer func() { |
| 24 | + _ = collection.Drop(context.Background()) |
| 25 | + }() |
| 26 | + |
| 27 | + dao := SummariesDAO{Collection: collection} |
| 28 | + |
| 29 | + content := "This is a test article content. It should generate a unique hash." |
| 30 | + summary := Summary{ |
| 31 | + Content: content, |
| 32 | + Summary: "This is a test summary of the article.", |
| 33 | + Model: "gpt-4o-mini", |
| 34 | + CreatedAt: time.Now(), |
| 35 | + } |
| 36 | + |
| 37 | + // Test saving a summary |
| 38 | + err = dao.Save(context.Background(), summary) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + // Test getting the summary |
| 42 | + foundSummary, found := dao.Get(context.Background(), content) |
| 43 | + assert.True(t, found) |
| 44 | + assert.Equal(t, summary.Summary, foundSummary.Summary) |
| 45 | + assert.Equal(t, summary.Model, foundSummary.Model) |
| 46 | + assert.NotEmpty(t, foundSummary.ID) |
| 47 | + |
| 48 | + // Test getting a non-existent summary |
| 49 | + _, found = dao.Get(context.Background(), "non-existent content") |
| 50 | + assert.False(t, found) |
| 51 | + |
| 52 | + // Test updating an existing summary |
| 53 | + updatedSummary := Summary{ |
| 54 | + ID: foundSummary.ID, |
| 55 | + Content: content, |
| 56 | + Summary: "This is an updated summary.", |
| 57 | + Model: "gpt-4o-mini", |
| 58 | + CreatedAt: foundSummary.CreatedAt, |
| 59 | + } |
| 60 | + |
| 61 | + err = dao.Save(context.Background(), updatedSummary) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + foundSummary, found = dao.Get(context.Background(), content) |
| 65 | + assert.True(t, found) |
| 66 | + assert.Equal(t, "This is an updated summary.", foundSummary.Summary) |
| 67 | + assert.Equal(t, updatedSummary.CreatedAt, foundSummary.CreatedAt) |
| 68 | + assert.NotEqual(t, updatedSummary.UpdatedAt, foundSummary.UpdatedAt) // UpdatedAt should be set by the DAO |
| 69 | + |
| 70 | + // Test deleting a summary |
| 71 | + err = dao.Delete(context.Background(), foundSummary.ID) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + _, found = dao.Get(context.Background(), content) |
| 75 | + assert.False(t, found) |
| 76 | +} |
| 77 | + |
| 78 | +func TestGenerateContentHash(t *testing.T) { |
| 79 | + content1 := "This is a test content." |
| 80 | + content2 := "This is a different test content." |
| 81 | + |
| 82 | + hash1 := generateContentHash(content1) |
| 83 | + hash2 := generateContentHash(content2) |
| 84 | + |
| 85 | + assert.NotEqual(t, hash1, hash2) |
| 86 | + assert.Equal(t, hash1, generateContentHash(content1)) // Same content should produce same hash |
| 87 | + assert.Equal(t, 64, len(hash1)) // SHA-256 produces 64 character hex string |
| 88 | +} |
0 commit comments