/usr/lib/python2.7/dist-packages/rpy2/robjects/tests/testPackages.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import unittest
import sys, io, tempfile
import rpy2.robjects as robjects
import rpy2.robjects.packages as packages
from rpy2.rinterface import RRuntimeError
rinterface = robjects.rinterface
class PackagesTestCase(unittest.TestCase):
def testNew(self):
env = robjects.Environment()
env['a'] = robjects.StrVector('abcd')
env['b'] = robjects.IntVector((1,2,3))
env['c'] = robjects.r(''' function(x) x^2''')
pck = robjects.packages.Package(env, "dummy_package")
self.assertTrue(isinstance(pck.a, robjects.Vector))
self.assertTrue(isinstance(pck.b, robjects.Vector))
self.assertTrue(isinstance(pck.c, robjects.Function))
def testNewWithDot(self):
env = robjects.Environment()
env['a.a'] = robjects.StrVector('abcd')
env['b'] = robjects.IntVector((1,2,3))
env['c'] = robjects.r(''' function(x) x^2''')
pck = robjects.packages.Package(env, "dummy_package")
self.assertTrue(isinstance(pck.a_a, robjects.Vector))
self.assertTrue(isinstance(pck.b, robjects.Vector))
self.assertTrue(isinstance(pck.c, robjects.Function))
def testNewWithDotConflict(self):
env = robjects.Environment()
env['a.a'] = robjects.StrVector('abcd')
env['a_a'] = robjects.IntVector((1,2,3))
env['c'] = robjects.r(''' function(x) x^2''')
self.assertRaises(packages.LibraryError,
robjects.packages.Package,
env, "dummy_package")
def testNewWithDotConflict2(self):
env = robjects.Environment()
env['__dict__'] = robjects.StrVector('abcd')
self.assertRaises(packages.LibraryError,
robjects.packages.Package,
env, "dummy_package")
class SignatureTranslatedAnonymousPackagesTestCase(unittest.TestCase):
def testNew(self):
string = """
square <- function(x) {
return(x^2)
}
cube <- function(x) {
return(x^3)
}
"""
powerpack = packages.SignatureTranslatedAnonymousPackage(string, "powerpack")
self.assertTrue(hasattr(powerpack, 'square'))
self.assertTrue(hasattr(powerpack, 'cube'))
class ImportrTestCase(unittest.TestCase):
def testImportStats(self):
stats = robjects.packages.importr('stats')
self.assertTrue(isinstance(stats, robjects.packages.Package))
def testImportStatsWithLibLoc(self):
path = robjects.packages.get_packagepath('stats')
stats = robjects.packages.importr('stats',
lib_loc = path)
self.assertTrue(isinstance(stats, robjects.packages.Package))
def testImportStatsWithLibLocAndSuppressMessages(self):
path = robjects.packages.get_packagepath('stats')
stats = robjects.packages.importr('stats', lib_loc=path,
suppress_messages=False)
self.assertTrue(isinstance(stats, robjects.packages.Package))
def testImportStatsWithLibLocWithQuote(self):
path = 'coin"coin'
with self.assertRaises(RRuntimeError):
if sys.version_info[0] == 3:
tmp_file = io.StringIO()
else:
# no need to test which Python 2, only 2.7 supported
tmp_file = tempfile.NamedTemporaryFile()
stdout = sys.stdout
sys.stdout = tmp_file
robjects.packages.importr('dummy_inexistant', lib_loc=path)
sys.stdout = stdout
tmp_file.close()
def testImportDatasets(self):
datasets = robjects.packages.importr('datasets')
self.assertTrue(isinstance(datasets, robjects.packages.Package))
self.assertTrue(isinstance(datasets.__rdata__,
robjects.packages.PackageData))
self.assertTrue(isinstance(robjects.packages.data(datasets),
robjects.packages.PackageData))
class WherefromTestCase(unittest.TestCase):
def testWherefrom(self):
stats = robjects.packages.importr('stats')
rnorm_pack = robjects.packages.wherefrom('rnorm')
self.assertEqual('package:stats',
rnorm_pack.do_slot('name')[0])
class InstalledPackagesTestCase(unittest.TestCase):
def testNew(self):
instapacks = robjects.packages.InstalledPackages()
res = instapacks.isinstalled('foo')
self.assertTrue(isinstance(res, bool))
ncols = len(instapacks.colnames)
for row in instapacks:
self.assertEqual(ncols, len(row))
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(PackagesTestCase)
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ImportrTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(WherefromTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(InstalledPackagesTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SignatureTranslatedAnonymousPackagesTestCase))
return suite
if __name__ == '__main__':
unittest.main()
|