git ssb

3+

arj / patchbook



Commit 74ba75f717d9bffcc0ea0b128ef9e0b0f02593c3

Better db and tests

Anders Rune Jensen committed on 10/18/2017, 9:40:15 PM
Parent: 193898c57c835343d8fd7b9e8b5c5ee87773a217

Files changed

db.jschanged
test/write.jschanged
db.jsView
@@ -41,23 +41,21 @@
4141 var books = []
4242
4343 pull(
4444 this.getStreamByType('bookclub'),
45 + pull.asyncMap((msg, cb) => {
46 + var book = {
47 + key: msg.key,
48 + common: msg.content.common,
49 + subjective: {}
50 + }
51 + book.subjective[msg.author] = msg.content.subjective
52 + books.push(book)
53 +
54 + this.applyAmends(book, cb)
55 + }),
4556 pull.collect((err, msgs) => {
46- if (err) throw err
47-
48- msgs.forEach(msg => {
49- var book = {
50- key: msg.key,
51- common: msg.content.common,
52- subjective: {}
53- }
54- book.subjective[msg.author] = msg.content.subjective
55-
56- books.push(book)
57-
58- this.applyAmends(book, cb)
59- })
57 + cb(books)
6058 })
6159 )
6260 },
6361
@@ -67,9 +65,9 @@
6765 var sbot = this.sbot
6866 pull(
6967 sbot.links({ dest: book.key }), // live: true
7068 pull.filter(data => data.key),
71- pull.asyncMap(function (data, cb) {
69 + pull.asyncMap((data, cb) => {
7270 sbot.get(data.key, cb)
7371 }),
7472 pull.collect((err, msgs) => { // for live use drain
7573 if (err) throw err
@@ -78,10 +76,9 @@
7876 book.common = Object.assign(book.common, msg.content.common)
7977 book.subjective[msg.author] = Object.assign(book.subjective[msg.author], msg.content.subjective)
8078 })
8179
82- // evil hack
83- cb([book])
80 + cb(book)
8481 })
8582 )
8683 },
8784
test/write.jsView
@@ -7,9 +7,9 @@
77 .use(require('scuttlebot/plugins/master'))
88
99 var db = require('../db.js')
1010
11-const common = { title: 'The moon is a harsh mistress', author: 'Robert A. Heinlein' }
11 +const common = { title: 'The moon is a harsh mistress', authors: 'Robert A. Heinlein' }
1212 const subjective = { rating: 5, 'rating-type': 'stars', read: Date.now() }
1313
1414 function getSbot()
1515 {
@@ -40,16 +40,21 @@
4040 var sbot = getSbot()
4141
4242 var bookDB = db.bookDB(sbot)
4343 bookDB.create(common, subjective, (err, msg) => {
44- const newSubj = { rating: 4 }
45- bookDB.amend(msg.key, null, newSubj, (err, msg) => {
46- bookDB.getAll(books => {
47- t.deepEqual(books[0].common, common, "book common amended correctly")
48- t.deepEqual(books[0].subjective[msg.value.author], Object.assign(subjective, newSubj),
49- "book subjective updated correctly")
50- t.end()
51- sbot.close()
44 + const newRating = { rating: 4 }
45 + bookDB.amend(msg.key, null, newRating, (err, msg) => {
46 + const newAuthor = { authors: "El gringo" }
47 + const readInfo = { shelves: "read", rating: 4.5, ratingType: "stars" }
48 + bookDB.amend(msg.key, newAuthor, readInfo, (err, msg) => {
49 + bookDB.getAll(books => {
50 + t.deepEqual(books[0].common, Object.assign(common, newAuthor), "book common amended correctly")
51 + t.deepEqual(books[0].subjective[msg.value.author],
52 + Object.assign(Object.assign(subjective, newRating), readInfo),
53 + "book subjective updated correctly")
54 + t.end()
55 + sbot.close()
56 + })
5257 })
5358 })
5459 })
5560 })

Built with git-ssb-web