Skip to content

Commit

Permalink
Unit tests added to test_replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
anbeemer committed Jul 16, 2024
1 parent 9b777fd commit 32be458
Showing 1 changed file with 110 additions and 3 deletions.
113 changes: 110 additions & 3 deletions test/test_replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
sys.path.insert(0,'../')
import markdown2canvas as mc

import json

import pytest


@pytest.fixture(scope='class')
def course():
import os
Expand All @@ -17,29 +20,133 @@ def course():


@pytest.fixture(scope='class')
def page_using_defaults(course):
def page_using_defaults():
import os
folder = 'uses_replacements_default'

yield mc.Page(folder)



@pytest.fixture(scope='class')
def page_using_custom(course):
def page_using_custom():
import os
folder = 'uses_replacements_custom'

yield mc.Page(folder)


@pytest.fixture(scope='class')
def default_filename():
with open('_course_metadata/defaults.json', "r", encoding="utf-8") as f:
defaults = f.read()
yield json.loads(defaults)['replacements']

@pytest.fixture(scope='class')
def replacements_default(default_filename):
with open(default_filename, "r", encoding="utf-8") as f:
yield f.read()

@pytest.fixture(scope='class')
def uses_defaults_source():
with open('uses_replacements_default/source.md', "r", encoding="utf-8") as f:
yield f.read()

# @pytest.fixture(scope='class')
# def html_using_defaults():
# with open('uses_replacements_default/result.html', "r", encoding="utf-8") as f:
# yield f.read()

@pytest.fixture(scope='class')
def html_using_defaults(course):
a = course.get_pages(search_term = 'Test replacements using default replacements file')[0]
rev = a.show_latest_revision()
yield rev.body


@pytest.fixture(scope='class')
def replacements_custom():
with open('_course_metadata/replacements2.json', "r", encoding="utf-8") as f:
yield f.read()

@pytest.fixture(scope='class')
def uses_custom_source():
with open('uses_replacements_custom/source.md', "r", encoding="utf-8") as f:
yield f.read()

# @pytest.fixture(scope='class')
# def html_using_custom():
# with open('uses_replacements_custom/result.html', "r", encoding="utf-8") as f:
# yield f.read()

@pytest.fixture(scope='class')
def html_using_custom(course):
a = course.get_pages(search_term = 'Test replacements with custom replacements file')[0]
rev = a.show_latest_revision()
yield rev.body





class TestPage():

def test_can_publish(self, course, page_using_defaults, page_using_custom):
page_using_defaults.publish(course,overwrite=True)
page_using_custom.publish(course,overwrite=True)


##Removed a " as e_info" after the def in the following... doesn't seem to have hurt it?
def test_get_default_replacements_name(self):
path = mc.get_default_replacements_name()
assert path == '_course_metadata/replacements.json'


def test_removed_default(self, html_using_defaults, replacements_default, uses_defaults_source):
replacements_dict_default = json.loads(replacements_default)
for key in replacements_dict_default:
if key in uses_defaults_source:
assert key not in html_using_defaults
#Want to add something about the new thing being in the html
#assert replacements_dict_default[key] in html_using_defaults

def test_replaced_default(self, html_using_defaults):
#default replacements that should translate seamlessly
assert 'with this text' in html_using_defaults
assert 'destination_without_spaces' in html_using_defaults
#check specific video options
assert '560' in html_using_defaults
assert '315' in html_using_defaults
assert 'https://www.youtube.com/embed/dQw4w9WgXcQ?si=BqTm4nbZOLTHaxnz' in html_using_defaults
assert 'YouTube video player' in html_using_defaults
assert 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share' in html_using_defaults
assert 'allowfullscreen' in html_using_defaults

def test_removed_custom(self, html_using_custom, uses_custom_source, replacements_custom):
replacements_dict_custom = json.loads(replacements_custom)
for key in replacements_dict_custom:
if key in uses_custom_source:
assert key not in html_using_custom
assert replacements_dict_custom[key] in html_using_custom

def test_replaced_custom(self, html_using_custom):
#custom replacements that should translate seamlessly
assert 'target custom replacement without space' in html_using_custom
assert 'target custom replacement from nospace' in html_using_custom

def test_incorrect_replacement_custom(self, html_using_custom):
#First check that none of the default replacements show up in the custom replacements file
assert 'with this text' not in html_using_custom
assert 'destination_without_spaces' not in html_using_custom
assert 'https://www.youtube.com/embed/dQw4w9WgXcQ?si=BqTm4nbZOLTHaxnz' not in html_using_custom


def test_incorrect_replacement_default(self, html_using_defaults):
#First check that none of the default replacements show up in the custom replacements file
assert 'target custom replacement without space' not in html_using_defaults
assert 'target custom replacement from nospace' not in html_using_defaults



def test_missing_replacements(self):
# constructing a page with a replacements file that doesn't exist should raise
with pytest.raises(FileNotFoundError):
Expand Down

0 comments on commit 32be458

Please sign in to comment.