def test_multi_extension_install(temp_function_directory, sym,df_X,testpackage1):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        make_test_repos(pdext, temp_function_directory)

        assert hasattr(dfext, 'calculate_circumference_from_radius') == False
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == False
        # two repos installing into the default
        spec = sym.__import_file_line_spec__.format(testpackage1, 
                                    'calculate_circumference_from_radius') +\
                '\n' +\
               sym.__import_file_line_spec__.format(testpackage1, 
                                    'calculate_circumference_from_diameter')
        pdext.import_extension(spec)
    
        assert hasattr(dfext, 'calculate_circumference_from_radius') == True
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == True

        # Test execution of the radius extension
        dfext.calculate_circumference_from_radius('numbers')
        assert 'circumference1_from_radius' in df_X.columns
        dfext.calculate_circumference_from_diameter('numbers')
        assert 'circumference1_from_diameter' in df_X.columns
def test_multi_install_from_github(temp_module_directory, df_X, caplog, sym):
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        make_test_repos(pdext, temp_module_directory)

        # capture the log record when a github archive is downloaded
        caplog.set_level(logging.INFO)
        assert hasattr(dfext, 'multi') == False
        # install three functions from one repo (which should only be
        # downloaded once)
        spec = sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection@pdext_test',
                            'multi.circle_calculations') + '\n'
        spec += sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection@pdext_test',
                            'multi.circle_calculations2') + '\n'
        spec += sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection@pdext_test',
                            'multi.circle_calculations3') + '\n'
        pdext.import_extension(spec)
        assert hasattr(dfext, 'multi') == True

        # Test execution of the extension
        assert 'circumference' not in df_X
        assert 'area' not in df_X
        assert 'circumference2' not in df_X
        assert 'area2' not in df_X
        assert 'circumference3' not in df_X
        assert 'area3' not in df_X
        dfext.multi.circle_calculations('numbers')
        dfext.multi.circle_calculations2('numbers')
        dfext.multi.circle_calculations3('numbers')
        assert 'circumference' in df_X
        assert 'area' in df_X
        assert 'circumference2' in df_X
        assert 'area2' in df_X
        assert 'circumference3' in df_X
        assert 'area3' in df_X

        # Clean up
        df_X = df_X.drop(columns=[
            'circumference',
            'area',
            'circumference2',
            'area2',
            'circumference3',
            'area3',
        ])
        dfext = sym.df_ext(df_X)

        # The github archive should only have been downloaded once
        assert len(caplog.records) == 1
예제 #3
0
def test_show_extension(temp_module_directory, testpackage1, capsys, sym):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        make_test_repos(pdext, temp_module_directory)

        pdext.show_extensions()
        out, _ = capsys.readouterr()
        assert 'No extensions are installed' in out
        assert 'For help on individual extensions, use help(df.ext.<extension name>)' not in out

        def spec(func):
            return sym.__import_file_line_spec__.format(testpackage1, func)

        pdext.import_extension(spec('calculate_circumference_from_radius'))
        pdext.show_extensions()
        out, _ = capsys.readouterr()
        assert 'There is 1 extension installed' in out
        assert 'df.{}.calculate_circumference_from_radius(radius)'\
                .format(sym.__df_ext__) in out
        assert 'For help on individual extensions, use help(df.{}.<extension name>)'\
                .format(sym.__df_ext__) in out

        pdext.import_extension(spec('calculate_circumference_from_diameter'))
        pdext.show_extensions()
        out, _ = capsys.readouterr()
        assert 'There are 2 extensions installed' in out
        assert 'df.{}.calculate_circumference_from_radius(radius)'\
                .format(sym.__df_ext__) in out
        assert 'df.{}.calculate_circumference_from_diameter(diameter)'\
                .format(sym.__df_ext__) in out
        assert 'For help on individual extensions, use help(df.{}.<extension name>)'\
                .format(sym.__df_ext__) in out

        pdext.import_extension(
            spec('test.calculate_circumference_from_radius'))
        pdext.show_extensions()
        out, _ = capsys.readouterr()
        assert 'There are 3 extensions installed' in out
        assert 'df.{}.calculate_circumference_from_radius(radius)'\
                .format(sym.__df_ext__) in out
        assert 'df.{}.calculate_circumference_from_diameter(diameter)'\
                .format(sym.__df_ext__) in out
        assert 'Collection: test' in out
        assert 'df.{}.test.calculate_circumference_from_radius(radius)'\
                .format(sym.__df_ext__) in out
        assert 'For help on individual extensions, use help(df.{}.<extension name>)'\
                .format(sym.__df_ext__) in out
