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))
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"]
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)
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"]
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))
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"])
def test_setitem(self): settings = Config(YAML_DATA) settings["sitename"] = "mysite" self.assertEqual(settings["sitename"], "mysite") self.assertEqual(settings["thingy"], ["one", "two", "three"])
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"])
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)
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"])
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))
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")
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())
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())
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])
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"])
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)
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"]
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")
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)])
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())
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)
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)
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()])
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())
def test_no_load(self): settings = Config() self.assertEqual(len(settings), 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)
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"])
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))
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)