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

727 into csv travis fix #843

Merged
merged 6 commits into from
Mar 22, 2017

Conversation

jrgoldfinemiddleton
Copy link
Contributor

@jrgoldfinemiddleton jrgoldfinemiddleton commented Mar 19, 2017

This PR should supersede #841 to fix #727 . The new tests do not cause errors on my local machine.

It seems that with commit af14329 only the Node.js 4 Travis build is failing for some reason.

@codecov-io
Copy link

codecov-io commented Mar 19, 2017

Codecov Report

Merging #843 into develop will increase coverage by 0.01%.
The diff coverage is 21.73%.

@@             Coverage Diff             @@
##           develop     #843      +/-   ##
===========================================
+ Coverage    67.71%   67.72%   +0.01%     
===========================================
  Files            1        1              
  Lines         9878     9879       +1     
  Branches      2947     2948       +1     
===========================================
+ Hits          6689     6691       +2     
+ Misses        3189     3188       -1
Impacted Files Coverage Δ
dist/alasql.fs.js 67.72% <21.73%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5daf1f3...72dc45d. Read the comment docs.

@jrgoldfinemiddleton
Copy link
Contributor Author

It appears there are no longer CI errors! Not sure what changed.

test/test612.js Outdated

it('With quote = \'\', single multiword string value', function(){
alasql("SELECT 'swing out' AS `colname` INTO CSV('test612-1', {quote:''})");
fs.open('test612-1.csv', 'r', function(err, fd) {
Copy link
Member

Choose a reason for hiding this comment

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

Looks really good with the tests.

A) Interacting with files is always async, so technically when fs starts reading the files we are not sure if the file has been written by the alasql command - so we need to use an async notation. replace alasql(...) with alasql.promise(...) and add .then(function(){ ... (code to assert things ... ) })

B) when testing async code the test must call first parameter passed from the it(...) function. We normally would change function(){ to function(done){ and then call done() as the last part after the assert

B) Next time you read a file you can avoid all the hassle by just using fs.readFileSync(file[, options])

The whole fs. thing can be changed to

assert('colname\n?swing out?' === fs.readFileSync('test612-1.csv','utf8)); 

(and then call done() after)

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 that. I've made the changes, but I'm having issues with the assertions. Here's what the first it() call looks like for me now:

it('With quote = \'\', single string value', function(){
		return alasql.promise("SELECT 'swing' AS `colname` INTO CSV('test612-0', {quote:''})")
			.then(function(state) {
				console.log(fs.readFileSync('test612-0.csv', 'utf8') + '\n\n');
				assert(fs.readFileSync('test612-0.csv', 'utf8') === 'colname\nswing\n');
		}, function(err) {});
	});

It appears that the string readFileSync() is pulling is not not what I imagine it to be because the assertion fails. I've also tried the following strings and various other combinations I've forgotten:
'colname\nswing\n\ufeff'
'colname\r\nswing\r\n\ufeff'
'colname\nswing\n\n

I'm at a loss for how to write the assertion. Any ideas on where I might be going wrong with this?

Also, I chose the return the Promise rather than use done() because I read that mocha will catch the errors automatically. That part seems to be working now, whereas I was getting errors using done() as you advised.

Copy link
Member

Choose a reason for hiding this comment

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

Dont worry, we will get there.

  • As the alasql call is async we need to givethe testframework a way of knowing when the call is done. We do that by calling the fist parameter of the callback function to the it(... function. Its not named at the moment. Lets call it done
it('With quote = \'\', single string value', function(done){
```

and then call it after the assert with `done()`

- The `then(` function does not hvae a second parameter, but return a promise where we can `catch` errors.

- Also, reading a file is expensive. If you need the data twice 

- The assert function can take a true/false (you get that by the `===`) but it works better if you give each side as a parameter - because then the test will output what was expected and what we got instead (making it easyer to debug)

In all I suggest the code looks like 

```js
it('With quote = \'\', single string value', function(done ){
	alasql.promise("SELECT 'swing' AS `colname` INTO CSV('test612-0', {quote:''})")
		.then(function(state) {
			var filecontent = fs.readFileSync('test612-0.csv', 'utf8');
			assert(filecontent,'colname\nswing\n');
			done();
		}).catch(err) {
			//console.error(err)
		}
	;
});
``` 

I dont get the `\ufeff` part of the code you are trying to assert


@mathiasrw
Copy link
Member

The Travis sometimes has a timeout error. I asked to rerun the test and it turned it all green :-)

@jrgoldfinemiddleton
Copy link
Contributor Author

jrgoldfinemiddleton commented Mar 21, 2017

It seems to me that we need to clarify what the expected behavior is when opt.quote is defined to be a character that appears in the data strings.

For example, if opt.quote = '?' and one of the column values is the string 'test?me', do we want the output in the CSV file to be ?test""me?? If opt.quote = '"' and one of the column values is the string 'help"me', should the CSV output be "help""me"? This is the current behavior.

With respect to the case where opt.quote = '', the CSV output should always have the format of string goes here (no double quotes on the ends, even to comply with Microsoft Excel), correct? My thinking is that setting opt.quote = '' would allow the user to override the Microsoft Excel default.

@mathiasrw
Copy link
Member

Hmm. Its a good question.

I would say that for

For example, if opt.quote = '?' and one of the column values is the string 'test?me', do we want the output in the CSV file to be ?test""me??

I would expect ?test??me?

a its the quotechar that must be escaped. Im not super sure about it, so please come with inputs.

@jrgoldfinemiddleton
Copy link
Contributor Author

jrgoldfinemiddleton commented Mar 22, 2017

Hey Mathias, I've figured out how to get the tests and assertions to work correctly now. I've opted not to use the done callback in favor of just returning the promise. Mocha lets you either call done() or return a Promise. I also decided the catch() chained call is unnecessary as well, since Mocha handles Promises that are rejected due to thrown errors. I've tested my test code by intentionally making the assertions fail and believe it is solid.

The \ufeff character represents the BOM which is not stripped out from the return value of readFileSync(). Now the tests are passing. I discovered this by looking at the Buffer return type.

And I suppose I see what you're saying regarding escaping the quote character within a field, whatever it happens to be. Right now, the code will always replace a quote character in the middle of the field with "", but if the quote character is set to ? we should change the behavior so that it would be replaced with ??. I just want to confirm with you that I should go ahead and make that change first. Anyone else is welcome to chime in.

@mathiasrw
Copy link
Member

mathiasrw commented Mar 22, 2017

Good work! had no idea I could just return the promise...

but if the quote character is set to ? we should change the behavior so that it would be replaced with ??

guess you are right. Let me know when I can merge in - and we actually should make a test case for it too..

Ahh, haha, its the bom :) just give the CSV command the option utf8Bom: false as described in https://github.com/agershun/alasql/wiki/Csv#options

@jrgoldfinemiddleton
Copy link
Contributor Author

Okay, fix applied to escape all quote characters other than ''. Test case added for it as well. Should be good to merge.

@mathiasrw
Copy link
Member

Awwweeesommmme

@mathiasrw mathiasrw merged commit 5817e72 into AlaSQL:develop Mar 22, 2017
@jrgoldfinemiddleton
Copy link
Contributor Author

Once again, thank you so much for your patient assistance. You've helped me feel much more confident in contributing to an open source project.

@mathiasrw
Copy link
Member

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.

Cant have empty quote in csv
3 participants