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

Issue #26 test cases and uri code updated #31

Merged
merged 11 commits into from
Mar 10, 2020
Merged

Conversation

DaasDaham
Copy link
Contributor

@DaasDaham DaasDaham commented Mar 2, 2020

Tested with all existing test cases (all passed) and added new test cases.
List of changes:

  1. Call _verify_srametadb(sqlite_file) function before calling super() to ensure first that the file is right since this function also calls BASEdb.
  2. Changed sqlite.connect() to use uri syntax which can be used to control details of the newly created database connection.
  3. Added a check to replace all the '?' characters in input as they were causing the input to be truncated at the first '?' character, this might be happening because the uri syntax also uses ? to start mode declaration.

self.db = sqlite3.connect(self.sqlite_file)
# Originally sqlite3.connect(self.sqlite_file)
self.sqlite_file = self.sqlite_file.replace('?','')
self.db = sqlite3.connect('file:{}?mode=rw'.format(self.sqlite_file), uri=True)
Copy link
Owner

Choose a reason for hiding this comment

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

Any particular reason to have the rw mode and not just r?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sqlite3.connect() has rwc (read-write-create) mode and it gives both read-write permission to user as well as creates the file if it is not present. I changed it to rw mode so that it does not create a new file if it does not exist and to keep the database permissions same as the original command. If we want to give only read permission I'll change it to ro, please confirm.

Copy link
Owner

Choose a reason for hiding this comment

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

We do not need the write/create permission. The file is downloaded from an online resource. If it does not exist, it should just throw an error. Probably it is simpler to just check os.path.isfile(input_file)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification. I'll change the mode to ro. Actually, it does throw an error in case no file is found sqlite3.OperationalError: unable to open database file which is caught in _verify_srametadb() function so there is no need for a separate check.

@@ -24,7 +25,9 @@ def __init__(self, sqlite_file):

def open(self):
"""Open sqlite connection."""
self.db = sqlite3.connect(self.sqlite_file)
# Originally sqlite3.connect(self.sqlite_file)
self.sqlite_file = self.sqlite_file.replace('?','')
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a need to replace '?'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The '?' character was causing the input to be truncated after '?' in uri syntax. But now since you have clarified that user does not provide a wrong path, I'll remove this check.

wrong_path = os.path.join(os.getcwd(), "data", "SRAmet")
try:
db = SRAdb(path)
except:
Copy link
Owner

Choose a reason for hiding this comment

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

This should explicitly mention the Exception it is catching.

db = SRAdb(path)
except:
pass
assert os.path.isfile(wrong_path) == False
Copy link
Owner

Choose a reason for hiding this comment

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

This is redundant test (as is user provided wrong path) and can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, thanks for clarifying. I will remove it.
I'll add other test cases then

db = SRAdb(path)
except:
pass
assert os.path.isfile(path) == False
Copy link
Owner

Choose a reason for hiding this comment

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

Redundant check, can be removed.

path = os.path.join(os.getcwd(), "data", "{}".format(wrong_filename))
try:
db = SRAdb(path)
except:
Copy link
Owner

Choose a reason for hiding this comment

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

Requires explicit Exception.

pass
try:
db = SRAdb(wrongfile_path)
assert False
Copy link
Owner

Choose a reason for hiding this comment

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

Not sure what is being tested 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.

This function is to check if it throws an error in case a wrong filename was provided by the user and the wrong filename already existed. It should throw error sqlite3.OperationalError: no such table: metaInfo.
However, since we are assuming that the user provides correct input then this test is also redundant.

db = SRAdb(wrongfile_path)
assert False
except Exception as e:
assert True
Copy link
Owner

Choose a reason for hiding this comment

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

What use case is this test case handling?

@saketkc
Copy link
Owner

saketkc commented Mar 3, 2020

Thanks @DaasDaham for your contribution! I have left inline reviews. _test_sradb.py would not run currently. It needs to be moved to test_sradb.py if it is to be detected by pytest. The problem however with that is the SRAmetadb.sqlite.gz can sometimes take a long time to be decompressed and hence the tests often fail on travis. I haven't tested it for a while, so its definitely worth a shot.

@DaasDaham
Copy link
Contributor Author

Thanks @DaasDaham for your contribution! I have left inline reviews. _test_sradb.py would not run currently. It needs to be moved to test_sradb.py if it is to be detected by pytest. The problem however with that is the SRAmetadb.sqlite.gz can sometimes take a long time to be decompressed and hence the tests often fail on travis. I haven't tested it for a while, so its definitely worth a shot.

