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(import): Add several features to productType json generator #47

Merged
merged 8 commits into from
Nov 22, 2016

Conversation

junajan
Copy link
Contributor

@junajan junajan commented Nov 10, 2016

This PR adds these features:

  • possibility to import xlsx file
  • possibility to specify an input encoding
  • possibility to specify a csv delimiter

@coveralls
Copy link

Coverage Status

Coverage increased (+3.5%) to 90.038% when pulling 68d9b49 on import-xlsx-encoding into 1499c1a on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.5%) to 90.038% when pulling dc4a160 on import-xlsx-encoding into 1499c1a on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.5%) to 90.038% when pulling 7569c08 on import-xlsx-encoding into e4bcd75 on master.

@hisabimbola
Copy link
Contributor

Instead of having all three in one PR, good if each were separate PRs, especially when they are independent.

@junajan
Copy link
Contributor Author

junajan commented Nov 11, 2016

@hisabimbola - yes I agree but in this case, I just took IO layer from another project and use it here with minor changes.

Copy link
Contributor

@hisabimbola hisabimbola left a comment

Choose a reason for hiding this comment

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

Dropped some comments about the PR

"jszip": "2.5.x",
"lodash": "^4.16.6",
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we using lodash and underscore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for a notice - I will unify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82

@@ -1,11 +1,15 @@
_ = require 'underscore'
Promise = require 'bluebird'
fs = Promise.promisifyAll require('fs')
Path = require 'path'
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for capitalising Path?

Class are the only Object in javascript that is capitalise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all requires (except fs) are capitalized there so I was following convention

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82

fileType = getFileType(path)

if not isSupportedFileType(fileType)
return Promise.reject(Error("File type #{fileType} is not supported. Use one of #{supportedFileTypes}"))
Copy link
Contributor

Choose a reason for hiding this comment

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

To use the Error object, I think you need to instantiate it with the new keyword

new Error()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82


stream.end()

createReader = (fileType, delimiter = ',', encoding = 'utf-8' ) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we factor out all this helper methods for test to a folder helper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82

expect(data[0].numb).to.equal('123')


it 'should not read csv file with unsupported encoding', ->
Copy link
Contributor

Choose a reason for hiding this comment

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

There are no assertions in this test. How do we validate this test and what it's trying to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can return a promise as a result of the function and mocha will check if the promise ends with an error. This test tries to create an instance of Reader with unsupported encoding which should throw an error.

Copy link
Contributor

Choose a reason for hiding this comment

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

and if it throw an error, the test should fail right?

I think this test can be rewritten.

Handle all promises and assert that it's fails as we expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check this update 25bdb72 if it is ok this way.

if not header
header = row
else
rows.push Reader.mapRow(header, row)
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for not using this to access mapRow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82

buffers.push buffer
stream.on 'error', (err) -> reject(err)
stream.on 'end', =>
buffer = Buffer.concat(buffers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Reason why I said I do not see the importance in streaming with this module. We saving all in memory here. Good as reading the file sync...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are loaded two files at the same time (attributes and product-type-attributes) so at least we save some time by reading them in parallel instead of synchronous version.

.then (workbook) ->
header = null
rows = []
worksheet = workbook.getWorksheet(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we hardcoding 1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was assuming that the worksheet in the xlsx file will be always the first one (as it is when exporting productTypes using product-type-export tool)

@@ -196,8 +196,22 @@ class ProductTypeGenerator
i18n = {}
languages = @_languages header, _.keys row
for language in languages
if row["#{header}.#{language}"].trim() isnt ''
i18n[language] = row["#{header}.#{language}"].trim()
# condition was commented out due to an error:
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not get this comment?

which error?

Meanwhile, we should not commit commented out code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was one issue that when you have a productType with LENUM attribute it exported first empty value like this:

"values": [
{
  "key": "",
  "label": {}
}

Which is wrong because you are missing a value in label and Sphere thus you will get an error: attributes -> type -> elementType -> values -> label: Values of LocalizedString must not be empty.
So I removed that if and now creates first empty value with language code:

"label": {
    "de": ""
}

Anyway - I removed that comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated here 9262a82

iconv.encode(row, 'win1250')
]

writeCsv(tempFile, dataInput)
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a race condition with this test. I think we want to use mocha async method(done) to write this.

Copy link
Contributor Author

@junajan junajan Nov 11, 2016

Choose a reason for hiding this comment

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

Mocha can work with promises too - so you don't have to use a done callback, it will try to use a promise returned from the function instead

Copy link
Contributor

Choose a reason for hiding this comment

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

Ach so.

Did not know, thanks.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.6%) to 90.189% when pulling 9262a82 on import-xlsx-encoding into e4bcd75 on master.

rename: (dest, matchedSrcPath) ->
dest + matchedSrcPath
dest + matchedSrcPath.replace('coffee', 'js')
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of doing this replace and removing ext parameter totally, what if we add the ext with .js only

ext: '.js'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hisabimbola I tried that but then it was renaming:

  • abcd.spec.coffee -> abcd.js
  • helper.coffee -> helper.js

Which did not start any tests because they did not have .spec.js suffix anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. Nice to hear...


read: (file) =>
debugLog "READER::stream file %s", file
@inputStream = fs.createReadStream file
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, no too much pain point... just we not utilizing the importance of the stream. That is too much for this PR.

Something we can do later, probably in a rewrite.


read: (file) =>
debugLog "READER::stream file %s", file
@inputStream = fs.createReadStream file
Copy link
Contributor

Choose a reason for hiding this comment

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

But this is fine for now.

iconv.encode(row, 'win1250')
]

writeCsv(tempFile, dataInput)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ach so.

Did not know, thanks.

expect(data[0].numb).to.equal('123')


it 'should not read csv file with unsupported encoding', ->
Copy link
Contributor

Choose a reason for hiding this comment

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

and if it throw an error, the test should fail right?

I think this test can be rewritten.

Handle all promises and assert that it's fails as we expect.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.6%) to 90.189% when pulling 25bdb72 on import-xlsx-encoding into e4bcd75 on master.

@junajan
Copy link
Contributor Author

junajan commented Nov 21, 2016

@hisabimbola - unsupported encoding test updated here 63d8cda

@coveralls
Copy link

Coverage Status

Coverage increased (+3.6%) to 90.189% when pulling 63d8cda on import-xlsx-encoding into e4bcd75 on master.

Copy link
Contributor

@hisabimbola hisabimbola left a comment

Choose a reason for hiding this comment

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

LGTM now.

@junajan junajan merged commit c4597aa into master Nov 22, 2016
@junajan junajan deleted the import-xlsx-encoding branch November 22, 2016 15:42
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

5 participants