def test_Post_eq(self): """Two distinct Posts with same attrs compare equal And also that two Posts with differing dates compare unequal: to check that Post is not just relying on its superclass's implementation. The overriding of the post path template is so that all the posts have the same output_path/url and we can be sure that the date itself is being compared. """ new_path = '{content.date.year}/{content.slug}' self.settings['paths']['post path template'] = new_path post_a = Post(title=self.title, body=self.body, settings=self.settings, date=self.naive_date) post_b = Post(title=self.title, body=self.body, settings=self.settings, date=self.naive_date) post_c = Post(title=self.title, body=self.body, settings=self.settings, date=datetime(2015, 1, 1)) self.assertEqual(post_a, post_b) self.assertNotEqual(post_a, post_c)
def test_explicit_draft(self): """.from_file raises DraftError given a file with 'draft' in meta 'draft' should appear (without quotes) on a line by itself """ with self.assertRaises(majestic.DraftError): Post.from_file( file=self.posts_path.joinpath('test_explicit_draft.md'), settings=self.settings)
def test_post_compare_lt_dates(self): """Posts with different dates compare properly""" post_1 = Post(title=self.title, body=self.body, settings=self.settings, date=datetime(2015, 1, 1)) post_2 = Post(title=self.title, body=self.body, settings=self.settings, date=datetime(2014, 1, 1)) self.assertLess(post_2, post_1)
def test_post_compare_lt_identical_dates(self): """Posts with the same date but different titles compare properly The Post class should only test the dates, and delegate title and slug comparison to its superclass. """ post_1 = Post(title='title a', body=self.body, date=self.naive_date, settings=self.settings) post_2 = Post(title='title B', body=self.body, date=self.naive_date, settings=self.settings) self.assertLess(post_1, post_2)
def test_post_future_date_raises_DraftPost(self): """Initialising a Post with a future date raises DraftError""" with self.assertRaises(majestic.DraftError): Post(title=self.title, body=self.body, settings=self.settings, date=datetime(2100, 1, 1))
def test_date_has_timezone(self): """Post correctly localizes the provided date""" post = Post(title=self.title, body=self.body, date=self.naive_date, settings=self.settings) self.assertEqual(post.date, self.aware_date)
def test_post_init_date(self): """Post stores provided date as self.date""" post = Post(title=self.title, body=self.body, date=self.naive_date, settings=self.settings) self.assertEqual(self.aware_date, post.date)
def test_post_inheritance(self): """Post instances are also an instance of Content""" post = Post(title=self.title, body=self.body, date=self.naive_date, settings=self.settings) self.assertTrue(isinstance(post, Content))
def test_parse_known_good_slug(self): """.from_file does not normalise known good slug""" known_good_file = self.posts_path.joinpath('test_good_slug.md') known_good_slug = 'valid%20slug' post = Post.from_file(known_good_file, settings=self.settings) self.assertTrue(post.slug, known_good_slug)
def test_post_init_date_string(self): """If given a str for date, Post parses it into a datetime object""" self.settings['dates']['format'] = '%Y-%m-%d %H:%M' post = Post(title=self.title, body=self.body, date=self.date_string, settings=self.settings) self.assertEqual(self.aware_date, post.date)
def setUp(self): settings_path = TEST_BLOG_DIR.joinpath('settings.json') self.settings = load_settings(files=[settings_path], local=False) starting_date = datetime(2015, 9, 22, 19) self.posts = [ Post(title='post {}'.format(i), body='Here’s some text!', date=starting_date - timedelta(i), settings=self.settings) for i in range(40) ] random.shuffle(self.posts) # Ensure not sorted
def test_parse_known_bad_slug(self): """.from_file detects and normalises invalid slugs When given a file containing an invalid value for the slug, .from_file should return a content object where the slug has been normalised. """ known_bad_file = self.posts_path.joinpath('test_invalid_slug.md') good_chars = set(string.ascii_lowercase + string.digits + '-') post = Post.from_file(known_bad_file, settings=self.settings) self.assertLess(set(post.slug), good_chars) # Subset test
def setUp(self): os.chdir(str(TEST_BLOG_DIR)) self.settings = load_settings() ext_dir_name = self.settings['paths']['extensions root'] self.ext_dir = TEST_BLOG_DIR.joinpath(ext_dir_name) self.posts = [ Post(title='test', body='test', date=datetime.now(), settings=self.settings) ] self.pages = [Page(title='test', body='test', settings=self.settings)]
def setUp(self): settings_path = TEST_BLOG_DIR.joinpath('settings.json') self.settings = load_settings(files=[settings_path], local=False) self.settings['index']['posts per page'] = 2 path_template = 'page-{content.page_number}.html' self.settings['paths']['index pages path template'] = path_template self.settings['site']['url'] = 'http://example.com' dates = [datetime(2015, 1, 1) + timedelta(i) for i in range(5)] titles = ['A', 'B', 'C', 'D', 'E'] bodies = ['A', 'B', 'C', 'D', 'E'] self.posts = [ Post(title=t, body=b, date=d, settings=self.settings) for t, b, d in zip(titles, bodies, dates) ]
def setUp(self): os.chdir(TEST_BLOG_DIR) settings_path = TEST_BLOG_DIR.joinpath('settings.json') self.settings = load_settings(files=[settings_path], local=False) self.output_dir = Path(self.settings['paths']['output root']) self.number_of_posts = 5 self.settings['feeds']['number of posts'] = self.number_of_posts starting_date = datetime(2015, 9, 22, 19) self.posts = [ Post(title='post {}'.format(i), body='Here’s some text! And [a relative URL](/about)', date=starting_date - timedelta(i), settings=self.settings) for i in range(40) ] random.shuffle(self.posts) # Ensure not sorted
def test_posts_general(self): """Posts returned with file's contents correctly stored This test tests the five liberation day posts, starting: 1917-11-07 1949-10-01 1959-01-01 1975-04-30 1979-07-19 The parsing rules should differentiate between the metadata and body. The metadata is all the lines between the start of the file and a blank line. The body is everything following the blank line. Of the metadata in the header, the title, slug and date should be available as attributes. The date should be a datetime object corresponding to the textual date in the metadata header. All other metadata should be available in a dictionary stored on the post as the meta attribute. The keys in that dictionary should be lower-case and stripped of leading and trailing whitespace. The values should be stripped only. The body should be stripped of leading and trailing newlines only. """ date_format = self.settings['dates']['format'] for file in self.lib_posts: with file.open() as f: meta, body = f.read().split('\n\n', maxsplit=1) meta = [line.split(':', maxsplit=1) for line in meta.splitlines()] meta = {key.lower().strip(): value.strip() for key, value in meta} file_dict = meta file_dict['body'] = body.strip('\n') file_dict['source_path'] = file post = Post.from_file(file, settings=self.settings) post_dict = post.meta.copy() post_dict['title'] = post.title post_dict['slug'] = post.slug post_dict['date'] = post.date.strftime(date_format) post_dict['body'] = post.body post_dict['source_path'] = post.source_path self.assertEqual(file_dict, post_dict)
def setUp(self): os.chdir(TEST_BLOG_DIR) settings_path = TEST_BLOG_DIR.joinpath('settings.json') self.settings = load_settings(files=[settings_path], local=False) self.output_dir = Path(self.settings['paths']['output root']) self.files = [ Post(title='', slug='post', date=datetime(2015, 1, 1), body='', settings=self.settings), Page(title='', slug='page', body='', settings=self.settings), Index(posts=[], settings=self.settings, page_number=1), ] # Make dummy files and directories for f in self.files: f.output_path.parent.mkdir(parents=True, exist_ok=True) f.output_path.touch()
def test_post_output_path_and_url(self): """Post defines output_path and url properties Output path should be a pathlib.Path object, url a str """ post = Post(title=self.title, body=self.body, settings=self.settings, date=self.naive_date) path_template = self.settings['paths']['post path template'] path = path_template.format(content=post) output_dir = self.settings['paths']['output root'] site_url = self.settings['site']['url'] expected_output = Path(output_dir, path) expected_url = site_url + '/' + path self.assertEqual(expected_output, post.output_path) self.assertEqual(expected_url, post.url)
def test_parse_bad_percent_encoding(self): """.from_file normalises slugs containing invalid percent encoding""" bad_percent_file = self.posts_path.joinpath('test_bad_percent.md') bad_percent_slug = 'this-is-not-100%-valid' post = Post.from_file(bad_percent_file, settings=self.settings) self.assertNotEqual(post.slug, bad_percent_slug)