Python testfixtures.TempDirectory() Examples

The following are 23 code examples of testfixtures.TempDirectory(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module testfixtures , or try the search function .
Example #1
Source File: test_server_definition_file.py    From pywbem with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_ServerDefinitionFile_init(testcase, sd_file_data):
    # pylint: disable=unused-argument
    """
    Test function for ServerDefinitionFile.__init__()
    """

    with TempDirectory() as tmp_dir:

        # Create the server definition file
        fd_filename = 'tmp_server_definition_file.yml'
        sd_filepath = os.path.join(tmp_dir.path, fd_filename)
        if isinstance(sd_file_data, six.text_type):
            sd_file_data = sd_file_data.encode('utf-8')
        tmp_dir.write(fd_filename, sd_file_data)

        # The code to be tested
        ServerDefinitionFile(filepath=sd_filepath) 
Example #2
Source File: test_us_equity_pricing_loader.py    From zipline-chinese with Apache License 2.0 6 votes vote down vote up
def setUpClass(cls):
        cls.test_data_dir = TempDirectory()
        cls.db_path = cls.test_data_dir.getpath('adjustments.db')
        all_days = TradingEnvironment().trading_days
        cls.calendar_days = all_days[
            all_days.slice_indexer(TEST_CALENDAR_START, TEST_CALENDAR_STOP)
        ]
        daily_bar_reader = MockDailyBarSpotReader()
        writer = SQLiteAdjustmentWriter(cls.db_path, cls.calendar_days,
                                        daily_bar_reader)
        writer.write(SPLITS, MERGERS, DIVIDENDS)

        cls.assets = TEST_QUERY_ASSETS
        cls.asset_info = EQUITY_INFO
        cls.bcolz_writer = SyntheticDailyBarWriter(
            cls.asset_info,
            cls.calendar_days,
        )
        cls.bcolz_path = cls.test_data_dir.getpath('equity_pricing.bcolz')
        cls.bcolz_writer.write(cls.bcolz_path, cls.calendar_days, cls.assets) 
Example #3
Source File: test_create_sitemap_files.py    From allura with Apache License 2.0 6 votes vote down vote up
def test_create(self):
        with TempDirectory() as tmpdir:
            rmtree(tmpdir.path)  # needs to be non-existent for the script
            self.run_script(['-o', tmpdir.path])

            tmpdir.check('sitemap-0.xml', 'sitemap.xml')

            xml_index = ET.parse(os.path.join(tmpdir.path, 'sitemap.xml'))
            ns = {'ns0': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
            locs = [loc.text for loc in xml_index.findall('ns0:sitemap/ns0:loc', ns)]
            assert_in('http://localhost/allura_sitemap/sitemap-0.xml', locs)

            xml_0 = ET.parse(os.path.join(tmpdir.path, 'sitemap-0.xml'))
            urls = [loc.text for loc in xml_0.findall('ns0:url/ns0:loc', ns)]
            assert_in('http://localhost/p/wiki/', urls)
            assert_in('http://localhost/p/test/sub1/', urls) 
Example #4
Source File: test_aws_lambda.py    From runway with Apache License 2.0 6 votes vote down vote up
def test_copydir():
    """Test copydir."""
    example_file = b'example file content'
    with TempDirectory() as tmp_dir:
        dest_path = tmp_dir.makedir('dest')
        src_path = tmp_dir.makedir('src')
        tmp_dir.makedir(('src', 'lib'))
        tmp_dir.write(('src', 'example_file'), example_file)
        tmp_dir.write(('src', 'lib', 'example_file'), example_file)

        copydir(src_path, dest_path, '**')

        assert tmp_dir.read(('src', 'example_file')) == example_file
        assert tmp_dir.read(('src', 'lib', 'example_file')) == example_file
        assert tmp_dir.read(('dest', 'example_file')) == example_file
        assert tmp_dir.read(('dest', 'lib', 'example_file')) == example_file 
Example #5
Source File: test_minute_bars.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def setUp(self):

        self.dir_ = TempDirectory()
        self.dir_.create()
        self.dest = self.dir_.getpath('minute_bars')
        os.makedirs(self.dest)
        self.writer = BcolzMinuteBarWriter(
            TEST_CALENDAR_START,
            self.dest,
            self.market_opens,
            self.market_closes,
            US_EQUITIES_MINUTES_PER_DAY,
        )
        self.reader = BcolzMinuteBarReader(self.dest) 
Example #6
Source File: test_logging.py    From pywbem with GNU Lesser General Public License v2.1 5 votes vote down vote up
def tmp_dir():
    with TempDirectory() as tmpdir:
        yield tmpdir


# Used in place of log_capture decorator with pytest since the
# test_fixtures log_capture decorator does not work with pytest 
Example #7
Source File: test_repository.py    From allura with Apache License 2.0 5 votes vote down vote up
def test_is_empty(self):
        assert not self.repo.is_empty()
        with TempDirectory() as d:
            repo2 = SM.Repository(
                name='test',
                fs_path=d.path,
                url_path='/test/',
                tool='svn',
                status='creating')
            repo2.init()
            assert repo2.is_empty()
            repo2.refresh()
            ThreadLocalORMSession.flush_all()
            assert repo2.is_empty() 
Example #8
Source File: test_repository.py    From allura with Apache License 2.0 5 votes vote down vote up
def test_is_empty(self):
        assert not self.repo.is_empty()
        with TempDirectory() as d:
            repo2 = GM.Repository(
                name='test',
                fs_path=d.path,
                url_path='/test/',
                tool='git',
                status='creating')
            repo2.init()
            assert repo2.is_empty()
            repo2.refresh()
            ThreadLocalORMSession.flush_all()
            assert repo2.is_empty() 
Example #9
Source File: dataset_test.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def _test_helper(self, *args, **kwargs):
    with testfixtures.TempDirectory() as d:
      for f in self.files:
        d.write(f, b'any')

      f = tempfile.NamedTemporaryFile(mode='r+t', suffix='.csv')
      mock_ret_val = ('image.csv', [self.bounding_box]*self.NUM_BOUNDING_BOXES)
      with mock.patch.object(
          annotation, 'read', return_value=mock_ret_val):
        dataset.gen_csv_from_annotations(d.path, f.name, *args, **kwargs)
        f.seek(0)
        reader = csv.reader(f)
      return reader 
Example #10
Source File: dataset_test.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def _test_helper(self, *args, **kwargs):
    with testfixtures.TempDirectory() as d:
      for image, label in zip(self.test_images, self.test_labels):
        d.write(os.path.join(label, image), b'any')

      f = tempfile.NamedTemporaryFile(mode='r+t', suffix='.csv')
      dataset.gen_csv_from_images(d.path, f.name, *args, **kwargs)
      f.seek(0)
      reader = csv.reader(f)
      return reader 
Example #11
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_raise_not_implimented(self):
        """Test NotImplimentedError is raised when no requirements file."""
        with TempDirectory() as tmp_dir:
            with pytest.raises(NotImplementedError):
                handle_requirements(
                    package_root=tmp_dir.path,
                    dest_path=tmp_dir.path,
                    requirements={
                        'requirements.txt': False,
                        'Pipfile': False,
                        'Pipfile.lock': False
                    }
                ) 
Example #12
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_implicit_pipenv(self):
        """Test implicit use of pipenv."""
        with TempDirectory() as tmp_dir:
            tmp_dir.write('Pipfile', self.PIPFILE.encode('utf-8'))
            req_path = handle_requirements(package_root=tmp_dir.path,
                                           dest_path=tmp_dir.path,
                                           requirements=find_requirements(tmp_dir.path))

            assert req_path == os.path.join(tmp_dir.path, 'requirements.txt')
            assert tmp_dir.read('Pipfile.lock')
            assert tmp_dir.read('requirements.txt') == \
                b'-i https://pypi.org/simple\n\n' 
Example #13
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_default(self):
        """Test default action."""
        expected = b'This is correct.'
        with TempDirectory() as tmp_dir:
            tmp_dir.write('Pipfile', self.PIPFILE.encode('utf-8'))
            tmp_dir.write('requirements.txt', expected)
            req_path = handle_requirements(package_root=tmp_dir.path,
                                           dest_path=tmp_dir.path,
                                           requirements=find_requirements(tmp_dir.path))

            assert req_path == os.path.join(tmp_dir.path, 'requirements.txt')
            assert not os.path.isfile(os.path.join(tmp_dir.path, 'Pipfile.lock'))
            assert tmp_dir.read('requirements.txt') == expected 
Example #14
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_with_docker_file(self):
        """Test with docker_file provided."""
        client = make_fake_client()
        with TempDirectory() as tmp_dir:
            docker_file = tmp_dir.write('Dockerfile', b'')
            dockerized_pip(os.getcwd(), client=client,
                           docker_file=docker_file)

            client.api.build.assert_called_with(
                path=tmp_dir.path,
                dockerfile='Dockerfile',
                forcerm=True
            )
            client.api.create_container.assert_called_with(
                detach=True,
                image=FAKE_IMAGE_ID,
                command=self.command,
                host_config=self.host_config
            )
            client.api.inspect_container.assert_called_with(FAKE_CONTAINER_ID)
            client.api.start.assert_called_with(FAKE_CONTAINER_ID)
            client.api.logs.assert_called_with(FAKE_CONTAINER_ID,
                                               stderr=True,
                                               stdout=True,
                                               stream=True,
                                               tail=0) 
Example #15
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_calculate_hash_different_ordering(self):
        """Test calculate hash different ordering."""
        files1 = ALL_FILES
        files2 = random.sample(ALL_FILES, k=len(ALL_FILES))
        with TempDirectory() as temp_dir1:
            root1 = temp_dir1.path
            for file_name in files1:
                temp_dir1.write(file_name, b"")
            with TempDirectory() as temp_dir2:
                root2 = temp_dir2.path
                for file_name in files2:
                    temp_dir2.write(file_name, b"")
                hash1 = _calculate_hash(files1, root1)
                hash2 = _calculate_hash(files2, root2)
                self.assertEqual(hash1, hash2) 
Example #16
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def test_calculate_hash_diff_filename_same_contents(self):
        """Test calculate hash diff filename same contents."""
        files = ["file1.txt", "f2/file2.txt"]
        file1, file2 = files
        with TempDirectory() as temp_dir:
            root = temp_dir.path
            for file_name in files:
                temp_dir.write(file_name, b"data")
            hash1 = _calculate_hash([file1], root)
            hash2 = _calculate_hash([file2], root)
        self.assertNotEqual(hash1, hash2) 
Example #17
Source File: test_aws_lambda.py    From runway with Apache License 2.0 5 votes vote down vote up
def temp_directory_with_files(cls, files=ALL_FILES):
        """Create a temp directory with files."""
        temp_dict = TempDirectory()
        for file_ in files:
            temp_dict.write(file_, b'')
        return temp_dict 
Example #18
Source File: test_algorithm.py    From catalyst with Apache License 2.0 5 votes vote down vote up
def _test_minute_data(self, algo_class):
        start_session = pd.Timestamp('2002-1-2', tz='UTC')
        period_end = pd.Timestamp('2002-1-4', tz='UTC')
        equities = pd.DataFrame([{
            'start_date': start_session,
            'end_date': period_end + timedelta(days=1),
            'exchange': "TEST",
        }] * 2)
        equities['symbol'] = ['A', 'B']
        with TempDirectory() as tempdir, \
                tmp_trading_env(equities=equities,
                                load=self.make_load_function()) as env:
            sim_params = SimulationParameters(
                start_session=start_session,
                end_session=period_end,
                capital_base=1.0e5,
                data_frequency='minute',
                trading_calendar=self.trading_calendar,
            )

            data_portal = create_data_portal(
                env.asset_finder,
                tempdir,
                sim_params,
                equities.index,
                self.trading_calendar,
            )
            algo = algo_class(sim_params=sim_params, env=env)
            algo.run(data_portal) 
Example #19
Source File: test_pipeline_algo.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        cls.AAPL = 1
        cls.MSFT = 2
        cls.BRK_A = 3
        cls.assets = [cls.AAPL, cls.MSFT, cls.BRK_A]
        asset_info = make_simple_equity_info(
            cls.assets,
            Timestamp('2014'),
            Timestamp('2015'),
            ['AAPL', 'MSFT', 'BRK_A'],
        )
        cls.env = trading.TradingEnvironment()
        cls.env.write_data(equities_df=asset_info)
        cls.tempdir = tempdir = TempDirectory()
        tempdir.create()
        try:
            cls.raw_data, bar_reader = cls.create_bar_reader(tempdir)
            adj_reader = cls.create_adjustment_reader(tempdir)
            cls.pipeline_loader = USEquityPricingLoader(
                bar_reader, adj_reader
            )
        except:
            cls.tempdir.cleanup()
            raise

        cls.dates = cls.raw_data[cls.AAPL].index.tz_localize('UTC')
        cls.AAPL_split_date = Timestamp("2014-06-09", tz='UTC') 
Example #20
Source File: test_engine.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        cls.first_asset_start = Timestamp('2015-04-01', tz='UTC')
        cls.env = TradingEnvironment()
        cls.trading_day = day = cls.env.trading_day
        cls.calendar = date_range('2015', '2015-08', tz='UTC', freq=day)

        cls.asset_info = make_rotating_equity_info(
            num_assets=6,
            first_start=cls.first_asset_start,
            frequency=day,
            periods_between_starts=4,
            asset_lifetime=8,
        )
        cls.last_asset_end = cls.asset_info['end_date'].max()
        cls.all_asset_ids = cls.asset_info.index

        cls.env.write_data(equities_df=cls.asset_info)
        cls.finder = cls.env.asset_finder

        cls.temp_dir = TempDirectory()
        cls.temp_dir.create()

        try:
            cls.writer = SyntheticDailyBarWriter(
                asset_info=cls.asset_info[['start_date', 'end_date']],
                calendar=cls.calendar,
            )
            table = cls.writer.write(
                cls.temp_dir.getpath('testdata.bcolz'),
                cls.calendar,
                cls.all_asset_ids,
            )

            cls.pipeline_loader = USEquityPricingLoader(
                BcolzDailyBarReader(table),
                NullAdjustmentReader(),
            )
        except:
            cls.temp_dir.cleanup()
            raise 
Example #21
Source File: test_us_equity_pricing.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def setUp(self):

        self.asset_info = EQUITY_INFO
        self.writer = SyntheticDailyBarWriter(
            self.asset_info,
            self.trading_days,
        )

        self.dir_ = TempDirectory()
        self.dir_.create()
        self.dest = self.dir_.getpath('daily_equity_pricing.bcolz') 
Example #22
Source File: test_server_definition_file.py    From pywbem with GNU Lesser General Public License v2.1 4 votes vote down vote up
def test_ServerDefinitionFile_list_servers(
        testcase, sd_file_data, list_nickname, exp_sd_nick_list):
    """
    Test function for ServerDefinitionFile.list_servers()
    """

    with TempDirectory() as tmp_dir:

        # Create the server definition file
        fd_filename = 'tmp_server_definition_file.yml'
        sd_filepath = os.path.join(tmp_dir.path, fd_filename)
        if isinstance(sd_file_data, six.text_type):
            sd_file_data = sd_file_data.encode('utf-8')
        tmp_dir.write(fd_filename, sd_file_data)

        try:
            sdf = ServerDefinitionFile(filepath=sd_filepath)
        except Exception as exc:  # pylint: disable=broad-except
            pytest.fail(
                "Unexpected exception from ServerDefinitionFile(): {0}: {1}".
                format(exc.__class__.__name__, exc))

        # The code to be tested
        sd_list = sdf.list_servers(list_nickname)

        # Ensure that exceptions raised in the remainder of this function
        # are not mistaken as expected exceptions
        assert testcase.exp_exc_types is None

        act_list_servers_len = len(sd_list)
        assert act_list_servers_len == len(exp_sd_nick_list), \
            "Unexpected number of ServerDefinition objects returned from " \
            "list_servers(): Expected nicks {0!r}, got nicks {1!r}". \
            format(exp_sd_nick_list, [sd.nickname for sd in sd_list])

        for i, sd in enumerate(sd_list):
            exp_sd_nick = exp_sd_nick_list[i]
            assert sd.nickname == exp_sd_nick, \
                "Unexpected ServerDefinition object returned from " \
                "list_servers() at position {0}: " \
                "Expected nick {1!r}, got nick {2!r}". \
                format(i, exp_sd_nick, sd.nickname) 
Example #23
Source File: test_server_definition_file.py    From pywbem with GNU Lesser General Public License v2.1 4 votes vote down vote up
def test_ServerDefinitionFile_list_all_servers(
        testcase, sd_file_data, exp_sd_nick_list):
    # pylint: disable=invalid-name
    """
    Test function for ServerDefinitionFile.list_all_servers()
    """

    with TempDirectory() as tmp_dir:

        # Create the server definition file
        fd_filename = 'tmp_server_definition_file.yml'
        sd_filepath = os.path.join(tmp_dir.path, fd_filename)
        if isinstance(sd_file_data, six.text_type):
            sd_file_data = sd_file_data.encode('utf-8')
        tmp_dir.write(fd_filename, sd_file_data)

        try:
            sdf = ServerDefinitionFile(filepath=sd_filepath)
        except Exception as exc:  # pylint: disable=broad-except
            pytest.fail(
                "Unexpected exception from ServerDefinitionFile(): {0}: {1}".
                format(exc.__class__.__name__, exc))

        # The code to be tested
        sd_list = sdf.list_all_servers()

        # Ensure that exceptions raised in the remainder of this function
        # are not mistaken as expected exceptions
        assert testcase.exp_exc_types is None

        act_list_servers_len = len(sd_list)
        assert act_list_servers_len == len(exp_sd_nick_list), \
            "Unexpected number of ServerDefinition objects returned from " \
            "list_all_servers(): Expected nicks {0!r}, got nicks {1!r}". \
            format(exp_sd_nick_list, [sd.nickname for sd in sd_list])

        for i, sd in enumerate(sd_list):
            exp_sd_nick = exp_sd_nick_list[i]
            assert sd.nickname == exp_sd_nick, \
                "Unexpected ServerDefinition object returned from " \
                "list_all_servers() at position {0}: " \
                "Expected nick {1!r}, got nick {2!r}". \
                format(i, exp_sd_nick, sd.nickname)