summaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-11-28 17:55:37 -0500
committersiddharth <s@ricketyspace.net>2021-11-28 17:55:37 -0500
commit0206d6e63427c168fe6f0fcdf2bee17e964a3d65 (patch)
tree39b4c42dbeda627d225a95b77db636dbc61b09b6 /db
parent0ee116a416bf1868ed6ed3e7c040b24e13fdb549 (diff)
db: add FernDB.Add
Diffstat (limited to 'db')
-rw-r--r--db/db.go14
-rw-r--r--db/db_test.go66
2 files changed, 80 insertions, 0 deletions
diff --git a/db/db.go b/db/db.go
index 6dfda22..bbe5b36 100644
--- a/db/db.go
+++ b/db/db.go
@@ -82,3 +82,17 @@ func (fdb *FernDB) Exists(feed, entry string) bool {
}
+func (fdb *FernDB) Add(feed, entry string) {
+ // Check if entry already exist for feed.
+ if fdb.Exists(feed, entry) {
+ return
+ }
+
+ // Add entry.
+ fdb.mutex.Lock()
+ if _, ok := fdb.downloaded[feed]; !ok {
+ fdb.downloaded[feed] = make([]string, 0)
+ }
+ fdb.downloaded[feed] = append(fdb.downloaded[feed], entry)
+ fdb.mutex.Unlock()
+}
diff --git a/db/db_test.go b/db/db_test.go
index 9a8642a..8fc9fed 100644
--- a/db/db_test.go
+++ b/db/db_test.go
@@ -182,3 +182,69 @@ func TestExists(t *testing.T) {
return
}
}
+
+func TestAdd(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"]}`)
+ 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 `Add` for an existing feed.
+ db.Add("npr", "julian-baker")
+ if len(db.downloaded["npr"]) != 3 {
+ t.Errorf("db.Add failed: expected 3 entries for 'npr'")
+ return
+ }
+ if !db.Exists("npr", "julian-baker") {
+ t.Errorf("db.Add failed: expected %s in 'npr' feed",
+ "julian-baker")
+ return
+ }
+ db.Add("npr", "julian-baker")
+ if len(db.downloaded["npr"]) != 3 {
+ t.Errorf("db.Add failed: expected 3 entries for 'npr'")
+ return
+ }
+
+ // Test `Add` for nonexistent feed.
+ db.Add("mark-rober", "glitter-bomb")
+ if len(db.downloaded["mark-rober"]) != 1 {
+ t.Errorf("db.Add failed: expected 1 entry for 'mark-rober'")
+ return
+ }
+ if !db.Exists("mark-rober", "glitter-bomb") {
+ t.Errorf("db.Add failed: expected %s in 'mark-rober' feed",
+ "glitter-bomb")
+ return
+ }
+ db.Add("mark-rober", "squirrel-maze")
+ if len(db.downloaded["mark-rober"]) != 2 {
+ t.Errorf("db.Add failed: expected 2 entries for 'mark-rober'")
+ return
+ }
+ if !db.Exists("mark-rober", "squirrel-maze") {
+ t.Errorf("db.Add failed: expected %s in 'mark-rober' feed",
+ "squirrel-maze")
+ return
+ }
+}