def test_pdext_install_and_remove_extension(temp_module_directory, df_X,sym,
                                   testpackage1, testpackage2, testpackage3):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        time_at_start = datetime.now().strftime(sym.__install_timestamp_fmt__)
        test_repos = make_test_repos(pdext, temp_module_directory)

        assert hasattr(dfext, 'calculate_circumference_from_radius') == False
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == False
        # two repos installing into the default
        def spec(func):
            return sym.__import_file_line_spec__.format(testpackage1, func)
        pdext.import_extension(spec('calculate_circumference_from_radius'))
        pdext.import_extension(spec('calculate_circumference_from_diameter'))
        
        assert hasattr(dfext, 'calculate_circumference_from_radius') == True
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == True

        # Check recorded install times are correct
        assert pdext.extension_collections[sym.__default_collection__]\
                                          ['calculate_circumference_from_radius']\
                            .ext_info.is_earlier_than(time_at_start) == False

        # can't remove default repo
        with pytest.raises(ValueError):
            pdext.remove_repository('test1')
        
        # change default and remove
        pdext.set_default_repository('test2')
        pdext.remove_repository('test1')

        assert hasattr(dfext, 'calculate_circumference_from_radius') == False
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == False

        # should install in test2 by which is now the default
        pdext.import_extension(sym.__import_file_line_spec__.format(testpackage2, 'calculate_circumference_from_radius'))
        pdext.import_extension(sym.__import_file_line_spec__.format(testpackage2, 'calculate_circumference_from_diameter'))
        assert hasattr(dfext, 'calculate_circumference_from_radius') == True
        assert hasattr(dfext, 'calculate_circumference_from_diameter') == True

        # add test1 back and make it default
        pdext.add_repository('test1', test_repos['test1'],
                              beginning=True, default=True)

        # Test execution of the radius extension based on search order
        def execute_radius_extension(col, df):
            dfext = sym.df_ext(df)
            assert col not in df
            dfext.calculate_circumference_from_radius('numbers')
            assert col in df
            df = df.drop(columns=col)
            return df
        
        df_X = execute_radius_extension('circumference1_from_radius', df_X)
        pdext.new_search_order(['test2', 'test1'])
        df_X = execute_radius_extension('circumference2_from_radius', df_X)
예제 #5
0
def test_extension_dependency_not_installed(temp_module_directory, df_X,
                                            testpackage1, caplog, capsys, sym):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        make_test_repos(pdext, temp_module_directory)

        assert hasattr(dfext,
                       'function_uses_external_package_not_installed') == False
        assert len(caplog.records) == 0
        # installing into the default - check for warning during install
        pdext.import_extension(testpackage1 + sym.__import_file_sep__ +
                               'function_uses_external_package_not_installed')
        # function exists but it is a dummy one
        assert hasattr(dfext,
                       'function_uses_external_package_not_installed') == True
        assert 'EXTENSION CANNOT BE LOADED' in dfext.function_uses_external_package_not_installed.__doc__

        assert len(caplog.records) == 1
        record = caplog.records[0]
        assert record.levelname == 'WARNING'
        assert 'extension function_uses_external_package_not_installed requires library pkg_doesnt_exist which is not installed'\
               in record.message

        # catch warning when the function is executed also
        dfext.function_uses_external_package_not_installed()
        assert len(caplog.records) == 2
        record = caplog.records[1]
        assert record.levelname == 'WARNING'
        assert record.message == '{} extension function_uses_external_package_not_installed requires library pkg_doesnt_exist which is not installed'\
                                    .format(sym.__pdext__)

        # Check that warning also included in the show_extensions() list
        pdext.show_extensions()
        out, _ = capsys.readouterr()
        function_warning = "function_uses_external_package_not_installed()  - Currently doesn't work because module:  pkg_doesnt_exist  needs to be installed"
        assert function_warning in out
