package repository_test import ( "context" "testing" "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" ) func TestSourceCreate(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) rows, err := r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) if err != nil { t.Log(err) t.FailNow() } if rows != 1 { t.Log("failed to create a record, why") t.FailNow() } } func TestSourceGetById(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, err = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) if err != nil { t.Log(err) t.FailNow() } item, err := r.GetById(ctx, 1) if err != nil { t.Log(err) t.FailNow() } if item.ID != 1 { t.Log("got no record or the wrong one") t.FailNow() } } func TestSourceGetByDisplayName(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, err = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) if err != nil { t.Log(err) t.FailNow() } item, err := r.GetByDisplayName(ctx, "Test") if err != nil { t.Log(err) t.FailNow() } if item.DisplayName != "Test" { t.Log("got no record or the wrong one") t.FailNow() } } func TestSourceGetBySource(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, err = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) if err != nil { t.Log(err) t.FailNow() } item, err := r.GetBySource(ctx, domain.SourceCollectorRss) if err != nil { t.Log(err) t.FailNow() } if item.Source != domain.SourceCollectorRss { t.Log("got no record or the wrong one") t.FailNow() } } func TestSourceList(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) items, err := r.List(ctx, 0, 4) if err != nil { t.Log(err) t.FailNow() } if len(items ) != 4 { t.Log("something bad happened here") t.FailNow() } } func TestSourceListBySource(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) items, err := r.ListBySource(ctx, 0, 4, domain.SourceCollectorRss) if err != nil { t.Log(err) t.FailNow() } if len(items ) != 4 { t.Log("something bad happened here") t.FailNow() } } func TestSourcesEnableRecord(t *testing.T) { // This depends on the seed migration db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", false) item, err := r.GetByDisplayName(ctx, "Test") if err != nil { t.Log(err) t.FailNow() } if item.Enabled != false { t.Log("the initial record was created wrong") t.FailNow() } _, err = r.Enable(ctx, item.ID) if err != nil { t.Log(err) t.FailNow() } updated, err := r.GetById(ctx, item.ID) if err != nil { t.Log(err) t.FailNow() } if item.Enabled == updated.Enabled { t.Log("failed to update the enabled value") t.FailNow() } } func TestSourcesDisableRecord(t *testing.T) { db, err := setupInMemoryDb() if err != nil { t.Log(err) t.FailNow() } defer db.Close() ctx := context.Background() r := repository.NewSourceRepository(db) _, _ = r.Create(ctx, domain.SourceCollectorRss, "Test", "www.badurl.com", "rss, badurl", true) item, err := r.GetByDisplayName(ctx, "Test") if err != nil { t.Log(err) t.FailNow() } if item.Enabled != true { t.Log("the initial record was created wrong") t.FailNow() } _, err = r.Disable(ctx, 1) if err != nil { t.Log(err) t.FailNow() } updated, err := r.GetById(ctx, 1) if err != nil { t.Log(err) t.FailNow() } if item.Enabled == updated.Enabled { t.Log("failed to update the enabled value") t.FailNow() } }