Skip to content

Commit

Permalink
fix: use contextmanager db initialize in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
codito committed Jan 16, 2018
1 parent 36603ca commit d6f3845
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import blinker
import sure
import tempfile
from contextlib import contextmanager
from peewee import SqliteDatabase
from pyfakefs import fake_filesystem

Expand Down Expand Up @@ -135,13 +137,14 @@ def test_default_hooks(self):
pomito.exit()

def test_initialize_creates_database_if_not_present(self):
pomito = self.main.Pomito()
self._setup_pomito_plugins(pomito)
with self._setup_data_dir():
pomito = self.main.Pomito()
self._setup_pomito_plugins(pomito)

pomito.initialize()
pomito.initialize()

pomito.get_db().shouldnt.be.equal(None)
pomito.exit()
pomito.get_db().shouldnt.be.equal(None)
pomito.exit()

def test_initialize_uses_injected_database(self):
dummy_db = SqliteDatabase(':memory:')
Expand All @@ -154,16 +157,17 @@ def test_initialize_uses_injected_database(self):
pomito.exit()

def test_initialize_setup_plugins_and_hooks(self):
pomito = self.main.Pomito()
self._setup_pomito_plugins(pomito)
self._setup_pomito_hooks(pomito)
with self._setup_data_dir():
pomito = self.main.Pomito()
self._setup_pomito_plugins(pomito)
self._setup_pomito_hooks(pomito)

pomito.initialize()
pomito.initialize()

pomito.ui_plugin.initialize.call_count.should.be.equal(1)
pomito.task_plugin.initialize.call_count.should.be.equal(1)
pomito._hooks[0].initialize.call_count.should.be.equal(1)
pomito.exit()
pomito.ui_plugin.initialize.call_count.should.be.equal(1)
pomito.task_plugin.initialize.call_count.should.be.equal(1)
pomito._hooks[0].initialize.call_count.should.be.equal(1)
pomito.exit()

def test_get_parser_returns_a_configparser_with_config_data(self):
pass
Expand All @@ -179,3 +183,11 @@ def _setup_pomito_plugins(self, pomito):

def _setup_pomito_hooks(self, pomito):
pomito._hooks[0] = Mock(spec=Hook)

@contextmanager
def _setup_data_dir(self):
with tempfile.TemporaryDirectory() as tmpdir:
data_dir = self.main.DATA_DIR
self.main.DATA_DIR = tmpdir
yield
self.main.DATA_DIR = data_dir

0 comments on commit d6f3845

Please sign in to comment.