예제 #1
0
    def test_KeyError_contains_name_of_child(self):
        node = mock.Mock()
        node.full_path = "this-file.html"
        config = Config({}, parent=Config({}), node=node)

        with self.assertRaises(KeyError) as exp:
            config["some key"]

        self.assertIn("this-file.html", str(exp.exception))
예제 #2
0
    def test_getitem(self):
        settings = Config(YAML_DATA)

        self.assertEqual(settings["sitename"], "bob")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])

        self.assertEqual(settings.get("sitename"), "bob")
        self.assertEqual(settings.get("sitetitle"), None)

        with self.assertRaises(KeyError):
            settings["sitetitle"]
예제 #3
0
    def test_contains_with_parent(self):
        parent = Config({"test": True})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        self.assertIn("test", settings)
        self.assertIn("sitename", settings)
        self.assertIn("thingy", settings)

        self.assertIn("test", parent)
        self.assertNotIn("sitename", parent)
        self.assertNotIn("thingy", parent)
예제 #4
0
    def test_setitem_with_parent(self):
        parent = Config({"test": True})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        settings["sitename"] = "mysite"
        settings["test"] = False

        self.assertEqual(settings["sitename"], "mysite")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
        self.assertEqual(settings["test"], False)
        self.assertEqual(parent["test"], True)

        with self.assertRaises(KeyError):
            parent["sitename"]
예제 #5
0
    def test_KeyError_contains_name_of_site_yaml(self):
        config = Config({})

        with self.assertRaises(KeyError) as exp:
            config["some key"]

        self.assertIn(SITE_YAML_PATH, str(exp.exception))
예제 #6
0
    def test_load_file(self):
        yaml_file = StringIO(YAML_DATA)
        settings = Config(yaml_file)

        self.assertEqual(len(settings), 2)
        self.assertEqual(settings["sitename"], "bob")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
예제 #7
0
    def test_setitem(self):
        settings = Config(YAML_DATA)

        settings["sitename"] = "mysite"

        self.assertEqual(settings["sitename"], "mysite")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
예제 #8
0
    def test_load_dict(self):
        yaml_dict = dict(YAML(typ="safe").load(YAML_DATA))
        settings = Config(yaml_dict)

        self.assertEqual(len(settings), 2)
        self.assertEqual(settings["sitename"], "bob")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
