git ssb

0+

Piet / ssb-loomio



Commit b44b7ffbf0a06ad5062d72c5cc462eeb13bb03cc

Make all the tests pass.

Piet Geursen committed on 4/11/2018, 2:46:14 AM
Parent: d6df270592b877a672af61b40638876aeba4c5a4

Files changed

test/results/sync/buildResults.test.jschanged
test/results/sync/buildResults.test.jsView
@@ -1,7 +1,6 @@
11 const test = require('tape')
22 const pull = require('pull-stream')
3-const pullAsync = require('pull-async')
43 const Server = require('../../../lib/testServer')
54 const server = Server()
65 const ChooseOne = require('../../../position/async/buildChooseOne')(server)
76 const ChooseOnePoll = require('../../../poll/sync/buildChooseOne')
@@ -26,8 +25,10 @@
2625 title: 'how many food',
2726 closesAt: now
2827 }
2928
29+test.onFinish(() => server.close())
30+
3031 test('ChooseOneResults - ChooseOneResults', function (t) {
3132 PublishChooseOnePoll(validPoll, function (err, poll) {
3233 t.error(err)
3334
@@ -85,130 +86,158 @@
8586 })
8687 })
8788
8889 test('ChooseOneResults - a position stated for an invalid choice index is not counted', function (t) {
89- const positions = [
90- { value: { content: {choice: 3, poll}, author: pietId } }
91- ]
90+ PublishChooseOnePoll(validPoll, function (err, poll) {
91+ t.error(err)
92+ const positions = [
93+ { value: { content: {choice: 2, poll}, author: pietId }, key: '%dfkjdf' }
94+ ]
9295
93- pull(
94- pull.values(positions),
95- pull.asyncMap((fullPosition, cb) => {
96- ChooseOne(fullPosition.value.content, (err, position) => {
97- fullPosition.value.content = position
98- cb(err, fullPosition)
96+ pull(
97+ pull.values(positions),
98+ pull.asyncMap((fullPosition, cb) => {
99+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
100+ ChooseOne(positionAndPoll, (err, position) => {
101+ t.error(err)
102+ position.details.choice = 3
103+ fullPosition.value.content = position
104+ cb(err, fullPosition)
105+ })
106+ }),
107+ pull.collect(postions => {
108+ const actual = chooseOneResults({positions, poll})
109+ t.false(actual.results[3], 'invalid vote is not counted')
110+ t.end()
99111 })
100- }),
101- pull.collect(postions => {
102- const actual = chooseOneResults({positions, poll: validPoll})
103- t.false(actual.results[3], 'invalid vote is not counted')
104- t.end()
105- })
106- )
112+ )
113+ })
107114 })
108115
109116 test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) {
110- const positions = [
111- { value: { content: {choice: 3, poll}, author: pietId } }
112- ]
117+ PublishChooseOnePoll(validPoll, function (err, poll) {
118+ t.error(err)
119+ const positions = [
120+ { value: { content: {choice: 2, poll}, author: pietId }, key: '%fdfslkjdf' }
121+ ]
113122
114- pull(
115- pull.values(positions),
116- pull.asyncMap((fullPosition, cb) => {
117- ChooseOne(fullPosition.value.content, (err, position) => {
118- fullPosition.value.content = position
119- cb(err, fullPosition)
123+ pull(
124+ pull.values(positions),
125+ pull.asyncMap((fullPosition, cb) => {
126+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
127+ ChooseOne(positionAndPoll, (err, position) => {
128+ t.error(err)
129+ position.details.choice = 3
130+ fullPosition.value.content = position
131+ cb(err, fullPosition)
132+ })
133+ }),
134+ pull.collect(postions => {
135+ const actual = chooseOneResults({positions, poll})
136+ t.deepEqual(actual.errors[0].type, ERROR_POSITION_CHOICE, 'invalid vote is on error object')
137+ t.end()
120138 })
121- }),
122- pull.collect(postions => {
123- const actual = chooseOneResults({positions, poll: validPoll})
124- t.deepEqual(actual.errors[0].type, ERROR_POSITION_CHOICE, 'invalid vote is on error object')
125- t.end()
126- })
127- )
139+ )
140+ })
128141 })
129142
130143 test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) {
131- const positions = [
132- { value: { content: {choice: 0, poll}, author: pietId, timestamp: now - 1 } }
133- ]
144+ PublishChooseOnePoll(validPoll, function (err, poll) {
145+ t.error(err)
146+ const positions = [
147+ { value: { content: {choice: 0, poll}, author: pietId, timestamp: now - 1 }, key: '%dfkjsdlkjf'}
148+ ]
134149
135- pull(
136- pull.values(positions),
137- pull.asyncMap((fullPosition, cb) => {
138- ChooseOne(fullPosition.value.content, (err, position) => {
139- fullPosition.value.content = position
140- cb(err, fullPosition)
150+ pull(
151+ pull.values(positions),
152+ pull.asyncMap((fullPosition, cb) => {
153+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
154+ ChooseOne(positionAndPoll, (err, position) => {
155+ fullPosition.value.content = position
156+ cb(err, fullPosition)
157+ })
158+ }),
159+ pull.collect(postions => {
160+ const actual = chooseOneResults({positions, poll})
161+ t.ok(actual.results[0].voters[pietId], 'valid vote is counted')
162+ t.end()
141163 })
142- }),
143- pull.collect(postions => {
144- const actual = chooseOneResults({positions, poll: validPoll})
145- t.ok(actual.results[0].voters[pietId], 'valid vote is counted')
146- t.end()
147- })
148- )
164+ )
165+ })
149166 })
150167
151168 test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) {
152- const positions = [
153- { value: { content: {choice: 0, poll}, author: pietId, timestamp: now + 1 } }
154- ]
169+ PublishChooseOnePoll(validPoll, function (err, poll) {
170+ t.error(err)
171+ const positions = [
172+ { value: { content: {choice: 0, poll}, author: pietId, timestamp: now + 1 }, key: '%dfljsdkdj'}
173+ ]
155174
156- pull(
157- pull.values(positions),
158- pull.asyncMap((fullPosition, cb) => {
159- ChooseOne(fullPosition.value.content, (err, position) => {
160- fullPosition.value.content = position
161- cb(err, fullPosition)
175+ pull(
176+ pull.values(positions),
177+ pull.asyncMap((fullPosition, cb) => {
178+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
179+ ChooseOne(positionAndPoll, (err, position) => {
180+ fullPosition.value.content = position
181+ cb(err, fullPosition)
182+ })
183+ }),
184+ pull.collect(postions => {
185+ const actual = chooseOneResults({positions, poll})
186+ t.deepEqual(actual.results[0].voters, {}, 'invalid vote is not counted')
187+ t.end()
162188 })
163- }),
164- pull.collect(postions => {
165- const actual = chooseOneResults({positions, poll: validPoll})
166- t.deepEqual(actual.results[0].voters, {}, 'invalid vote is not counted')
167- t.end()
168- })
169- )
189+ )
190+ })
170191 })
171192
172193 test('ChooseOneResults - A position stated after the closing time of the poll is included in the error object', function (t) {
173- const positions = [
174- { value: { content: {choice: 0, poll}, author: pietId, timestamp: now + 1 } }
175- ]
194+ PublishChooseOnePoll(validPoll, function (err, poll) {
195+ t.error(err)
196+ const positions = [
197+ { value: { content: {choice: 0, poll}, author: pietId, timestamp: now + 1 }, key: '%dfrdkjfd' }
198+ ]
176199
177- pull(
178- pull.values(positions),
179- pull.asyncMap((fullPosition, cb) => {
180- ChooseOne(fullPosition.value.content, (err, position) => {
181- fullPosition.value.content = position
182- cb(err, fullPosition)
200+ pull(
201+ pull.values(positions),
202+ pull.asyncMap((fullPosition, cb) => {
203+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
204+ ChooseOne(positionAndPoll, (err, position) => {
205+ fullPosition.value.content = position
206+ cb(err, fullPosition)
207+ })
208+ }),
209+ pull.collect(postions => {
210+ const actual = chooseOneResults({positions, poll})
211+ t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object')
212+ t.end()
183213 })
184- }),
185- pull.collect(postions => {
186- const actual = chooseOneResults({positions, poll: validPoll})
187- t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object')
188- t.end()
189- })
190- )
214+ )
215+ })
191216 })
192217
193218 test('ChooseOneResults - ChooseOneResults only counts latest vote by an author', function (t) {
194- const positions = [
195- { value: { content: {choice: 2, poll}, author: pietId } },
196- { value: { content: {choice: 0, poll}, author: pietId } }
197- ]
219+ PublishChooseOnePoll(validPoll, function (err, poll) {
220+ t.error(err)
221+ const positions = [
222+ { value: { content: {choice: 2, poll}, author: pietId }, key: '%dfrdkjfd' },
223+ { value: { content: {choice: 0, poll}, author: pietId }, key: '%dfrdkjf3' }
224+ ]
198225
199- pull(
200- pull.values(positions),
201- pull.asyncMap((fullPosition, cb) => {
202- ChooseOne(fullPosition.value.content, (err, position) => {
203- fullPosition.value.content = position
204- cb(err, fullPosition)
226+ pull(
227+ pull.values(positions),
228+ pull.asyncMap((fullPosition, cb) => {
229+ var positionAndPoll = Object.assign({}, fullPosition.value.content, { poll })
230+ ChooseOne(positionAndPoll, (err, position) => {
231+ fullPosition.value.content = position
232+ cb(err, fullPosition)
233+ })
234+ }),
235+ pull.collect(postions => {
236+ const actual = chooseOneResults({positions, poll})
237+ t.false(actual.results[2].voters[pietId], 'old vote is deleted')
238+ t.true(actual.results[0].voters[pietId], 'new vote is counted')
239+ t.end()
205240 })
206- }),
207- pull.collect(postions => {
208- const actual = chooseOneResults({positions, poll: validPoll})
209- t.false(actual.results[2].voters[pietId], 'old vote is deleted')
210- t.true(actual.results[0].voters[pietId], 'new vote is counted')
211- t.end()
212- })
213- )
241+ )
242+ })
214243 })

Built with git-ssb-web