-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_models.py
More file actions
102 lines (82 loc) · 2.95 KB
/
test_models.py
File metadata and controls
102 lines (82 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# pylint: disable=redefined-outer-name
"""
Tests for the `sample-plugin` models module.
"""
import pytest
from django.contrib.auth import get_user_model
from django.db.utils import IntegrityError
from opaque_keys.edx.keys import CourseKey
from openedx_catalog.api import create_course_run_for_modulestore_course_with
from openedx_plugin_sample.models import CourseArchiveStatus
User = get_user_model()
@pytest.fixture
def user():
"""
Create and return a test user.
"""
return User.objects.create_user(
username="testuser", email="testuser@example.com", password="password123"
)
@pytest.fixture
def staff_user():
"""
Create and return a test staff user.
"""
return User.objects.create_user(
username="staffuser",
email="staffuser@example.com",
password="password123",
is_staff=True,
)
@pytest.fixture
def course_run():
"""
Create and return a test CourseRun (plus its Organization and CatalogCourse).
"""
return create_course_run_for_modulestore_course_with(
CourseKey.from_string("course-v1:edX+DemoX+Demo_Course"),
title="Demo Course",
)
@pytest.mark.django_db
def test_course_archive_status_creation(user, course_run):
"""
Test that a CourseArchiveStatus can be created with valid data.
"""
course_archive_status = CourseArchiveStatus.objects.create(
course_run=course_run, user=user, is_archived=False
)
assert course_archive_status.pk is not None
assert course_archive_status.course_run == course_run
assert course_archive_status.user == user
assert course_archive_status.is_archived is False
assert course_archive_status.archive_date is None
assert course_archive_status.created_at is not None
assert course_archive_status.updated_at is not None
@pytest.mark.django_db
def test_course_archive_status_uniqueness(user, course_run):
"""
Test that a CourseArchiveStatus must be unique per user and course_run.
"""
CourseArchiveStatus.objects.create(
course_run=course_run, user=user, is_archived=False
)
# Creating another with same user and course_run should raise an IntegrityError
with pytest.raises(IntegrityError):
CourseArchiveStatus.objects.create(
course_run=course_run, user=user, is_archived=True
)
@pytest.mark.django_db
def test_course_archive_status_str_method(user, course_run):
"""
Test the string representation of CourseArchiveStatus.
"""
course_archive_status = CourseArchiveStatus.objects.create(
course_run=course_run, user=user, is_archived=True
)
expected_str = f"{course_run.course_key} - {user.username} - Archived"
assert str(course_archive_status) == expected_str
course_archive_status.is_archived = False
course_archive_status.save()
expected_str = f"{course_run.course_key} - {user.username} - Not Archived"
assert str(course_archive_status) == expected_str