Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added tests for new tools
  • Loading branch information
JoaoRodrigues committed Dec 10, 2018
1 parent f55d2e2 commit 9388d6c
Show file tree
Hide file tree
Showing 3 changed files with 478 additions and 0 deletions.
169 changes: 169 additions & 0 deletions tests/test_pdb_delresname.py
@@ -0,0 +1,169 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 João Pedro Rodrigues
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Unit Tests for `pdb_delresname`.
"""

import os
import sys
import unittest

from config import data_dir
from utils import OutputCapture


class TestTool(unittest.TestCase):
"""
Generic class for testing tools.
"""

def setUp(self):
# Dynamically import the module
name = 'pdbtools.pdb_delresname'
self.module = __import__(name, fromlist=[''])

def exec_module(self):
"""
Execs module.
"""

with OutputCapture() as output:
try:
self.module.main()
except SystemExit as e:
self.retcode = e.code

self.stdout = output.stdout
self.stderr = output.stderr

return

def test_single_sele(self):
"""$ pdb_delresname -ARG data/dummy.pdb"""

# Simulate input
sys.argv = ['', '-ARG', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 0)
self.assertEqual(len(self.stdout), 128)
self.assertEqual(len(self.stderr), 0)

def test_multiple_sele(self):
"""$ pdb_delresname -ARG,CA data/dummy.pdb"""

# Simulate input
sys.argv = ['', '-ARG,CA', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 0)
self.assertEqual(len(self.stdout), 127)
self.assertEqual(len(self.stderr), 0)

def test_empty_sele(self):
"""$ pdb_delresname - data/dummy.pdb"""

# Simulate input
sys.argv = ['', '-', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0)
self.assertEqual(self.stderr[0][:22],
"ERROR!! Residue name s")

def test_invalid_sele(self):
"""$ pdb_delresname -ABCD data/dummy.pdb"""

# Simulate input
sys.argv = ['', '-ABCD', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0)
self.assertEqual(self.stderr[0][:22],
"ERROR!! Residue name i")

def test_file_not_found(self):
"""$ pdb_delresname not_existing.pdb"""

afile = os.path.join(data_dir, 'not_existing.pdb')
sys.argv = ['', afile]

self.exec_module()

self.assertEqual(self.retcode, 1) # exit code is 1 (error)
self.assertEqual(len(self.stdout), 0) # nothing written to stdout
self.assertEqual(self.stderr[0][:22],
"ERROR!! File not found") # proper error message

def test_file_missing(self):
"""$ pdb_delresname -ARG"""

sys.argv = ['', '-ARG']

self.exec_module()

self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0) # no output
self.assertEqual(self.stderr[0],
"ERROR!! No data to process!")

def test_helptext(self):
"""$ pdb_delresname"""

sys.argv = ['']

self.exec_module()

self.assertEqual(self.retcode, 1) # ensure the program exited gracefully.
self.assertEqual(len(self.stdout), 0) # no output
self.assertEqual(self.stderr, self.module.__doc__.split("\n")[:-1])

def test_not_an_option(self):
"""$ pdb_delresname 20 data/dummy.pdb"""

sys.argv = ['', '20', os.path.join(data_dir, 'dummy.pdb')]

self.exec_module()

self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0)
self.assertEqual(self.stderr[0],
"ERROR! First argument is not an option: '20'")


if __name__ == '__main__':
from config import test_dir

mpath = os.path.abspath(os.path.join(test_dir, '..'))
sys.path.insert(0, mpath) # so we load dev files before any installation

unittest.main()
140 changes: 140 additions & 0 deletions tests/test_pdb_head.py
@@ -0,0 +1,140 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 João Pedro Rodrigues
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Unit Tests for `pdb_head`.
"""

import os
import sys
import unittest

from config import data_dir
from utils import OutputCapture


class TestTool(unittest.TestCase):
"""
Generic class for testing tools.
"""

def setUp(self):
# Dynamically import the module
name = 'pdbtools.pdb_head'
self.module = __import__(name, fromlist=[''])

def exec_module(self):
"""
Execs module.
"""

with OutputCapture() as output:
try:
self.module.main()
except SystemExit as e:
self.retcode = e.code

self.stdout = output.stdout
self.stderr = output.stderr

return

def test_valid_option(self):
"""$ pdb_head -10 data/dummy.pdb"""

# Simulate input
sys.argv = ['', '-10', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 0)
self.assertEqual(len(self.stdout), 10)
self.assertEqual(len(self.stderr), 0)

def test_invalid_option(self):
"""$ pdb_head --10 data/dummy.pdb"""

# Simulate input
sys.argv = ['', '--10', os.path.join(data_dir, 'dummy.pdb')]

# Execute the script
self.exec_module()

# Validate results
self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0)
self.assertEqual(self.stderr[0][:22],
"ERROR!! Option must be")

def test_file_not_found(self):
"""$ pdb_head not_existing.pdb"""

afile = os.path.join(data_dir, 'not_existing.pdb')
sys.argv = ['', afile]

self.exec_module()

self.assertEqual(self.retcode, 1) # exit code is 1 (error)
self.assertEqual(len(self.stdout), 0) # nothing written to stdout
self.assertEqual(self.stderr[0][:22],
"ERROR!! File not found") # proper error message

def test_file_missing(self):
"""$ pdb_head -10"""

sys.argv = ['', '-10']

self.exec_module()

self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0) # no output
self.assertEqual(self.stderr[0],
"ERROR!! No data to process!")

def test_helptext(self):
"""$ pdb_head"""

sys.argv = ['']

self.exec_module()

self.assertEqual(self.retcode, 1) # ensure the program exited gracefully.
self.assertEqual(len(self.stdout), 0) # no output
self.assertEqual(self.stderr, self.module.__doc__.split("\n")[:-1])

def test_not_an_option(self):
"""$ pdb_head 20 data/dummy.pdb"""

sys.argv = ['', '20', os.path.join(data_dir, 'dummy.pdb')]

self.exec_module()

self.assertEqual(self.retcode, 1)
self.assertEqual(len(self.stdout), 0)
self.assertEqual(self.stderr[0],
"ERROR! First argument is not an option: '20'")


if __name__ == '__main__':
from config import test_dir

mpath = os.path.abspath(os.path.join(test_dir, '..'))
sys.path.insert(0, mpath) # so we load dev files before any installation

unittest.main()

0 comments on commit 9388d6c

Please sign in to comment.