def test_project_seeks_project_path(): fake_project_sub_directory = fixture_path('sample_project/lev1/lev2') base_parser = create_base_parser() base_options = base_parser.parse_args(args=[]) with in_directory(fake_project_sub_directory): project = Project.load(base_options) assert project.path() == fixture_path('sample_project') assert project.name == 'sample_project'
def setup(self): self._logger_spy = LoggerSpy() self._current_version = "0.0.1" self._bumped_version = "0.1.0" self._tmp_folder = tmp_folder() copy_tree(fixture_path("executors/before"), self._tmp_folder)
def test_test_environment_context(): templates_path = fixture_path('templates') with temp_template_environment(templates_path): env = environment() env.get_template('tests/test_template.sh.tmpl') env = environment() template = env.get_template('init/activate.sh.tmpl')
def test_get_trade_latest_returns_expected(self): xls_io = fixture_path(ba_name=self.c.__module__, filename='data1.xls') self.c.fetch_xls = MagicMock(return_value=ExcelFile(xls_io)) results = self.c.get_trade(latest=True) self.assertTrue(len(results), 1) self.assertEqual(results[0]['timestamp'], Timestamp('2017-10-16T12:40:00Z')) self.assertAlmostEqual(results[0]['net_exp_MW'], 964.22479248)
def test_test_environment_context(): from jinja2 import FileSystemLoader templates_path = fixture_path('templates') with temp_template_environment(templates_path): env = environment() env.get_template('tests/test_template.sh.jinja') env = environment() template = env.get_template('init/activate.sh.jinja')
def test_get_trade_yesterday_returns_expected(self): xls_io = fixture_path(ba_name=self.c.__module__, filename='data1.xls') self.c.fetch_xls = MagicMock(return_value=ExcelFile(xls_io)) results = self.c.get_trade(yesterday=True) self.assertTrue(len(results), 288) # 24 hours * 12 observations per hour self.assertEqual(results[0]['timestamp'], Timestamp('2017-10-15T07:00:00Z')) self.assertAlmostEqual(results[0]['net_exp_MW'], 662.693040848) self.assertEqual(results[287]['timestamp'], Timestamp('2017-10-16T06:55:00Z')) self.assertAlmostEqual(results[287]['net_exp_MW'], 700.70085144)
def test_in_directory_raises_error(): raised_error = False original_working_dir = os.getcwd() basic_project_dir = fixture_path('sample_project') try: with in_directory(basic_project_dir): raise TestException() except TestException: raised_error = True assert raised_error assert os.getcwd() == original_working_dir
def test_config_semver(self): config_file = fixture_path("config/.bumpit-semver.yaml") config = Configuration.parse(file=config_file) assert config.config_file == config_file assert config.current_version == "0.0.1" assert config.strategy.name == "semver" assert config.strategy.part == "minor" assert config.tag.apply assert config.tag.format == "{version}" assert config.auto_remote_push is False assert config.tracked_files == ["setup.py", "sample/VERSION"]
def test_get_trade_valid_range_returns_expected(self): xls_io = fixture_path(ba_name=self.c.__module__, filename='data1.xls') self.c.fetch_xls = MagicMock(return_value=ExcelFile(xls_io)) start_at = self.tzaware_utcnow - timedelta(days=3) end_at = self.tzaware_utcnow - timedelta(days=1) results = self.c.get_trade(start_at=start_at, end_at=end_at) self.assertTrue(len(results), 576) # 48 hours * 12 observations per hour self.assertEqual(results[0]['timestamp'], Timestamp('2017-10-13T12:45:00Z')) self.assertAlmostEqual(results[0]['net_exp_MW'], 1072.920944214) self.assertEqual(results[575]['timestamp'], Timestamp('2017-10-15T12:40:00Z')) self.assertAlmostEqual(results[575]['net_exp_MW'], 506.151199341)
def test_project_command_renders_template(FakeProject): """Test that a project command adds current project to the context""" (FakeProject.expects('load') .with_args('options').returns('proj')) with temp_template_environment(fixture_path('templates')): class FakeCommand(NoEventProjectCommand): name = 'test' def run(self, project, options, **kwargs): assert self.render_template_string('{{project}}') == 'proj' assert self.render_template( 'tests/test_project_template.sh.tmpl').strip() == 'proj' command = FakeCommand() assert command.execute(None, 'options', test='test') == 0
def test_config_calver(self): config_file = fixture_path("config/.bumpit-calver.yaml") config = Configuration.parse(file=config_file) assert config.config_file == config_file assert config.current_version == "201910.1.0" assert config.strategy.name == "calver" assert config.strategy.part == "auto" assert config.strategy.version_format == "YYYYMM.MINOR.MICRO" assert config.tag.apply assert config.tag.format == "{version}" assert config.auto_remote_push is False assert config.tracked_files == ["setup.py", "sample/VERSION"]
def test_config_custom_base_branch(self): config_file = fixture_path("config/.bumpit-custom-base.yaml") config = Configuration.parse(file=config_file) assert config.base_branch == "main" assert config.config_file == config_file assert config.current_version == "0.0.1" assert config.strategy.name == "semver" assert config.strategy.part == "minor" assert config.tag.apply assert config.tag.format == "{version}" assert config.auto_remote_push is False assert config.tracked_files == ["setup.py"]
def test_project_command_runs_with_project_not_faked(): """Test NoEventProjectCommand in a sandbox""" class FakeProjectCommand(NoEventProjectCommand): name = 'test' def run(self, project, options, **kwargs): self.called = True assert project.name == 'sample_project' assert project.env_path().endswith('sample_project/.vs.env') from virtstrap.options import create_base_parser base_parser = create_base_parser() base_options = base_parser.parse_args(args=[]) fake_project_sub_directory = fixture_path('sample_project/lev1/lev2') with in_directory(fake_project_sub_directory): command = FakeProjectCommand() return_code = command.execute(None, base_options) assert return_code == 0 assert_called_flag(command)
def test_command_renders_template_string(): """Test that command renders a template string""" with temp_template_environment(fixture_path('templates')): class FakeCommand(NoEventCommand): name = 'test' def run(self, *args, **kwargs): self.called = True assert self.render_template_string( '{{command.name}}') == 'test' assert self.render_template_string( '{{options}}') == 'options' # Test with a user defined context assert self.render_template_string( '{{testvalue}}', testvalue='foo') == 'foo' command = FakeCommand() return_code = command.execute(None, 'options') assert return_code == 0 assert_called_flag(command)
def test_command_renders_template(): """Test that command renders a template file""" with temp_template_environment(fixture_path('templates')): class FakeCommand(NoEventCommand): name = 'test' def run(self, *args, **kwargs): self.called = True # Test with the default context assert self.render_template( 'tests/test_template.sh.tmpl').strip() == 'test::options' # Test with a user defined context assert self.render_template( 'tests/test_with_context.sh.tmpl', testvalue='bar').strip() == 'bar' command = FakeCommand() return_code = command.execute(None, 'options') assert return_code == 0 assert_called_flag(command)
def test_update(self): files = self._tracked_files() executor = FolderManager(folder=self._tmp_folder, logger=self._logger_spy, dry_run=False) executor.update( current_version=self._current_version, bumped_version=self._bumped_version, files=files, ) assert [ (f"Could not find version {self._current_version} in file '{files[0]}'. " f"Skipping..."), (f"--- before: {files[1]}\n" f"+++ after: {files[1]}\n" f"@@ -1 +1 @@\n" f"-{self._current_version}\n" f"+{self._bumped_version}"), f"Updated file '{files[1]}'.", (f"--- before: {files[2]}\n" f"+++ after: {files[2]}\n" f"@@ -1,4 +1,4 @@\n" f" 14.04.6-LTS\n" f"- {self._current_version}...\n" f"+ {self._bumped_version}...\n" f" and another one\n" f"-{self._current_version}\n" f"+{self._bumped_version}"), f"Updated file '{files[2]}'.", (f"Could not find version {self._current_version} in file '{files[3]}'. " f"Skipping..."), ] == self._logger_spy.messages for index in range(0, 4): expected = self._file_contents( fixture_path(f"executors/after/file{index}")) actual = self._file_contents(files[index]) assert expected == actual
def test_find_project_directory_from_sample_project(): fake_project_sub_directory = fixture_path('sample_project') with in_directory(fake_project_sub_directory): project_dir = find_project_dir() assert project_dir == fixture_path('sample_project')
def test_config_invalid_tag(self): with pytest.raises(ValueError): Configuration.parse( file=fixture_path("config/.bumpit-invalid-tag.yaml"))
from tests import fixture_path def fake_req(name, lock_string=None): lock_string = lock_string or name fake = fudge.Fake() fake.has_attr(name=name, req=name) fake.provides("to_pip_str").returns(lock_string) return fake def test_initialize_locker(): locker = RequirementsLocker() PACKAGES_DIR = fixture_path("packages") def test_initialize_locked_requirement_set(): fake_graph = fudge.Fake() top_level_requirements = [] locked_req_set = LockedRequirementSet(fake_graph, top_level_requirements) @fudge.patch("virtstrap.locker.RequirementsGraphDisplay") def test_display_locked_requirement_set(fake_display): fake_graph = fudge.Fake("RequirementsGraph") top_level_requirements = [] fake_display_func = fudge.Fake() locked_req_set = LockedRequirementSet(fake_graph, top_level_requirements) fake_display.expects("graph_to_str").with_args(
def test_load_from_file(): """Load configuration from a file""" config = VirtstrapConfig.from_file(fixture_path('test.yml')) assert isinstance(config, VirtstrapConfig)
def test_config_missing_fields(self): with pytest.raises(ValueError): Configuration.parse( file=fixture_path("config/.bumpit-missing-fields.yaml"))
from virtstrap.locker import * from tests import fixture_path def fake_req(name, lock_string=None): lock_string = lock_string or name fake = fudge.Fake() fake.has_attr(name=name, req=name) fake.provides('to_pip_str').returns(lock_string) return fake def test_initialize_locker(): locker = RequirementsLocker() PACKAGES_DIR = fixture_path('packages') def test_initialize_locked_requirement_set(): fake_graph = fudge.Fake() top_level_requirements = [] locked_req_set = LockedRequirementSet(fake_graph, top_level_requirements) @fudge.patch('virtstrap.locker.RequirementsGraphDisplay') def test_display_locked_requirement_set(fake_display): fake_graph = fudge.Fake('RequirementsGraph') top_level_requirements = [] fake_display_func = fudge.Fake() locked_req_set = LockedRequirementSet(fake_graph, top_level_requirements) fake_display.expects('graph_to_str').with_args(fake_graph, top_level_requirements,
def test_in_directory(): """Test change CWD to a specific directory""" basic_project_dir = fixture_path('sample_project') with in_directory(basic_project_dir): assert basic_project_dir == os.path.abspath('.') assert basic_project_dir != os.path.abspath('.')
def test_file_system_loader(): loader = FileSystemLoader(fixture_path('templates')) fake_env = fake_environment() template = loader.get_template('tests/test_template.tmpl', fake_env) assert template is not None
def setup(self): template_path = fixture_path('templates') loaders = [FileSystemLoader(template_path)] self.env = TempitaEnvironment(loaders=loaders)
def test_load_from_file_does_not_exist(): """Load a configuration file even if it doesn't exist""" config = VirtstrapConfig.from_file(fixture_path('i_do_not_exist____.yml')) assert isinstance(config, VirtstrapConfig)
import sys import os import fudge from fudge.patcher import patch_object from nose.plugins.attrib import attr from tests import fixture_path from virtstrap import constants from virtstrap.testing import * from virtstrap_local.commands.install import InstallCommand PACKAGES_DIR = fixture_path('packages') def test_initialize_command(): command = InstallCommand() class SpecialFake(fudge.Fake): def __iter__(self): return iter(self.__iter_patch__()) def fake_requirements(names): requirements = [] for name in names: fake = fudge.Fake(name) fake.has_attr(name=name) requirements.append(fake) return requirements def test_special_fake_works(): fake = SpecialFake() fake.expects('__iter_patch__').returns(fake_requirements(['test1'])) looped = False
def test_run(self): run( config=fixture_path("config/.bumpit-lite.yaml"), logger=LoggerSpy(), run_settings=RunSettings(dry_run=True, target_part=None, force_value=None), )