Skip to content
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

feat: fromFile function add #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

maddhruv
Copy link
Member

@maddhruv maddhruv commented May 4, 2018

Function to add entries from a JSON file

Copy link
Member

@zeke zeke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I made a few suggestions, and let's get some test coverage on it.

index.js Outdated
@@ -29,6 +29,17 @@ class Glossary {
this._entries[term] = description
}

fromFile (filename) {
let rawdata = fs.readFileSync(filename)
let glossary = JSON.parse(rawdata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're only supporting JSON, this could just a be a require instead.

index.js Outdated
let rawdata = fs.readFileSync(filename)
let glossary = JSON.parse(rawdata)
for (var i in glossary) {
assert(i && i.length, `term is required for description ${glossary[i]}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than re-doing this validation, we should just use the existing .add() method and get the existing validation for free.

readme.md Outdated
### `glossary.fromFile(filename)`

Uploads entries from the a file provided. Entries only exist in memory until you
call `glossary.upload()` but file remains unchanged.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"but file remains unchanged." doesn't seem necessary.

readme.md Outdated
Uploads entries from the a file provided. Entries only exist in memory until you
call `glossary.upload()` but file remains unchanged.

- `filename` String (required)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to mention that this should be a full path.

@zeke
Copy link
Member

zeke commented May 15, 2018

Hey @maddhruv wanna take another look at this when you get a chance?

@maddhruv
Copy link
Member Author

@zeke I would surely make relevant changes today

@maddhruv
Copy link
Member Author

@zeke have a look at the changes made

@zeke
Copy link
Member

zeke commented May 17, 2018

Looking good. Can you add some tests for this new functionality?

@maddhruv
Copy link
Member Author

the build is failing as I have referred a non-existing dummy file

so should I also add the dummy file in the repo?

@zeke
Copy link
Member

zeke commented May 17, 2018

should I also add the dummy file in the repo?

Yeah this is typically referred to as a "test fixture". It can be included in the repo itself in a file like test/fixture.json or you could create the file dynamically during the test and remove it after the test is finished.

@zeke
Copy link
Member

zeke commented May 17, 2018

That's a good start. You'll also want to test the "happy path" scenario, where the API works as expected.

Try using this command to help ensure you have good test coverage:

jest --watch --notify --notifyMode=change --coverage

(We should probably also add this command to the npm scripts for convenience)

@maddhruv
Copy link
Member Author

I am poor at testing!
Haven't implemented them in codes before, honestly!
Will check that too

@maddhruv
Copy link
Member Author

maddhruv commented Jun 1, 2018

@zeke can this be merged now?

screenshot from 2018-06-01 09-17-31

@zeke
Copy link
Member

zeke commented Jun 1, 2018

You'll also want to test the "happy path" scenario, where the API works as expected.

☝️ @maddhruv I'd love to see this implemented before we land it.

@maddhruv
Copy link
Member Author

maddhruv commented Jun 9, 2018

You'll also want to test the "happy path" scenario, where the API works as expected.
I do not get ti 😕

@zeke
Copy link
Member

zeke commented Jun 9, 2018

You have a test that verifies the behavior when the "wrong thing" happens (empty file), but no test that verifies when the "right thing" happens (non-empty file). This is sometimes called the "happy path", meaning everything works as expected. You should add a test that has a non-empty JSON file and verify that the fromFile function behaves as expected when loading it.

@zeke zeke changed the title fromFile function add feat: fromFile function add Jun 9, 2018
@maddhruv
Copy link
Member Author

😸

assert(term, 'Term or description not found')
this.add(term[0], term[1])
})
this._entries = glossary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._entries = glossary is not necessary. This line can be removed.

let glossary = require(filename)
assert(Object.keys(glossary).length, 'The file seems to be empty')
glossary.forEach(term => {
assert(term, 'Term or description not found')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert is not necessary. This is already handled by the add method.

@@ -0,0 +1,3 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs and the implementation expect an array of arrays, not an object.

@@ -0,0 +1,4 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be an array, not an object.

})

test('upload from a file', () => {
expect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like:

glossary.fromFile('./test/glossary_test.json')
expect(glossary.entries.length).toEq(2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants