<filename>portals/test/test_yaml_db.py
import os
from pathlib import Path

from pyfakefs.fake_filesystem_unittest import TestCase

from portals import yaml_db


class TestYamlDb(TestCase):
    def setUp(self):
        self.setUpPyfakefs()

    def test_dump(self):
        """After writing a config a file should exist"""
        path_to_config = Path("fred.yaml")
        data = {"some": "stuff"}
        yaml_db.dump("fred.py", data)
        self.assertTrue(path_to_config.exists())
        os.remove(path_to_config)

    def test_dump_load(self):
        """After writing a config it should be readable"""
        path_to_config = Path("fred.yaml")
        expected = {
            "test": [1, 2, 3],
            "fred": [4, 5, [6, 7, 8]],
        }
        yaml_db.dump("fred.py", expected)
        actual = yaml_db.load("fred.py")
        self.assertEqual(actual, expected)
        os.remove(path_to_config)

    def test_indentation(self):
        """A written config should be indented

        Data used in this test should have the lists indented by 2 spaces
            and sub-list by 4 spaces
        """
        path_to_config = Path("fred.yaml")
        data = {
            "test": [1, 2, 3],
            "fred": [4, 5, [6, 7, 8]],
        }
        indented_lines = {
            # key is number of indented spaces
            # value is the line numbers that should have that
            0: {0, 1, 7},
            2: {2, 3, 4, 8, 9, 10},
            4: {5, 6},
        }
        yaml_db.dump("fred.py", data)
        with open(path_to_config) as stream:
            lines = stream.read().splitlines()
            for i, line in enumerate(lines):
                spaces = len(line) - len(line.lstrip())
                self.assertTrue(i in indented_lines[spaces])
        os.remove(path_to_config)

    def test_dump_to_good_path(self):
        """Writing to a existing path should return True"""
        expected = True
        actual = yaml_db.dump("./fred.txt", {"test": "data"})
        self.assertEqual(actual, expected)

    def test_dump_to_bad_path(self):
        """Writing to a missing directory should return False"""
        expected = False
        actual = yaml_db.dump("/path/to/nowhere/fred.py", {"test": "data"})
        self.assertEqual(actual, expected)

<filename>portals/test/test_yaml_db.py
import os
from pathlib import Path

from pyfakefs.fake_filesystem_unittest import TestCase

from portals import yaml_db


class TestYamlDb(TestCase):
    def setUp(self):
        self.setUpPyfakefs()

    def test_dump(self):
        """After writing a config a file should exist"""
        path_to_config = Path("fred.yaml")
        data = {"some": "stuff"}
        yaml_db.dump("fred.py", data)
        self.assertTrue(path_to_config.exists())
        os.remove(path_to_config)

    def test_dump_load(self):
        """After writing a config it should be readable"""
        path_to_config = Path("fred.yaml")
        expected = {
            "test": [1, 2, 3],
            "fred": [4, 5, [6, 7, 8]],
        }
        yaml_db.dump("fred.py", expected)
        actual = yaml_db.load("fred.py")
        self.assertEqual(actual, expected)
        os.remove(path_to_config)

    def test_indentation(self):
        """A written config should be indented

        Data used in this test should have the lists indented by 2 spaces
            and sub-list by 4 spaces
        """
        path_to_config = Path("fred.yaml")
        data = {
            "test": [1, 2, 3],
            "fred": [4, 5, [6, 7, 8]],
        }
        indented_lines = {
            # key is number of indented spaces
            # value is the line numbers that should have that
            0: {0, 1, 7},
            2: {2, 3, 4, 8, 9, 10},
            4: {5, 6},
        }
        yaml_db.dump("fred.py", data)
        with open(path_to_config) as stream:
            lines = stream.read().splitlines()
            for i, line in enumerate(lines):
                spaces = len(line) - len(line.lstrip())
                self.assertTrue(i in indented_lines[spaces])
        os.remove(path_to_config)

    def test_dump_to_good_path(self):
        """Writing to a existing path should return True"""
        expected = True
        actual = yaml_db.dump("./fred.txt", {"test": "data"})
        self.assertEqual(actual, expected)

    def test_dump_to_bad_path(self):
        """Writing to a missing directory should return False"""
        expected = False
        actual = yaml_db.dump("/path/to/nowhere/fred.py", {"test": "data"})
        self.assertEqual(actual, expected)

After writing a config a file should exist After writing a config it should be readable A written config should be indented          Data used in this test should have the lists indented by 2 spaces             and sub-list by 4 spaces # key is number of indented spaces # value is the line numbers that should have that Writing to a existing path should return True Writing to a missing directory should return False