Skip to content

Commit

Permalink
Replace TmpDir helper with pytest fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
replaceafill committed Nov 22, 2023
1 parent f122430 commit a69a706
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 335 deletions.
49 changes: 26 additions & 23 deletions tests/test_aips_models.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
#!/usr/bin/env python
import os
import unittest

import pytest

from aips import models
from tests.tests_helpers import TmpDir

THIS_DIR = os.path.abspath(os.path.dirname(__file__))
TMP_DIR = os.path.join(THIS_DIR, ".tmp-aips-models")
DATABASE_FILE = os.path.join(TMP_DIR, "aips.db")


class TestAipsModels(unittest.TestCase):
def test_init_success(self):
"""Test that the database, table and session are created."""
assert not os.path.isfile(DATABASE_FILE)
assert not hasattr(models, "Session")
with TmpDir(TMP_DIR):
session = models.init(DATABASE_FILE)
assert os.path.isfile(DATABASE_FILE)
assert "aip" in models.Base.metadata.tables
assert hasattr(session, "add")
assert callable(session.add)

def test_init_fail(self):
"""Test that the database can't be created in a wrong path."""
self.assertRaises(IOError, models.init, "/this/should/be/a/wrong/path/to.db")


def test_init_success(tmp_path):
"""Test that the database, table and session are created."""
tmp_dir = tmp_path / "dir"
tmp_dir.mkdir()

DATABASE_FILE = (tmp_dir / "aips.db").as_posix()

assert not os.path.isfile(DATABASE_FILE)
assert not hasattr(models, "Session")

session = models.init(DATABASE_FILE)

assert os.path.isfile(DATABASE_FILE)
assert "aip" in models.Base.metadata.tables
assert hasattr(session, "add")
assert callable(session.add)


def test_init_fail():
"""Test that the database can't be created in a wrong path."""
with pytest.raises(IOError):
models.init("/this/should/be/a/wrong/path/to.db")
Loading

0 comments on commit a69a706

Please sign in to comment.