Yes travis is throwing no space left error while extracting SRAmetadb.sqlite.gz. Can this be resolved?

@saketkc
Copy link
Owner

saketkc commented Mar 5, 2020

Hi @DaasDaham, I cannot think of an alternate to test the SRAmetadb mode. For now I would suggest, that we move back test_sradb.py to _test_sradb.py and add a new test_sradb.py with the only test being the one which throws an error if the file is either not present or not a valid sqlite file.

Thanks once again for your contribution!

@DaasDaham
Copy link
Contributor Author

Hi @saketkc I have made all the required changes. Please have a look

@DaasDaham
Copy link
Contributor Author

Hi @DaasDaham, I cannot think of an alternate to test the SRAmetadb mode. For now I would suggest, that we move back test_sradb.py to _test_sradb.py and add a new test_sradb.py with the only test being the one which throws an error if the file is either not present or not a valid sqlite file.

Thanks once again for your contribution!

Hi @saketkc, I haven't heard from you regarding this issue, I have made the changes that you mentioned in my latest commit. Please have a look at it.

path = 'SRAmetadb.sqlite'
try:
db = SRAdb(path)
assert False
Copy link
Owner

Choose a reason for hiding this comment

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

Why assert False?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because while testing, SRAmetadb.sqlite file won't exist in the current directory, hence if it succeeds in establishing a connection, then it would be wrong, hence the assert False.

Copy link
Owner

Choose a reason for hiding this comment

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

Ummm, but an error would be raised before hand and hence this statement is practically unreachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, practically it shouldn't reach it.
This statement is just for edge case in case it makes a connection...

Copy link
Owner

Choose a reason for hiding this comment

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

I guess it should be removed then...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, I've removed it in the latest commit.
But travis is failing to build even though pytest passes all checks. It may be because of the black --check statement.

try:
db = SRAdb(path)
assert False
except SystemExit:
Copy link
Owner

Choose a reason for hiding this comment

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

Can you help me understand which case would a SystemExit error arise?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case if a file does not exist, sqlite3.connect() (line 27) in basedb.py will throw an Operational error which will be caught in except: statement (line 152 in sradb.py) which will eventually lead to a sys.exit(1) on line 157 in sradb.py. This is the SystemExit that my function would catch.

removed assert False statement
@saketkc
Copy link
Owner

saketkc commented Mar 10, 2020

@DaasDaham can you run black . and recommit? The tests are failing because of the formatting.

@codecov
Copy link

codecov bot commented Mar 10, 2020

Codecov Report

Merging #31 into master will increase coverage by 14.84%.
The diff coverage is 59.62%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master      #31       +/-   ##
===========================================
+ Coverage   24.17%   39.02%   +14.84%     
===========================================
  Files           8        5        -3     
  Lines        1187      984      -203     
===========================================
+ Hits          287      384       +97     
+ Misses        900      600      -300     
Impacted Files Coverage Δ
pysradb/cli.py 0.00% <0.00%> (ø)
pysradb/download.py 22.41% <28.00%> (+4.63%) ⬆️
pysradb/basedb.py 41.86% <50.00%> (-55.76%) ⬇️
pysradb/sraweb.py 81.66% <81.75%> (ø)
pysradb/__init__.py 100.00% <100.00%> (ø)

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 99f738e...783e017. Read the comment docs.

@DaasDaham
Copy link
Contributor Author

@DaasDaham can you run black . and recommit? The tests are failing because of the formatting.

Thanks for the help! , it builds successfully now

@saketkc saketkc merged commit babd8f6 into saketkc:master Mar 10, 2020
@saketkc
Copy link
Owner

saketkc commented Mar 10, 2020

Thanks for your contribution @DaasDaham! Are you okay with your name being added in CONTRIBUTORS?

@saketkc saketkc mentioned this pull request Mar 10, 2020
@DaasDaham
Copy link
Contributor Author

Thanks for your contribution @DaasDaham! Are you okay with your name being added in CONTRIBUTORS?

Sure, thanks!.
Just one thing, this latest commit introduced a change in the print statement on line 46 in cli.py. I am really sorry for this, I have opened a pull request to revert it back to original print statement #33

@saketkc
Copy link
Owner

saketkc commented Mar 22, 2020

Hi @DaasDaham, are you okay with me adding you to the AUTHORS list?

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