-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests for logic/vote #26
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove all "..." texts on it. I think the other texts don't have that
Done! '...' Removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jarenal Thanks a lot for your code, it's really appreciated!
I've made some notes about your code.
Some things that I would recommend you:
- Use semi columns. There are no semi columns in the code.
- Avoid to make a function call for every variable that you need to get from the original file. Always try to cache locally as much data as you can :)
- Try to follow code patterns/stylings. I'm my opinion the whole code should look like it has been written only by only one person. E.g. I would avoid to use contexts if they have not been used it also before.
I'd also recommend you to setup prettier and use it with webstorm. It's an 💥 combination that will style your code in an awesome way.
tests/unit/logic/vote.spec.js
Outdated
|
||
describe('logger and schema should be setted properly into library', function () { | ||
it('should be an object', function () { | ||
expect(Vote.__get__('library').logger).to.equals(logger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could get library at the beginning of the "it" and check then in every expect what you need to expect.
In this way you your code will look a bit better and be a bit more performant :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
}) | ||
}) | ||
|
||
describe('when we call to...', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see here a different pattern in writing tests. I believe we should agree on something.
Roman's pattern (which is the one that I'm following) is to not wrap all the methods inside another wrapper that is inside another wrapper like in this case.
I'd reccomend to follow his pattern:
describe('logic/vote', function () {
describe('constructor', function () {
it('test', ..)
it('test', ..)
it('test', ..)
})
describe('method', function () {
it('test', ..)
it('test', ..)
it('test', ..)
})
describe('method2', function () {
it('test', ..)
it('test', ..)
it('test', ..)
})
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, Lets stick with the later pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
|
||
it('delegates, rounds and system should be setted properly into modules variable', function () { | ||
vote.bind(delegates, rounds, system) | ||
expect(Vote.__get__('modules').delegates).to.equals(delegates) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend to call get only once :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 👍 it also improves code readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
|
||
it('should return vote\'s fee', function () { | ||
vote.bind(delegates, rounds, system) | ||
var fee = vote.calculateFee(trs, sender, height) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend to stub calculateFees and check if it's called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm? you mean removing this test entirely? I would actually keep it @xunga
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No modifications were done
tests/unit/logic/vote.spec.js
Outdated
|
||
context('if trs.recipientId and trs.senderId are not equals', function () { | ||
|
||
beforeEach(function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more a question from my side. Do we really need a beforeEach and afterEach when we have only one test case? In my opinion I would put the logic directly into the it. Maybe this is not a correct approach, I would like to get an opinion on this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well test cases should only test
behavioural stuff. all init
and teardown
should be kept separate as much as you can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
context('if everything is ok', function () { | ||
it('should return an object with votes as property in Array format', function () { | ||
var result = vote.dbRead(raw_success) | ||
expect(result).to.have.deep.property('votes') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the next expect is enough, or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the second one for to verify that the values received are correct.
tests/unit/logic/vote.spec.js
Outdated
|
||
|
||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test of dbTable and dbFields are missing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that to test a static object is within the testing standards. What do you think guys?
tests/unit/logic/vote.spec.js
Outdated
trs = {id: 123, asset: {votes: [1, 2, 3]}} | ||
}) | ||
|
||
it('should return an object with a valid db schema', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say that expect(result).to.instanceof(Object)
is useless and I would also test what happens if trs.asset.votes
is not an array...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
beforeEach(function () { | ||
trs = {} | ||
trs_case_4 = {signatures: [1, 2, 3]} | ||
trs_case_4_b = {signatures: [1, 2]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same of before, no need of this code in here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
tests/unit/logic/vote.spec.js
Outdated
}) | ||
}) | ||
|
||
context('Fail 5: If v_votes is not a string', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on issue #31
tests/unit/logic/vote.spec.js
Outdated
|
||
it('delegates, rounds and system should be setted properly into modules variable', function () { | ||
vote.bind(delegates, rounds, system) | ||
expect(Vote.__get__('modules').delegates).to.equals(delegates) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 👍 it also improves code readability.
tests/unit/logic/vote.spec.js
Outdated
|
||
it('should return vote\'s fee', function () { | ||
vote.bind(delegates, rounds, system) | ||
var fee = vote.calculateFee(trs, sender, height) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm? you mean removing this test entirely? I would actually keep it @xunga
tests/unit/logic/vote.spec.js
Outdated
|
||
context('if trs.recipientId and trs.senderId are not equals', function () { | ||
|
||
beforeEach(function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well test cases should only test
behavioural stuff. all init
and teardown
should be kept separate as much as you can.
tests/unit/logic/vote.spec.js
Outdated
|
||
beforeEach(function () { | ||
clock = sinon.useFakeTimers() | ||
Vote.__set__('setImmediate', setImmediate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you use context instead of sub describe
while it's a safe guard we shouldn't misuse contexts especially if they're not really used.
Also it would be better to have one beforeEach
and afterEach
where you sinon.useFakeTimers
and also do other shared stuff between tests (like the setImmediate thing?)
Right now there is a lot of boilerplate involving the init/teardown of test groups.
tests/unit/logic/vote.spec.js
Outdated
clock.restore() | ||
}) | ||
|
||
it('should call to callback without errors', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
tests/unit/logic/vote.spec.js
Outdated
sender = {address: 123} | ||
block = {id: 123, height: 123} | ||
Vote.__set__('setImmediate', setImmediate) | ||
Vote.__set__('self.checkConfirmedDelegates', function (trs, cb) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
tests/unit/logic/vote.spec.js
Outdated
clock.restore() | ||
}) | ||
|
||
it('should call to callback', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test says should call callback
and tests that. If there's a need to check the parent.scope.account then another test (probably above this one) needs to be written
tests/unit/logic/vote.spec.js
Outdated
it('should call to scope.account.merge() and callback()', function () { | ||
clock.runAll() | ||
var result = Vote.__get__('self.scope') | ||
expect(result.account.merge.called).to.be.true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
Test ready for review! Some test will fail until I fix some little issues that I found in logic/vote