예제 #9
0
    def test_stripped_extension_not_exists(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        self.get_server(settings)

        self.client.request("GET", "/unknown")
        response = self.client.getresponse()
        self.assertEqual(response.status, 404)
예제 #10
0
    def test_from_path(self):
        with NamedTemporaryFile() as yaml_file:
            yaml_file.write(YAML_DATA.encode())
            yaml_file.file.flush()  # just to be sure

            settings = Config.from_path(yaml_file.name)

            self.assertEqual(len(settings), 2)
            self.assertEqual(settings["sitename"], "bob")
            self.assertEqual(settings["thingy"], ["one", "two", "three"])
예제 #11
0
    def test_clean_deploy_dir(self):
        with TemporaryDirectory() as deploy, TemporaryDirectory() as content:
            settings = Config({"deploy_path": deploy, "content_path": content})
            old_file = os.path.join(deploy, "someoldfile")
            with open(old_file, "w") as of:
                of.write("content!")

            # file exists and then is deleted during site generation
            self.assertTrue(os.path.exists(old_file))
            gen(settings)
            self.assertFalse(os.path.exists(old_file))
예제 #12
0
    def test_404(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        self.get_server(settings)

        self.client.request("GET", "/not-existing.html")
        response = self.client.getresponse()
        self.assertEqual(response.status, 404)
        headers = dict(response.getheaders())
        self.assertEqual(headers["Cache-Control"], "no-store")
예제 #13
0
    def test_stripped_extension_with_extension(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        self.get_server(settings)

        self.client.request("GET", "/page.html")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
        content = response.read()
        self.assertEqual(content, PAGE_CONTENTS.encode())
예제 #14
0
    def test_dir_index(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        self.get_server(settings)

        self.client.request("GET", "/blog/")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
        content = response.read()
        self.assertEqual(content, BLOG_INDEX_CONTENTS.encode())
예제 #15
0
    def test_values_with_parent(self):
        parent = Config({"test": True})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        self.assertCountEqual(list(settings.values()),
                              ["bob", ["one", "two", "three"], True])
        self.assertCountEqual(list(parent.values()), [True])
예제 #16
0
    def test_iter_with_parent(self):
        parent = Config({"test": True, "sitename": "me"})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        self.assertCountEqual(list(settings.keys()),
                              ["sitename", "thingy", "test"])
        self.assertCountEqual(list(parent.keys()), ["test", "sitename"])
예제 #17
0
    def test_dir_without_index(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        index = pathlib.Path(self.blog_dir, "index.html")
        index.unlink()
        self.assertFalse(index.exists())
        self.get_server(settings)

        self.client.request("GET", "/blog/")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
예제 #18
0
    def test_getitem_with_parent(self):
        parent = Config({"test": True})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        self.assertEqual(settings["sitename"], "bob")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
        self.assertEqual(settings["test"], True)

        self.assertEqual(settings.get("sitename"), "bob")
        self.assertEqual(settings.get("test"), True)
        self.assertEqual(settings.get("sitetitle"), None)
        self.assertEqual(parent.get("sitename"), None)

        with self.assertRaises(KeyError):
            settings["sitetitle"]
예제 #19
0
    def test_fetch_file(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name
        })
        self.get_server(settings)

        self.client.request("GET", "/style.css")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
        content = response.read()
        self.assertEqual(content, CSS_CONTENTS.encode())
        headers = dict(response.getheaders())
        self.assertEqual(headers["Cache-Control"], "no-store")
예제 #20
0
    def test_items_with_parent(self):
        parent = Config({"test": True})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())

        self.assertCountEqual(list(settings.items()),
                              [("sitename", "bob"),
                               ("thingy", ["one", "two", "three"]),
                               ("test", True)])
        self.assertCountEqual(list(parent.items()), [("test", True)])
예제 #21
0
    def test_parent_and_node(self):
        with self.assertRaises(AssertionError):
            Config({}, parent=Config({}))

        with self.assertRaises(AssertionError):
            Config({}, node=mock.Mock())

        # these should be fine
        Config({})
        Config({}, parent=Config({}), node=mock.Mock())
예제 #22
0
    def test_fetch_file_with_prefix(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name,
            "base_url": "/bob/"
        })
        self.get_server(settings)

        self.client.request("GET", "/bob/style.css")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
        content = response.read()
        self.assertEqual(content, CSS_CONTENTS.encode())

        self.client.request("GET", "/style.css")
        response = self.client.getresponse()
        self.assertEqual(response.status, 404)
예제 #23
0
    def test_custom_stripped_extension(self):
        settings = Config({
            "deploy_path": self.tmp_dir.name,
            "content_path": self.tmp_dir.name,
            "strip_exts": ".css"
        })
        self.get_server(settings)

        self.client.request("GET", "/style")
        response = self.client.getresponse()
        self.assertEqual(response.status, 200)
        content = response.read()
        self.assertEqual(content, CSS_CONTENTS.encode())

        self.client.request("GET", "/page")
        response = self.client.getresponse()
        self.assertEqual(response.status, 404)
예제 #24
0
    def test_copy(self):
        parent = Config({})
        settings = Config(YAML_DATA, parent=parent, node=mock.Mock())
        copied = settings.copy()

        self.assertNotEqual(settings, copied)
        self.assertEqual([i for i in settings.keys()],
                         [i for i in copied.keys()])
        self.assertEqual(settings.parent, copied.parent)

        settings["bob"] = "hello"
        self.assertNotEqual([i for i in settings.keys()],
                            [i for i in copied.keys()])
예제 #25
0
    def test_walk_nodes(self):
        files = [
            "blog/index.html",
            "blog/post.html",
            "index.html",
            "style.css",
        ]
        dirs = ["blog"]
        with TemporaryDirectory() as deploy, TemporaryDirectory() as content:
            settings = Config({"deploy_path": deploy, "content_path": content})
            for d in dirs:
                pathlib.Path(content, d).mkdir()

            for f in files:
                pathlib.Path(content, f).touch()

            gen(settings)

            for item in files + dirs:
                with self.subTest("%s exists in %s" % (item, deploy)):
                    self.assertTrue(pathlib.Path(deploy, item).exists())
예제 #26
0
 def test_no_load(self):
     settings = Config()
     self.assertEqual(len(settings), 0)
예제 #27
0
 def test_load_AssertionError(self):
     # raise an AssertionError if it's not a dict or file-like object
     with self.assertRaises(AssertionError):
         Config(12)
예제 #28
0
    def test_load_str(self):
        settings = Config(YAML_DATA)

        self.assertEqual(len(settings), 2)
        self.assertEqual(settings["sitename"], "bob")
        self.assertEqual(settings["thingy"], ["one", "two", "three"])
예제 #29
0
 def test_repr(self):
     settings = Config({})
     # just a smoke test to make sure it doesn't blow up
     self.assertTrue(isinstance(repr(settings), str))
     self.assertTrue(repr(settings))
예제 #30
0
 def test_get_name_for_node_config(self):
     node = mock.Mock()
     node.full_path = "this-file.html"
     config = Config({}, parent=mock.Mock(), node=node)
     self.assertEqual(config.get_name(), node.full_path)