diff options
author | siddharth <s@ricketyspace.net> | 2021-11-28 17:23:55 -0500 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2021-11-28 17:23:55 -0500 |
commit | 0ee116a416bf1868ed6ed3e7c040b24e13fdb549 (patch) | |
tree | 17d71cf76946d5a9c246c91b8cc10e8c5921354b /db | |
parent | 43dade27aa1cd5e77201c3100276f299e321d213 (diff) |
db: add FernDB.Exists
Diffstat (limited to 'db')
-rw-r--r-- | db/db.go | 14 | ||||
-rw-r--r-- | db/db_test.go | 43 |
2 files changed, 57 insertions, 0 deletions
@@ -68,3 +68,17 @@ func Open() (*FernDB, error) { } return db, nil } + +func (fdb *FernDB) Exists(feed, entry string) bool { + if _, ok := fdb.downloaded[feed]; !ok { + return false + } + for _, e := range fdb.downloaded[feed] { + if e == entry { + return true + } + } + return false + +} + diff --git a/db/db_test.go b/db/db_test.go index 37ec6ca..9a8642a 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -139,3 +139,46 @@ func TestOpenExistingDB(t *testing.T) { } } } + +func TestExists(t *testing.T) { + // Set custom path for db. + dbPath = path.Join(os.TempDir(), "fern-db.json") + defer os.Remove(dbPath) + + // Write a sample test db to fern-db.json + testDBJSON := []byte(`{"npr":["william-prince","joy-oladokun","lucy-ducas"]}`) + dbFile, err := os.Create(dbPath) + defer dbFile.Close() + if err != nil { + t.Errorf("Unable to create fern-db.json: %v", err.Error()) + return + } + n, err := dbFile.Write(testDBJSON) + if len(testDBJSON) != n { + t.Errorf("Write to fern-db.json failed: %v", err.Error()) + return + } + + // Open the db. + db, err := Open() + if err != nil { + t.Errorf("db.Open failed: %v", err.Error()) + return + } + + // Test Exists. + if db.Exists("mkbhd", "v-raptor") { + t.Errorf("db.Exists failed: mkbhd does not exist in db") + return + } + if db.Exists("npr", "julien-baker") { + t.Errorf("db.Exists failed: (%s, %s) does not exist in db", + "npr", "julien-baker") + return + } + if !db.Exists("npr", "joy-oladokun") { + t.Errorf("db.Exists failed: (%s, %s) exists in db", + "npr", "joy-oladokun") + return + } +} |