Skip to content

Commit

Permalink
Fix all flake8 errors in test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed May 5, 2018
1 parent c059217 commit 3719fc8
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 104 deletions.
2 changes: 1 addition & 1 deletion astrality/tests/actions/test_action_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_retrieving_triggers_from_action_block():
'trigger': [
{'block': 'on_startup'},
{'block': 'on_modified', 'path': 'test.template'},
]
],
}
action_block = ActionBlock(
action_block=action_block_dict,
Expand Down
2 changes: 0 additions & 2 deletions astrality/tests/actions/test_compile_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os
from pathlib import Path

import pytest

from astrality.actions import CompileAction


Expand Down
2 changes: 1 addition & 1 deletion astrality/tests/actions/test_import_context_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_importing_entire_file(context_directory):
'section2': {
'k2_1': 'v2_1',
'k2_2': 'v2_2',
}
},
}
assert context_store == expected_context

Expand Down
10 changes: 9 additions & 1 deletion astrality/tests/actions/test_run_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from astrality.actions import RunAction


def test_null_object_pattern():
"""Null objects should be executable."""
run_action = RunAction(
Expand All @@ -16,6 +17,7 @@ def test_null_object_pattern():
)
run_action.execute()


def test_directory_of_executed_shell_command(tmpdir):
"""All commands should be run from `directory`."""
temp_dir = Path(tmpdir)
Expand All @@ -28,6 +30,7 @@ def test_directory_of_executed_shell_command(tmpdir):
run_action.execute()
assert (temp_dir / 'touched.tmp').is_file()


def test_use_of_replacer(tmpdir):
"""All commands should be run from `directory`."""
temp_dir = Path(tmpdir)
Expand All @@ -41,6 +44,7 @@ def test_use_of_replacer(tmpdir):
assert command == 'echo test'
assert result == 'test'


def test_run_timeout_specified_in_action_block(tmpdir):
"""
Run actions can time out.
Expand All @@ -66,6 +70,7 @@ def test_run_timeout_specified_in_action_block(tmpdir):
_, result = run_action.execute(default_timeout=0)
assert result == 'hi'


def test_run_timeout_specified_in_execute(tmpdir, caplog):
"""
Run actions can time out, and should log this.
Expand Down Expand Up @@ -96,6 +101,7 @@ def test_run_timeout_specified_in_execute(tmpdir, caplog):
_, result = run_action.execute(default_timeout=0.2)
assert result == 'hi'


def test_running_shell_command_with_non_zero_exit_code(caplog):
"""Shell commands with non-zero exit codes should log this."""
run_action = RunAction(
Expand All @@ -109,6 +115,7 @@ def test_running_shell_command_with_non_zero_exit_code(caplog):
assert 'not found' in caplog.record_tuples[1][2]
assert 'non-zero return code' in caplog.record_tuples[2][2]


def test_running_shell_command_with_environment_variable(caplog):
"""Shell commands should have access to the environment."""
run_action = RunAction(
Expand All @@ -130,9 +137,10 @@ def test_running_shell_command_with_environment_variable(caplog):
'astrality.utils',
logging.INFO,
os.environ['USER'] + '\n',
)
),
]


def test_that_environment_variables_are_expanded():
"""String parameters in any Action type should expand env variables."""
run_action = RunAction(
Expand Down
3 changes: 3 additions & 0 deletions astrality/tests/actions/test_trigger_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from astrality.actions import TriggerAction


def test_null_object_pattern():
"""Trigger action should be a dummy when no options are provided."""
trigger_action = TriggerAction(
Expand All @@ -15,6 +16,7 @@ def test_null_object_pattern():
trigger = trigger_action.execute()
assert trigger is None


def test_triggering_non_on_modified_block():
"""
Triggering a startup block should return that block.
Expand All @@ -33,6 +35,7 @@ def test_triggering_non_on_modified_block():
assert trigger.relative_path is None
assert trigger.absolute_path is None


def test_triggering_on_modified_block():
"""
Triggering a on_modified block should return that block and path info.
Expand Down
21 changes: 13 additions & 8 deletions astrality/tests/config/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ def test_cloning_non_existent_github_repository(tmpdir):
github_user_directory = modules_directory / 'jakobgm'
assert not github_user_directory.is_dir()

repository_directory = github_user_directory / 'i-will-never-create-this-repository'
repository_directory = github_user_directory \
/ 'i-will-never-create-this-repository'
assert not repository_directory.is_dir()


@pytest.mark.slow
def test_cloning_two_repositories(tmpdir):
modules_directory = Path(tmpdir.mkdir('modules'))
repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='color-schemes.astrality',
modules_directory=modules_directory,
)
repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='solar-desktop.astrality',
modules_directory=modules_directory,
Expand All @@ -64,14 +65,14 @@ def test_cloning_two_repositories(tmpdir):
@pytest.mark.slow
def test_cloning_one_existent_and_one_non_existent_repo(tmpdir):
modules_directory = Path(tmpdir.mkdir('modules'))
repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='color-schemes.astrality',
modules_directory=modules_directory,
)

with pytest.raises(GithubModuleError):
repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='i-will-never-create-this-repository',
modules_directory=modules_directory,
Expand All @@ -84,16 +85,19 @@ def test_cloning_one_existent_and_one_non_existent_repo(tmpdir):
@pytest.mark.slow
def test_cloning_the_same_repo_twice(tmpdir):
modules_directory = Path(tmpdir.mkdir('modules'))
repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='color-schemes.astrality',
modules_directory=modules_directory,
)

config_file = modules_directory / 'jakobgm' / 'color-schemes.astrality' / 'config.yml'
config_file = modules_directory \
/ 'jakobgm' \
/ 'color-schemes.astrality' \
/ 'config.yml'
config_file.write_text('user edited')