예제 #6
0
def test_import_extension(temp_module_directory, df_X, testpackage1, sym):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        test_repos = make_test_repos(pdext, temp_module_directory)

        assert hasattr(dfext, 'calculate_circumference_from_radius') == False
        assert hasattr(dfext, 'test') == False
        # two repos installing into the default
        pdext.import_extension(testpackage1 + sym.__import_file_sep__ +
                               'calculate_circumference_from_radius')
        pdext.import_extension(testpackage1 + sym.__import_file_sep__ +
                               'test.calculate_circumference_from_diameter')

        assert hasattr(dfext, 'calculate_circumference_from_radius') == True
        assert hasattr(dfext, 'test') == True

        assert 'circumference1_from_radius' not in df_X.columns
        assert 'circumference1_from_diameter' not in df_X.columns
        # get the extension to test
        ext = sym.df_ext(df_X)
        ext.calculate_circumference_from_radius('numbers')
        ext.test.calculate_circumference_from_diameter('numbers')
        assert 'circumference1_from_radius' in df_X.columns
        assert 'circumference1_from_diameter' in df_X.columns
        df_X = df_X.drop(columns=[
            'circumference1_from_radius', 'circumference1_from_diameter'
        ])
        ext = sym.df_ext(df_X)

        # Create an import file is the current directory
        import_file_content = """\
# Comment to be ignored
            {package} {sep}  imp.calculate_circumference_from_radius
            {package} {sep}  calculate_circumference_from_diameter
            """.format(package=testpackage1, sep=sym.__import_file_sep__)
        import_file_name = 'import_test' + sym.__import_file_ext__
        with open(import_file_name, 'w') as f:
            f.writelines(import_file_content)

        assert hasattr(dfext, 'calculate_circumference_from_diameter') == False
        assert hasattr(dfext, 'imp') == False

        # import the packages in the file
        import_test = importlib.import_module('import_test')

        # Dummy module loaded in the namespace
        assert 'import_test' in locals()
        assert import_test.__file__ == '<{} import>'.format(sym.__pdext__)

        # clean up before completing tests
        os.remove(import_file_name)

        assert hasattr(dfext, 'calculate_circumference_from_diameter') == True
        assert hasattr(dfext, 'imp') == True

        assert 'circumference1_from_radius' not in df_X.columns
        assert 'circumference1_from_diameter' not in df_X.columns
        # get the extension to test
        ext = sym.df_ext(df_X)
        ext.imp.calculate_circumference_from_radius('numbers')
        ext.calculate_circumference_from_diameter('numbers')
        assert 'circumference1_from_radius' in df_X.columns
        assert 'circumference1_from_diameter' in df_X.columns
        df_X = df_X.drop(columns=[
            'circumference1_from_radius', 'circumference1_from_diameter'
        ])
        ext = sym.df_ext(df_X)
def test_install_from_github(temp_module_directory, df_X, caplog, sym):
    # Create a test repo for the whole duration of tests
    with save_current_installed_extensions():
        # make two test repos to install extensions into
        pdext = sym.pd_ext()
        dfext = sym.df_ext(df_X)
        make_test_repos(pdext, temp_module_directory)

        # capture the log record when a github archive is downloaded
        caplog.set_level(logging.INFO)

        # Check default latest version is downloaded and executes correctly
        assert hasattr(dfext, 'circle_calculations') == False
        # two repos installing into the default
        spec = sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection',
                            'circle_calculations')
        pdext.import_extension(spec)
        assert hasattr(dfext, 'circle_calculations') == True
        # Test execution of the extension
        assert 'circumference' not in df_X
        assert 'area' not in df_X
        dfext.circle_calculations('numbers')
        assert 'circumference' in df_X
        assert 'area' in df_X
        # Test correct version of the docstring
        assert 'Calculates the circumference' in dfext.circle_calculations.__doc__
        # Clean up
        df_X = df_X.drop(columns=['circumference', 'area'])
        dfext = sym.df_ext(df_X)

        # Check latest version of a particular branch is downloaded and executes correctly
        assert hasattr(dfext, 'test1') == False
        # two repos installing into the default
        spec = sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection@pdext_test',
                            'demo.circle_calculations')
        pdext.import_extension(spec)

        assert hasattr(dfext, 'demo') == True
        # Test execution of the extension
        assert 'circumference' not in df_X.columns
        assert 'area' not in df_X.columns
        dfext.demo.circle_calculations('numbers')
        assert 'circumference' in df_X.columns
        assert 'area' in df_X.columns
        # Test correct version of the docstring
        assert 'Calculates the circumference' not in dfext.demo.circle_calculations.__doc__
        assert 'Version: test branch' in dfext.demo.circle_calculations.__doc__
        # Clean up
        df_X = df_X.drop(columns=['circumference', 'area'])
        dfext = sym.df_ext(df_X)

        # Check a particular tag is downloaded and executes correctly
        assert hasattr(dfext, 'test2') == False
        # two repos installing into the default
        spec = sym.__import_file_line_spec__\
                    .format('github:connectedblue/pdext_collection@pdext_test_tag',
                            'test2.circle_calculations')
        pdext.import_extension(spec)

        assert hasattr(dfext, 'test2') == True
        # Test execution of the extension
        assert 'circumference' not in df_X.columns
        assert 'area' not in df_X.columns
        dfext.test2.circle_calculations('numbers')
        assert 'circumference' in df_X.columns
        assert 'area' in df_X.columns
        # Test correct version of the docstring
        assert 'Calculates the circumference' not in dfext.test2.circle_calculations.__doc__
        assert 'Version: pdext_test_tag tag' in dfext.test2.circle_calculations.__doc__
        # Clean up
        df_X = df_X.drop(columns=['circumference', 'area'])
        dfext = sym.df_ext(df_X)
        # should be three log records, one for each download
        assert len(caplog.records) == 3