Commit 74ba75f717d9bffcc0ea0b128ef9e0b0f02593c3
Better db and tests
Anders Rune Jensen committed on 10/18/2017, 9:40:15 PMParent: 193898c57c835343d8fd7b9e8b5c5ee87773a217
Files changed
db.js | changed |
test/write.js | changed |
db.js | ||
---|---|---|
@@ -41,23 +41,21 @@ | ||
41 | 41 … | var books = [] |
42 | 42 … | |
43 | 43 … | pull( |
44 | 44 … | 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 … | + }), | |
45 | 56 … | 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) | |
60 | 58 … | }) |
61 | 59 … | ) |
62 | 60 … | }, |
63 | 61 … | |
@@ -67,9 +65,9 @@ | ||
67 | 65 … | var sbot = this.sbot |
68 | 66 … | pull( |
69 | 67 … | sbot.links({ dest: book.key }), // live: true |
70 | 68 … | pull.filter(data => data.key), |
71 | - pull.asyncMap(function (data, cb) { | |
69 … | + pull.asyncMap((data, cb) => { | |
72 | 70 … | sbot.get(data.key, cb) |
73 | 71 … | }), |
74 | 72 … | pull.collect((err, msgs) => { // for live use drain |
75 | 73 … | if (err) throw err |
@@ -78,10 +76,9 @@ | ||
78 | 76 … | book.common = Object.assign(book.common, msg.content.common) |
79 | 77 … | book.subjective[msg.author] = Object.assign(book.subjective[msg.author], msg.content.subjective) |
80 | 78 … | }) |
81 | 79 … | |
82 | - // evil hack | |
83 | - cb([book]) | |
80 … | + cb(book) | |
84 | 81 … | }) |
85 | 82 … | ) |
86 | 83 … | }, |
87 | 84 … |
test/write.js | |||
---|---|---|---|
@@ -7,9 +7,9 @@ | |||
7 | 7 … | .use(require('scuttlebot/plugins/master')) | |
8 | 8 … | ||
9 | 9 … | var db = require('../db.js') | |
10 | 10 … | ||
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' } | ||
12 | 12 … | const subjective = { rating: 5, 'rating-type': 'stars', read: Date.now() } | |
13 | 13 … | ||
14 | 14 … | function getSbot() | |
15 | 15 … | { | |
@@ -40,16 +40,21 @@ | |||
40 | 40 … | var sbot = getSbot() | |
41 | 41 … | ||
42 | 42 … | var bookDB = db.bookDB(sbot) | |
43 | 43 … | 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 … | + }) | ||
52 | 57 … | }) | |
53 | 58 … | }) | |
54 | 59 … | }) | |
55 | 60 … | }) |
Built with git-ssb-web