repo_dir = clone_repo(
clone_repo(
user='jakobgm',
repository='color-schemes.astrality',
modules_directory=modules_directory,
Expand All @@ -102,6 +106,7 @@ def test_cloning_the_same_repo_twice(tmpdir):
with open(config_file) as file:
assert file.read() == 'user edited'


@pytest.mark.slow
def test_clone_or_pull_repository_by_updating_outdated_repository(tmpdir):
modules_directory = Path(tmpdir.mkdir('modules'))
Expand Down
2 changes: 1 addition & 1 deletion astrality/tests/config/test_modules_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_processing_of_enabled_statements(
lambda x: tuple(x.items()),
enabled_modules.process_enabling_statements(
enabling_statements=[
{'name': '*::*'}
{'name': '*::*'},
],
modules_directory=test_config_directory / 'freezed_modules',
),
Expand Down
6 changes: 1 addition & 5 deletions astrality/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""Application wide fixtures."""
import copy
import os
from pathlib import Path

import pytest

from astrality.actions import ActionBlock
from astrality.config import (
ASTRALITY_DEFAULT_GLOBAL_SETTINGS,
user_configuration,
)
from astrality.config import user_configuration
from astrality.context import Context
from astrality.module import Module, ModuleManager
from astrality.utils import generate_expanded_env_dict
Expand Down
17 changes: 13 additions & 4 deletions astrality/tests/event_listener/test_daylight.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ def daylight_config():
'elevation': 0,
}


@pytest.fixture
def daylight(daylight_config):
"""A daylight event listener in Trondheim, Norway."""
return Daylight(daylight_config)


# --- Times around dawn ---


@pytest.fixture
def dawn(daylight):
return daylight.construct_astral_location().sun()['dawn']
Expand Down Expand Up @@ -97,10 +101,10 @@ def test_time_left_before_new_event(daylight, before_dusk, freezer):
def test_time_right_before_midnight(daylight, freezer):
"""
This function requires special handling when the UTC time is later than all
daylight events within the same day, which is the case right before midnight.
daylight events within the same day, which is the case right before
midnight.
"""

timezone = daylight.location.timezone
before_midnight = datetime(
year=2019,
month=12,
Expand All @@ -115,6 +119,11 @@ def test_time_right_before_midnight(daylight, freezer):
time_left = daylight.time_until_next_event()
assert 0 < time_left.total_seconds() < 60 * 60 * 24

def test_time_until_night_when_other_periods_are_inbetween(daylight, before_dusk, freezer):

def test_time_until_night_when_other_periods_are_inbetween(
daylight,
before_dusk,
freezer,
):
freezer.move_to(before_dusk - timedelta(hours=6))
assert daylight.time_until_next_event().total_seconds() == 120 + 6*60*60
assert daylight.time_until_next_event().total_seconds() == 120 + 6 * 60 * 60
8 changes: 7 additions & 1 deletion astrality/tests/event_listener/test_event_listener.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

from astrality.event_listener import Periodic, Solar, Static, Weekday, event_listener_factory
from astrality.event_listener import (
Periodic,
Solar,
Static,
Weekday,
event_listener_factory,
)


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions astrality/tests/event_listener/test_periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def test_weekday_time_until_next_event_of_periodic_event_listener(freezer):
now = datetime.now()
freezer.move_to(now)
default_periodic = Periodic({})
assert default_periodic.time_until_next_event().total_seconds() == 60*60
assert default_periodic.time_until_next_event().total_seconds() == 60 * 60

fourty_minutes = timedelta(minutes=40)
freezer.move_to(now + fourty_minutes)
assert default_periodic.time_until_next_event().total_seconds() == 20*60
assert default_periodic.time_until_next_event().total_seconds() == 20 * 60

freezer.move_to(now + 2 * fourty_minutes)
assert default_periodic.time_until_next_event().total_seconds() == 40*60
assert default_periodic.time_until_next_event().total_seconds() == 40 * 60


def test_enumeration_of_periodic_event_listener_events(freezer):
Expand Down
12 changes: 10 additions & 2 deletions astrality/tests/event_listener/test_solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ def solar_config():
'elevation': 0,
}


@pytest.fixture
def solar(solar_config):
"""A solar event listener in Trondheim, Norway."""
return Solar(solar_config)


# --- Times around dawn ---


@pytest.fixture
def dawn(solar):
return solar.construct_astral_location().sun()['dawn']
Expand Down Expand Up @@ -91,19 +95,22 @@ def test_that_sunset_is_correctly_identified_before_dusk(

def test_location(solar):
location = solar.construct_astral_location()
assert str(location) == 'CityNotImportant/RegionIsNotImportantEither, tz=UTC, lat=63.45, lon=10.42'
assert str(location) \
== 'CityNotImportant/RegionIsNotImportantEither, '\
'tz=UTC, lat=63.45, lon=10.42'


def test_time_left_before_new_event(solar, before_dusk, freezer):
freezer.move_to(before_dusk)
assert solar.time_until_next_event().total_seconds() == 120


def test_time_right_before_midnight(solar, freezer):
"""
This function requires special handling when the UTC time is later than all
solar events within the same day, which is the case right before midnight.
"""

timezone = solar.location.timezone
before_midnight = datetime(
year=2019,
month=12,
Expand All @@ -118,6 +125,7 @@ def test_time_right_before_midnight(solar, freezer):
time_left = solar.time_until_next_event()
assert 0 < time_left.total_seconds() < 60 * 60 * 24


def test_config_event_listener_method():
solar_event_listener_application_config = {'type': 'solar'}
solar_event_listener = Solar(solar_event_listener_application_config)
Expand Down

0 comments on commit 3719fc8

Please sign in to comment.