def test_no_courses(): """Loading directory with no courses succeeds, but gives no course """ model = models.Root() model.load_local_courses(fixture_path / 'empty-lessons-dir') assert sorted(model.courses) == []
def init_model(): return models.Root( url_factories={ 'api': { models.Root: lambda **kw: url_for('api', **kw), models.Course: lambda **kw: url_for('course_api', **kw), models.RunYear: lambda **kw: url_for('run_year_api', **kw), }, 'web': { models.Lesson: lambda **kw: url_for('page', page_slug='index', **kw), models.Page: lambda **kw: url_for('page', **kw), models.Solution: lambda **kw: url_for('solution', **kw), models.Course: lambda **kw: url_for('course', **kw), models.Session: lambda **kw: url_for('session', **kw), models.SessionPage: lambda **kw: url_for( 'session', **kw), models.StaticFile: lambda **kw: url_for('page_static', **kw), models.Root: lambda **kw: url_for('index', **kw) }, }, schema_url_factory=lambda m, is_input, **kw: url_for( 'schema', model_slug=m.model_slug, is_input=is_input, **kw), )
def test_load_empty_dir(): model = models.Root() # Collections are available but empty assert not model.courses assert not model.run_years assert not model.licenses
def test_empty_model(): model = models.Root() assert not model.courses assert not model.licenses assert not model.run_years assert model.get_pks() == {} with pytest.raises(models.NoURL): model.get_url()
def test_no_courses(): """Loading directory with no courses gives only an empty "lessons" course """ model = models.Root() model.load_local_courses(fixture_path / 'empty-lessons-dir') assert sorted(model.courses) == ['lessons'] assert not model.courses['lessons'].sessions assert not model.courses['lessons'].lessons
def make_model(**extra_kwargs): """Return a set-up testing model, possibly with additional init kwargs""" model = models.Root( schema_url_factory=dummy_schema_url_factory, url_factories={ 'api': DummyURLFactories('api'), 'web': DummyURLFactories('web'), }, **extra_kwargs, ) model.load_licenses(fixture_path / 'licenses') return model
def test_add_course_from_data(): model = models.Root() add_test_course(model, 'courses/minimal', { 'title': 'A minimal course', 'sessions': [], }) assert sorted(model.courses) == ['courses/minimal'] assert model.courses['courses/minimal'].title == 'A minimal course' assert model.courses['courses/minimal'].slug == 'courses/minimal'
def init_model(): trusted = os.environ.get('NAUCSE_TRUSTED_REPOS', None) if trusted is None: trusted_repo_patterns = () else: trusted_repo_patterns = tuple(line for line in trusted.split() if line) return models.Root( url_factories={ 'api': { models.Root: lambda **kw: url_for('api', **kw), models.Course: lambda **kw: url_for('course_api', **kw), models.RunYear: lambda **kw: url_for('run_year_api', **kw), }, 'web': { models.Lesson: lambda **kw: url_for('page', page_slug='index', **kw), models.Page: lambda **kw: url_for('page', **kw), models.Solution: lambda **kw: url_for('solution', **kw), models.Course: lambda **kw: url_for('course', **kw), models.Session: lambda **kw: url_for('session', **kw), models.SessionPage: lambda **kw: url_for('session', **kw), models.StaticFile: lambda **kw: url_for('page_static', **kw), models.Root: lambda **kw: url_for('index', **kw) }, }, schema_url_factory=lambda m, is_input, **kw: url_for( 'schema', model_slug=m.model_slug, is_input=is_input, **kw), arca=Arca( settings={ "ARCA_BACKEND": "arca.backend.CurrentEnvironmentBackend", "ARCA_BACKEND_CURRENT_ENVIRONMENT_REQUIREMENTS": "requirements.txt", "ARCA_BACKEND_VERBOSITY": 2, "ARCA_BACKEND_KEEP_CONTAINER_RUNNING": True, "ARCA_BACKEND_USE_REGISTRY_NAME": "docker.io/naucse/naucse.python.cz", "ARCA_SINGLE_PULL": True, "ARCA_IGNORE_CACHE_ERRORS": True, "ARCA_CACHE_BACKEND": "dogpile.cache.dbm", "ARCA_CACHE_BACKEND_ARGUMENTS": { "filename": ".arca/cache/naucse.dbm" }, "ARCA_BASE_DIR": str(Path('.arca').resolve()), }), trusted_repo_patterns=trusted_repo_patterns, )
def test_licenses(): model = models.Root() model.load_licenses(fixture_path / 'licenses') assert sorted(model.licenses) == ['cc-by-sa-40', 'cc0'] assert model.licenses['cc0'].slug == 'cc0' assert model.licenses['cc0'].url.endswith('/publicdomain/zero/1.0/') assert model.licenses['cc0'].title.endswith('Public Domain Dedication') assert model.licenses['cc-by-sa-40'].slug == 'cc-by-sa-40' assert model.licenses['cc-by-sa-40'].url.endswith('/licenses/by-sa/4.0/') assert model.licenses['cc-by-sa-40'].title.endswith('4.0 International')
def test_add_local_course(): model = models.Root() path = fixture_path / 'minimal-courses' model.add_course(models.Course.load_local( parent=model, path=path, repo_info=get_local_repo_info(path), slug='courses/minimal', )) assert sorted(model.courses) == ['courses/minimal'] assert model.courses['courses/minimal'].title == 'A minimal course' assert model.courses['courses/minimal'].slug == 'courses/minimal'
def test_load_courses(): model = models.Root() model.load_local_courses(fixture_path / 'minimal-courses') assert sorted(model.courses) == [ '2019/minimal', 'courses/minimal', 'lessons', ] assert model.courses['courses/minimal'].title == 'A minimal course' assert model.courses['courses/minimal'].slug == 'courses/minimal' assert model.courses['2019/minimal'].title == 'A minimal course' assert model.courses['2019/minimal'].slug == '2019/minimal' assert model.courses['lessons'].title == 'Kanonické lekce' assert model.courses['lessons'].slug == 'lessons'
def model(): """Return the root of the naucse model In debug mode (elsa serve), a new model is returned for each requests, so changes are picked up. In non-debug mode (elsa freeze), a single model is used (and stored in _cached_model), so that metadata is only read once. """ global _cached_model if _cached_model: return _cached_model model = models.Root(os.path.join(app.root_path, '..')) if not app.config['DEBUG']: _cached_model = model return model
def test_from_renderer(): model = models.Root() path = fixture_path / 'minimal-courses' renderer = LocalRenderer( path=path, repo_info=get_local_repo_info(path), slug='courses/minimal', ) model.add_course( models.Course.from_renderer( parent=model, renderer=renderer, )) assert sorted(model.courses) == ['courses/minimal'] assert model.courses['courses/minimal'].title == 'A minimal course' assert model.courses['courses/minimal'].slug == 'courses/minimal'
def model(fork): """Generate a Root instance with the courses and runs generated in ``fork`` """ path = Path(__file__).parent / 'fixtures/test_content' root = models.Root(path) course = models.CourseLink(root, path / 'courses/test-course') course.repo = fork course.branch = 'test_branch' course_broken = models.CourseLink(root, path / 'courses/test-broken-course') course_broken.repo = fork course_broken.branch = 'test_broken_branch' run = models.CourseLink(root, path / 'runs/2018/test-run') run.repo = fork run.branch = 'test_branch' run_broken = models.CourseLink(root, path / 'runs/2018/test-broken-run') run_broken.repo = fork run_broken.branch = 'test_broken_branch' # so rendering still works meta = models.Course(root, path / 'courses/normal-course') meta.is_meta = True # so no file operations are needed, override list of courses and runs as well root.courses = OrderedDict([('test-course', course), ('test-broken-course', course_broken), ('meta', meta)]) run_year = models.RunYear(root, path / 'runs/2018') run_year.runs = OrderedDict([ ("test-run", run), ("test-broken-run", run_broken) ]) root.run_years = OrderedDict([ (2018, run_year) ]) root.runs = {(2018, "test-run"): run, (2018, "test-broken-run"): run_broken} return root
def test_lessons_slug(): """Test that an arbitrary course can have the slug 'lessons', which used to be special """ model = models.Root() path = fixture_path / 'minimal-courses' renderer = LocalRenderer( path=path, repo_info=get_local_repo_info(path), slug='lessons', api_slug='courses/minimal', ) model.add_course( models.Course.from_renderer( parent=model, renderer=renderer, )) assert sorted(model.courses) == ['lessons'] assert model.courses['lessons'].slug == 'lessons' assert model.courses['lessons'].title == 'A minimal course'
def model(fork): """ This fixture generates a Root instance, with the courses and runs generated in fixture ``fork`` """ path = Path(__file__).parent / 'fixtures/test_content' root = models.Root(path) course = models.CourseLink(root, path / 'courses/test-course') course.repo = fork course.branch = "test_branch" course_broken = models.CourseLink(root, path / 'courses/test-broken-course') course_broken.repo = fork course_broken.branch = "test_broken_branch" run = models.CourseLink(root, path / 'runs/2018/test-run') run.repo = fork run.branch = "test_branch" run_broken = models.CourseLink(root, path / 'runs/2018/test-broken-run') run_broken.repo = fork run_broken.branch = "test_broken_branch" # so no file operations are needed, override list of courses and runs as well root.courses = OrderedDict([("test-course", course), ('test-broken-course', course_broken)]) run_year = models.RunYear(root, path / 'runs/2018') run_year.runs = OrderedDict([ ("test-run", run), ("test-broken-run", run_broken) ]) root.run_years = OrderedDict([ (2018, run_year) ]) root.runs = {(2018, "test-run"): run, (2018, "test-broken-run"): run_broken} return root
def test_load_empty_dir(): model = models.Root() with pytest.raises(FileNotFoundError): model.load_local_courses(fixture_path / 'empty-directory') assert not model.courses
def test_dump_empty_model(): model = models.Root(schema_url_factory=dummy_schema_url_factory) assert_yaml_dump(models.dump(model), 'empty-root.yml')
def model(): path = Path(__file__).parent / 'fixtures/test_content' return models.Root(path)
import calendar from naucse import models import pytest root = models.Root('.') @pytest.mark.parametrize(('slug', 'weekday'), [ ('runs/2017/mipyt-zima', calendar.WEDNESDAY), ('runs/2017/pyladies-praha-podzim-cznic', calendar.TUESDAY), ('runs/2017/pyladies-praha-podzim-ntk', calendar.MONDAY), ]) def test_run_is_only_on_given_weekday(slug, weekday): run = models.Course(root, slug) for session in run.sessions.values(): assert session.date.weekday() == weekday
def test_dump_empty_model(assert_model_dump): model = models.Root(schema_url_factory=dummy_schema_url_factory) assert_model_dump(model, 'empty-root')
def model(): return models.Root(os.path.join(app.root_path, '..'))