Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions learning_resources/etl/mitpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,13 @@ def _transform_runs(resource_data: dict) -> list[dict]:
duration = parse_resource_duration(resource_data.get("duration"))
published_runs = []
for run_data in runs_data:
run_id = run_data[0]
start = parse_date(run_data[1])
end = parse_date(run_data[2])
enrollment_end = parse_date(run_data[3])
published = (not end and not enrollment_end) or (now <= (enrollment_end or end))
published = run_id and (
(not end and not enrollment_end) or (now <= (enrollment_end or end))
)
if published:
published_runs.append(
{
Expand Down Expand Up @@ -283,8 +286,8 @@ def transform_course(resource_data: dict) -> dict or None:
Returns:
dict: transformed course data if it has any viable runs
"""
runs = _transform_runs(resource_data)
if runs:
published_runs = _transform_runs(resource_data)
if published_runs:
return {
"readable_id": resource_data["uuid"],
"offered_by": copy.deepcopy(OFFERED_BY),
Expand All @@ -303,7 +306,7 @@ def transform_course(resource_data: dict) -> dict or None:
"delivery": transform_delivery(resource_data["learning_format"]),
"published": True,
"topics": parse_topics(resource_data),
"runs": runs,
"runs": published_runs,
"format": [Format.asynchronous.name],
"pace": [Pace.instructor_paced.name],
"availability": Availability.dated.name,
Expand All @@ -321,8 +324,8 @@ def transform_program(resource_data: dict) -> dict or None:
Returns:
dict: transformed program data
"""
runs = _transform_runs(resource_data)
if runs:
published_runs = _transform_runs(resource_data)
if published_runs:
return {
"readable_id": resource_data["uuid"],
"offered_by": copy.deepcopy(OFFERED_BY),
Expand All @@ -343,7 +346,7 @@ def transform_program(resource_data: dict) -> dict or None:
"delivery": transform_delivery(resource_data["learning_format"]),
"published": True,
"topics": parse_topics(resource_data),
"runs": runs,
"runs": published_runs,
"format": [Format.asynchronous.name],
"pace": [Pace.instructor_paced.name],
"availability": Availability.dated.name,
Expand Down
39 changes: 27 additions & 12 deletions learning_resources/etl/mitpe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ def read_json(file_path):
)


@pytest.fixture
def mitpe_offeror_and_topics(mocker):
"""Create the MIT Professional Education offeror and 2 topics"""
offeror = LearningResourceOfferorFactory.create(code="mitpe")
LearningResourceTopicMappingFactory.create(
offeror=offeror,
topic=LearningResourceTopicFactory.create(name="Product Innovation"),
topic_name="Technology Innovation",
)
LearningResourceTopicMappingFactory.create(
offeror=offeror,
topic=LearningResourceTopicFactory.create(name="Data Science"),
topic_name="Data Science",
)


@pytest.mark.parametrize("prof_ed_api_url", ["http://pro_edd_api.com", None])
def test_extract(settings, mock_fetch_data, prof_ed_api_url):
"""Test extract function"""
Expand All @@ -213,23 +229,22 @@ def test_extract(settings, mock_fetch_data, prof_ed_api_url):


@pytest.mark.django_db
def test_transform(mock_fetch_data):
def test_transform(mock_fetch_data, mitpe_offeror_and_topics):
"""Test transform function, and effectively most other functions"""
offeror = LearningResourceOfferorFactory.create(code="mitpe")
LearningResourceTopicMappingFactory.create(
offeror=offeror,
topic=LearningResourceTopicFactory.create(name="Product Innovation"),
topic_name="Technology Innovation",
)
LearningResourceTopicMappingFactory.create(
offeror=offeror,
topic=LearningResourceTopicFactory.create(name="Data Science"),
topic_name="Data Science",
)
extracted = mitpe.extract()
assert len(extracted) == 3
courses, programs = mitpe.transform(extracted)
assert_json_equal(
sorted(courses, key=lambda course: course["readable_id"]), EXPECTED_COURSES
)
assert_json_equal(programs, EXPECTED_PROGRAMS)


@pytest.mark.django_db
def test_transform__course_no_run_ids(mock_fetch_data, mitpe_offeror_and_topics):
"""Resources with no run IDs should not be published"""
extracted = mitpe.extract()
assert len(extracted) == 3
for resource in extracted:
resource["run__readable_id"] = ""
assert mitpe.transform(extracted) == ([], [])
Loading