mysql2sqlite
mysql2sqlite copied to clipboard
Add unit tests for solved issues and make it a commit policy
What would be the right way to do this?
mysql2sqlite mydump01.sql | sqlite3 test01.db
Then testing that
- No error caused the process to fail
- The resulting schema contains the correct results.
EG, assert tables exist, indexes exist, columns are of appropriate type, correct number of rows inserted?
$ test01_count=$( sqlite3 test01.db "select count(*) from test_table" )
I've never written tests for shell / awk scripts before. I found this article linking off to "BATS"
Hi @dkam, thanks for the thorough tips.
My idea of unit testing is proportional to the size and importance of the tested program. In this case, I was thinking about the dumbest (and easiest) KISS solution - plain string matching in one shell file containing here doc sections. This shell file ought to be placed to a separate directory "tests". Why just string matching? Because that's exactly what unattended string filters do (mysql2sqlite is just a string filter).
Once we'll reach e.g. 30 unit tests, we'll switch to separate files instead of here docs, but I would keep the plain string matching. Also one of the goals is to have everything in this tiny project fully POSIX-compatible.
BATS seems really professional, but is not fully POSIX-compatible and is quite a huge dependency. And so I'm highly reluctant to use it. But maybe I missed something, so feel free to persuade me :)
Perfectly reasonable @dumblob. One nice reason for including SQLite in the chain is that when a new version comes out, you can confidently say it works as well as the previous versions. It doesn't seem like much work to do it that way - however I think either approach will be fine and I can see the advantages to the KISS approach.
So, here's a first crack at a Bash string matching version (running in test/). The output captured seems to eat the single quotes used in b'0'
#!/bin/bash
output=$( ../mysql2sqlite - <<'EOF'
CREATE TABLE `example` (
`priority` bit(1) NOT NULL DEFAULT b'0',
KEY `priority` (`priority`)
);
EOF
)
err_result="\`priority\` bit(1) NOT NULL DEFAULT b'0'"
if [[ $output =~ $err_result ]]; then
echo 'match'
else
echo 'no match'
fi
## The output variable *should* contain the string in err_result, but quotes seem to be eatten
echo $output
And here's a working version that uses Sqlite. This one captures stderr into the output variable and redirects stdout to /dev/null so we don't get the normal output of "memory" captured.
#!/bin/bash
output=$( ../mysql2sqlite - <<'EOF' | sqlite3 test01.db 2>&1 > /dev/null
CREATE TABLE `example` (
`priority` bit(1) NOT NULL DEFAULT b'0',
KEY `priority` (`priority`)
);
EOF
)
if [[ $? == 0 ]]; then
echo "Passed"
else
echo "Failed with error: ${output}"
fi
It would be nice to use shunit2 for testing...
@onlyjob shunit2 seems to be a nice framework, but even for this one undoubtedly holds what I wrote above. I've also already written about 30% of the desired tests (but didn't commit) and I'm completely fine with pure POSIX shell (it's really high-level enough for that purpose without any abstractions nor wrappers).
I didn't commit these changes, because I've moved few months ago to a foreign country and have still lots of stuff to handle (and the presence of just a 32kbit - yes, kbit, not kbyte - internet connection, which is in addition also very unstable, doesn't make things any easier for me :cry:; don't worry, it should be possible to get a working DSL line here and such services are quite quick here - it might take only few weeks from now).
Stay tuned!
I'm working in new features I bet for expect for testing shell scripts Do you think it's good that I work on that line? I'll do all my very best! ;-)
@jagarsoft just take a look at unit_tests.sh
(https://github.com/dumblob/mysql2sqlite/blob/master/unit_tests.sh ) and proceed. I.e. check all the current tests where many of them are not yet finished, then correct them, test them and finally make a pull request to this repository :wink:.
I've started to work on it ... By now, I have an more general solution that I think it will be more useful in the (near) future, but remember it was just born ;-)
All of you can check it out at https://github.com/jagarsoft/shell-test.git
Your comments will be welcomed ....
I'm proud to announce that shell-test project is approaching @frrad's PR #60 I hope it will be ready in next weeks (my time constraints are not smaller than yours 😢)
Hi all.
I have shell-test ready for mysql2sqlite, but tests do not pass (😢) and it should be
I have open an issue here https://github.com/jagarsoft/mysql2sqlite/issues/1
Please, would you be so kind having a look? Code is on branch unit_tests
Thanks in advance!!!