def test_r_package_usage(self): import r_index_logging r_index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}) ]) _, rows = r(service, None, ''' library(race) library(boot) library(race) output = data.frame(h1=c('v1')) ''') indexed_events = r_index_logging.get_log_entries() def is_indexed(text): for e in indexed_events: if text in e: return True return False self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"race\"')) self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"boot\"'))
def test_r_package_usage_from_custom_script(self): import r_index_logging r_index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}) ]) scriptlib.add(service, 'test', """ library(race) add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) _, rows = r(service, None, 'test.r') indexed_events = r_index_logging.get_log_entries() def is_indexed(text): for e in indexed_events: if text in e: return True return False self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"race\"'))
def test_create_stanza(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) r_config.create_stanza(service, 'test', 'test4', {'a': 'b'}) cnt = 0 for stanza, name in r_config.iter_stanzas(service, 'test'): self.assertEqual(cnt, 0) cnt += 1 self.assertEqual(getattr(stanza, 'a'), 'b')
def test_iter_stanzas(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) cnt = 0 for stanza, name in config.iter_stanzas(service, 'script'): self.assertTrue(name.startswith('test')) self.assertTrue(stanza.name.startswith('script://')) cnt += 1 self.assertEqual(2, cnt)
def test_delete_stanza(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) config.delete_stanza(service, 'script', 'test3') config.delete_stanza(service, 'script', 'test1') for _ in config.iter_stanzas(service, 'script'): self.fail('No stanza should be found') cnt = 0 for stanza, name in config.iter_stanzas(service, 'package'): self.assertEqual('package://test2', stanza.name) cnt += 1 self.assertEqual(1, cnt)
def test_r_package_installation(self): service = Service([ Stanza('package://race', {}) ]) input_data = [{'Name': 'Robert'}] _, rows = r(service, input_data, ''' library(race) output = input ''') self.assertEqual(len(rows), 1) self.assertEqual(rows[0]['Name'], 'Robert')
def test_update_library(self): service = Service([ Stanza( 'script://test', { 'content': 'YWRkIDwtIGZ1bmN0aW9uKGEsYikgew0KICBjID0gYStiDQogIHJldHVybihjKQ0KfQ0KcmVzdWx0ID0gY' 'WRkKDQsMjAwKQ0Kb3V0cHV0ID0gZGF0YS5mcmFtZShSZXN1bHQ9YyhyZXN1bHQpKQ==', }), ]) scripts.create_files(service) installed_scripts = os.listdir(scripts.get_custom_scripts_path()) self.assertEqual(len(installed_scripts), 1) self.assertEqual(installed_scripts[0], 'test.r') mtime = os.path.getmtime( os.path.join(scripts.get_custom_scripts_path(), installed_scripts[0])) scripts.create_files(service) self.assertEqual( mtime, os.path.getmtime( os.path.join(scripts.get_custom_scripts_path(), installed_scripts[0]))) time.sleep(1.5) scripts.add( service, 'test', """ add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) time.sleep(1.5) scripts.create_files(service) installed_scripts = os.listdir(scripts.get_custom_scripts_path()) self.assertEqual(len(installed_scripts), 1) self.assertEqual(installed_scripts[0], 'test.r') self.assertNotEqual( mtime, os.path.getmtime( os.path.join(scripts.get_custom_scripts_path(), installed_scripts[0]))) mtime = os.path.getmtime( os.path.join(scripts.get_custom_scripts_path(), installed_scripts[0])) scripts.create_files(service) self.assertEqual( mtime, os.path.getmtime( os.path.join(scripts.get_custom_scripts_path(), installed_scripts[0])))
def test_r_package_usage(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) _, rows = r( service, None, ''' library(race) library(boot) library(race) output = data.frame(h1=c('v1')) ''') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 4) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1]) self.assertTrue('action=\"package_usage\"' in indexed_events[2]) self.assertTrue('package_name=\"boot\"' in indexed_events[2])
def test_r_package_usage_from_custom_script(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) scriptlib.add( service, 'test', """ library(race) add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) _, rows = r(service, None, 'test.r') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 3) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1])
def test_execute(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) with tempfile.NamedTemporaryFile(delete=False) as f: output_csv_filename = f.name try: script = 'output = data.frame(a=c(1,2,3))\n' script += 'write.csv(output, file = "' + output_csv_filename.replace( '\\', '\\\\') + '")\n' r_framework.exeute( service, script, r_packages.get_library_path(), r_scripts.get_custom_scripts_path(), ) with open(output_csv_filename, 'r') as f: reader = csv.reader(f) rows = [row for row in reader] finally: os.remove(output_csv_filename) self.assertEqual(len(rows), 4)
def test_update_library(self): service = Service([ Stanza('package://boot', {}), ]) r_packages.update_library(service) installed_packages = os.listdir(r_packages.get_library_path()) self.assertEqual(len(installed_packages), 1) self.assertEqual(installed_packages[0], 'boot') self.assertEqual(r_packages.get_package_state('boot'), r_packages.metadata_package_installed) r_packages.update_library(service) installed_packages = os.listdir(r_packages.get_library_path()) self.assertEqual(len(installed_packages), 1) self.assertEqual(installed_packages[0], 'boot') self.assertEqual(r_packages.get_package_state('boot'), r_packages.metadata_package_installed) r_packages.add(service, 'forecast') self.assertEqual(r_packages.get_package_state('forecast'), r_packages.metadata_package_not_installed) r_packages.update_library(service) installed_packages = os.listdir(r_packages.get_library_path()) self.assertTrue('boot' in installed_packages) self.assertTrue('forecast' in installed_packages) self.assertEqual(r_packages.get_package_state('boot'), r_packages.metadata_package_installed) self.assertEqual(r_packages.get_package_state('forecast'), r_packages.metadata_package_installed) r_packages.remove(service, 'boot') self.assertEqual(r_packages.get_package_state('boot'), r_packages.metadata_package_installed) r_packages.update_library(service) installed_packages = os.listdir(r_packages.get_library_path()) self.assertFalse('boot' in installed_packages) self.assertTrue('timeDate' in installed_packages) self.assertTrue('forecast' in installed_packages) self.assertEqual(r_packages.get_package_state('boot'), r_packages.metadata_package_not_installed) self.assertEqual(r_packages.get_package_state('timeDate'), r_packages.metadata_package_installed) self.assertEqual(r_packages.get_package_state('forecast'), r_packages.metadata_package_installed)