-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_api.py
More file actions
1651 lines (1495 loc) · 62.8 KB
/
test_api.py
File metadata and controls
1651 lines (1495 loc) · 62.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests of the Publishing app's python API
"""
from __future__ import annotations
from datetime import datetime, timezone
from uuid import UUID
import pytest
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.test import TestCase
from openedx_content.applets.publishing import api as publishing_api
from openedx_content.applets.publishing.models import (
Draft,
DraftChangeLog,
DraftChangeLogRecord,
DraftSideEffect,
LearningPackage,
PublishableEntity,
PublishLog,
)
User = get_user_model()
class LearningPackageTestCase(TestCase):
"""
Test creating a LearningPackage
"""
def test_normal(self) -> None: # Note: we must specify '-> None' to opt in to type checking
"""
Normal flow with no errors.
"""
key = "my_key"
title = "My Excellent Title with Emoji 🔥"
created = datetime(2023, 4, 2, 15, 9, 0, tzinfo=timezone.utc)
description = "A fun Description!"
package = publishing_api.create_learning_package(
key=key,
title=title,
description=description,
created=created
)
assert package.key == "my_key"
assert package.title == "My Excellent Title with Emoji 🔥"
assert package.description == "A fun Description!"
assert package.created == created
assert package.updated == created
# Should be auto-generated
assert isinstance(package.uuid, UUID)
# Having an actual value here means we were persisted to the database.
assert isinstance(package.id, int)
# Now test editing the fields.
updated_package = publishing_api.update_learning_package(
package.id,
key="new_key",
title="new title",
description="new description",
)
assert updated_package.key == "new_key"
assert updated_package.title == "new title"
assert updated_package.description == "new description"
assert updated_package.created == created
assert updated_package.updated != created # new time would be auto-generated
def test_auto_datetime(self) -> None:
"""
Auto-generated created datetime works as expected.
"""
key = "my_key"
title = "My Excellent Title with Emoji 🔥"
package = publishing_api.create_learning_package(key, title)
assert package.key == "my_key"
assert package.title == "My Excellent Title with Emoji 🔥"
# Auto-generated datetime checking...
assert isinstance(package.created, datetime)
assert package.created == package.updated
assert package.created.tzinfo == timezone.utc # pylint: disable=no-member,useless-suppression
# Should be auto-generated
assert isinstance(package.uuid, UUID)
# Having an actual value here means we were persisted to the database.
assert isinstance(package.id, int)
def test_non_utc_time(self) -> None:
"""
Require UTC timezone for created.
"""
with pytest.raises(ValidationError) as excinfo:
publishing_api.create_learning_package(
key="my_key",
title="A Title",
created=datetime(2023, 4, 2)
)
message_dict = excinfo.value.message_dict
# Both datetime fields should be marked as invalid
assert "created" in message_dict
assert "updated" in message_dict
def test_already_exists(self) -> None:
"""
Raises ValidationError for duplicate keys.
"""
publishing_api.create_learning_package("my_key", "Original")
with pytest.raises(ValidationError) as excinfo:
publishing_api.create_learning_package("my_key", "Duplicate")
message_dict = excinfo.value.message_dict
assert "key" in message_dict
class DraftTestCase(TestCase):
"""
Test basic operations with Drafts.
"""
now: datetime
learning_package_1: LearningPackage
learning_package_2: LearningPackage
@classmethod
def setUpTestData(cls) -> None:
cls.now = datetime(2024, 1, 28, 16, 45, 30, tzinfo=timezone.utc)
cls.learning_package_1 = publishing_api.create_learning_package(
"my_package_key_1",
"Draft Testing LearningPackage 🔥 1",
created=cls.now,
)
cls.learning_package_2 = publishing_api.create_learning_package(
"my_package_key_2",
"Draft Testing LearningPackage 🔥 2",
created=cls.now,
)
def test_draft_lifecycle(self) -> None:
"""
Test basic lifecycle of a Draft.
"""
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
# Drafts are NOT created when a PublishableEntity is created, only when
# its first PublisahbleEntityVersion is.
assert publishing_api.get_draft_version(entity.id) is None
entity_version = publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
assert entity_version == publishing_api.get_draft_version(entity.id)
# We never really remove rows from the table holding Drafts. We just
# mark the version as None.
publishing_api.soft_delete_draft(entity.id)
deleted_entity_version = publishing_api.get_draft_version(entity.id)
assert deleted_entity_version is None
def test_set_draft_args(self) -> None:
"""Make sure it works with Draft and int, and raises exception otherwise"""
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_set_draft_args_entity",
created=self.now,
created_by=None,
)
entity_version = publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
# Int calling version
publishing_api.soft_delete_draft(entity.id)
publishing_api.set_draft_version(entity.draft.pk, entity_version.pk)
assert Draft.objects.get(entity=entity).version == entity_version
# Draft calling version
publishing_api.soft_delete_draft(entity.id)
publishing_api.set_draft_version(entity.draft, entity_version.pk)
assert Draft.objects.get(entity=entity).version == entity_version
# Unrecognized type
with pytest.raises(TypeError):
publishing_api.set_draft_version(1.0, entity_version.pk) # type: ignore[arg-type]
def test_soft_deletes(self) -> None:
"""Test the publishing behavior of soft deletes."""
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
entity_version = publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
# Initial publish
publish_log = publishing_api.publish_all_drafts(self.learning_package_1.id)
log_records = list(publish_log.records.all())
assert len(log_records) == 1
record = log_records[0]
assert record.entity_id == entity.id
assert record.old_version is None
assert record.new_version_id == entity_version.id
# Publishing the soft-delete
publishing_api.soft_delete_draft(entity.id)
publish_log = publishing_api.publish_all_drafts(self.learning_package_1.id)
log_records = list(publish_log.records.all())
assert len(log_records) == 1
record = log_records[0]
assert record.entity_id == entity.id
assert record.old_version_id == entity_version.id
assert record.new_version is None
# Verify that we do not re-publish soft-deleted records. We initially
# had a bug here because NULL != NULL in SQL, so the check to "publish
# all the Drafts that have different versions than their Published
# counterparts" would mistakenly pull in records that were NULL in both
# places.
publish_log = publishing_api.publish_all_drafts(self.learning_package_1.id)
assert publish_log.records.count() == 0
def test_soft_delete_and_reset(self) -> None:
"""
Test edge case where we create, soft-delete, and then reset.
This is an edge case because creating and then soft-deleting an item
sequence of actions will make both the Draft and Published version NULL.
In this situation reset_drafts_to_published should NOT create a
DraftChangeLog (because they already match, so there's nothing to do).
But we had a bug that redundantly set the Draft version to NULL again
because NULL != NULL in SQL and we were doing the Draft vs. Published
comparison naively without taking that into account.
"""
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
# Draft Change #1: create the new version
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
assert DraftChangeLog.objects.count() == 1
# Change #1: delete the draft (set the draft version to None)
publishing_api.soft_delete_draft(entity.id)
assert DraftChangeLog.objects.count() == 2
# This should NOT create a change:
publishing_api.reset_drafts_to_published(self.learning_package_1.id)
assert DraftChangeLog.objects.count() == 2
def test_reset_drafts_to_published(self) -> None:
"""
Test throwing out Draft data and resetting to the Published versions.
One place this might turn up is if we've imported an older version of
the library and it causes a bunch of new versions to be created.
Note that these tests don't associate any content with the versions
being created. They don't have to, because making those content
associations is the job of the ``components`` package, and potentially
other higher-level things. We're never deleting ``PublishableEntity``
or ``PublishableEntityVersion`` instances, so we don't have to worry
about potentially breaking the associated models of those higher level
apps. These tests just need to ensure that the Published and Draft
models are updated properly to point to the correct versions.
This could be broken up into separate tests for each scenario, but I
wanted to make sure nothing went wrong when multiple types of reset were
happening at the same time.
"""
# This is the Entity that's going to get a couple of new versions
# between Draft and Published.
entity_with_changes = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"entity_with_changes",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity_with_changes.id,
version_num=1,
title="I'm entity_with_changes v1",
created=self.now,
created_by=None,
)
# This Entity is going to remain unchanged between Draft and Published.
entity_with_no_changes = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"entity_with_no_changes",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity_with_no_changes.id,
version_num=1,
title="I'm entity_with_no_changes v1",
created=self.now,
created_by=None,
)
# This Entity will be Published, but will then be soft-deleted.
entity_with_pending_delete = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"entity_with_pending_delete",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity_with_pending_delete.id,
version_num=1,
title="I'm entity_with_pending_delete v1",
created=self.now,
created_by=None,
)
# Publish!
publishing_api.publish_all_drafts(self.learning_package_1.id)
# Create versions 2, 3, 4 of entity_with_changes. After this, the
# Published version is 1 and the Draft version is 4.
for version_num in range(2, 5):
publishing_api.create_publishable_entity_version(
entity_with_changes.id,
version_num=version_num,
title=f"I'm entity_with_changes v{version_num}",
created=self.now,
created_by=None,
)
# Soft-delete entity_with_pending_delete. After this, the Published
# version is 1 and the Draft version is None.
publishing_api.soft_delete_draft(entity_with_pending_delete.id)
# Create a new entity that only exists in Draft form (no Published
# version).
new_entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"new_entity",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
new_entity.id,
version_num=1,
title="I'm new_entity v1",
created=self.now,
created_by=None,
)
# The versions we expect in (entity, published version_num, draft
# version_num) tuples.
expected_pre_reset_state = [
(entity_with_changes, 1, 4),
(entity_with_no_changes, 1, 1),
(entity_with_pending_delete, 1, None),
(new_entity, None, 1),
]
for entity, pub_version_num, draft_version_num in expected_pre_reset_state:
# Make sure we grab a new copy from the database so we're not
# getting stale cached values:
assert pub_version_num == self._get_published_version_num(entity)
assert draft_version_num == self._get_draft_version_num(entity)
# Now reset to draft here!
publishing_api.reset_drafts_to_published(self.learning_package_1.id)
# Versions we expect after reset in (entity, published version num)
# tuples. The only entity that is not version 1 is the new one.
expected_post_reset_state = [
(entity_with_changes, 1),
(entity_with_no_changes, 1),
(entity_with_pending_delete, 1),
(new_entity, None),
]
for entity, pub_version_num in expected_post_reset_state:
assert (
self._get_published_version_num(entity) ==
self._get_draft_version_num(entity) ==
pub_version_num
)
def test_reset_drafts_to_published_bulk(self) -> None:
"""bulk_draft_changes_for creates only one DraftChangeLog."""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
self.test_reset_drafts_to_published()
assert DraftChangeLog.objects.count() == 1
def test_get_entities_with_unpublished_changes(self) -> None:
"""Test fetching entities with unpublished changes after soft deletes."""
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
# Fetch unpublished entities
entities = publishing_api.get_entities_with_unpublished_changes(self.learning_package_1.id)
records = list(entities.all())
assert len(records) == 1
record = records[0]
assert record.id == entity.id
# Initial publish
publishing_api.publish_all_drafts(self.learning_package_1.id)
# soft-delete entity
publishing_api.soft_delete_draft(entity.id)
entities = publishing_api.get_entities_with_unpublished_changes(self.learning_package_1.id)
assert len(entities) == 0
entities = publishing_api.get_entities_with_unpublished_changes(self.learning_package_1.id,
include_deleted_drafts=True)
assert len(entities) == 1
# publish soft-delete
publishing_api.publish_all_drafts(self.learning_package_1.id)
entities = publishing_api.get_entities_with_unpublished_changes(self.learning_package_1.id,
include_deleted_drafts=True)
# should not return published soft-deleted entities.
assert len(entities) == 0
def test_filter_publishable_entities(self) -> None:
count_published = 7
count_drafts = 6
count_no_drafts = 3
for index in range(count_published):
# Create entities to publish
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
f"entity_published_{index}",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title=f"Entity_published_{index}",
created=self.now,
created_by=None,
)
publishing_api.publish_all_drafts(self.learning_package_1.id)
for index in range(count_drafts):
# Create entities with drafts
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
f"entity_draft_{index}",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title=f"Entity_draft_{index}",
created=self.now,
created_by=None,
)
for index in range(count_no_drafts):
# Create entities without drafts
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
f"entity_no_draft_{index}",
created=self.now,
created_by=None,
)
drafts = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_draft=True,
)
assert drafts.count() == (count_published + count_drafts)
no_drafts = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_draft=False,
)
assert no_drafts.count() == count_no_drafts
published = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_published=True,
)
assert published.count() == count_published
no_published = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_published=False,
)
assert no_published.count() == (count_drafts + count_no_drafts)
def _get_published_version_num(self, entity: PublishableEntity) -> int | None:
published_version = publishing_api.get_published_version(entity.id)
if published_version is not None:
return published_version.version_num
return None
def _get_draft_version_num(self, entity: PublishableEntity) -> int | None:
draft_version = publishing_api.get_draft_version(entity.id)
if draft_version is not None:
return draft_version.version_num
return None
class DraftChangeLogTestCase(TestCase):
"""
Test basic operations with DraftChangeLogs and bulk draft operations.
"""
now: datetime
learning_package_1: LearningPackage
learning_package_2: LearningPackage
@classmethod
def setUpTestData(cls) -> None:
cls.now = datetime(2024, 1, 28, 16, 45, 30, tzinfo=timezone.utc)
cls.learning_package_1 = publishing_api.create_learning_package(
"my_package_key_1",
"Draft Testing LearningPackage 🔥 1",
created=cls.now,
)
cls.learning_package_2 = publishing_api.create_learning_package(
"my_package_key_2",
"Draft Testing LearningPackage 🔥 2",
created=cls.now,
)
def test_simple_draft_change_log(self) -> None:
"""
Simplest test that multiple writes make it into one DraftChangeLog.
"""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
entity2 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity2",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity2.id,
version_num=1,
title="An Entity 🌴 2",
created=self.now,
created_by=None,
)
draft_sets = list(DraftChangeLog.objects.all())
assert len(draft_sets) == 1
assert len(draft_sets[0].records.all()) == 2
# Now that we're outside of the context manager, check that we're making
# a new DraftChangeLog...
entity3 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity3",
created=self.now,
created_by=None,
)
e3_v1 = publishing_api.create_publishable_entity_version(
entity3.id,
version_num=1,
title="An Entity 🌴 3",
created=self.now,
created_by=None,
)
draft_sets = list(DraftChangeLog.objects.all().order_by('id'))
assert len(draft_sets) == 2
assert len(draft_sets[1].records.all()) == 1
# Now make one entirely redundant change, and make sure it didn't create
# anything (setting a draft to the same version it already was should be
# a no-op).
publishing_api.set_draft_version(entity3.id, e3_v1.pk)
draft_sets = list(DraftChangeLog.objects.all().order_by('id'))
assert len(draft_sets) == 2
assert len(draft_sets[1].records.all()) == 1
def test_nested_draft_changesets(self) -> None:
"""
We should look up the stack to find the right one for our Learning Package.
"""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id) as dcl_1:
lp1_e1 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"lp1_e1",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
lp1_e1.id,
version_num=1,
title="LP1 E1 v1",
created=self.now,
created_by=None,
)
with publishing_api.bulk_draft_changes_for(self.learning_package_2.id) as dcl_2:
# This should make its way into the *outer* context, because
# we're creating the new publishable entity version for
# learning_package_1, not learning_package_2
lp1_e1_v2 = publishing_api.create_publishable_entity_version(
lp1_e1.id,
version_num=2,
title="LP1 E1 v1",
created=self.now,
created_by=None,
)
# Make sure our change above made it to the outer context and
# didn't make a new one (or go to the inner context).
assert DraftChangeLog.objects.all().count() == 2
assert DraftChangeLogRecord.objects.all().count() == 1
lp1_e1_record = DraftChangeLogRecord.objects.first()
assert lp1_e1_record is not None
assert lp1_e1_record.old_version is None
assert lp1_e1_record.new_version == lp1_e1_v2
assert lp1_e1_record.draft_change_log.learning_package == self.learning_package_1
# This will go to the inner context:
lp2_e1 = publishing_api.create_publishable_entity(
self.learning_package_2.id,
"lp2_e1",
created=self.now,
created_by=None,
)
lp2_e1_v1 = publishing_api.create_publishable_entity_version(
lp2_e1.id,
version_num=1,
title="LP2 E1 v1",
created=self.now,
created_by=None,
)
# This doesn't error, but it creates a new DraftChangeLog instead of
# re-using dcl_2
lp2_e1_v2 = publishing_api.create_publishable_entity_version(
lp2_e1.id,
version_num=2,
title="LP2 E1 v2",
created=self.now,
created_by=None,
)
# Sanity check that the first/outer DraftChangeLog hasn't changed.
assert dcl_1.records.count() == 1
# Check the state of the second/inner DraftChangeLog
assert dcl_2.records.count() == 1
lp2_e1_record = dcl_2.records.get(entity=lp2_e1)
assert lp2_e1_record.old_version is None
assert lp2_e1_record.new_version == lp2_e1_v1
# We should have 3 DraftChangeLogs, because the last change to lp2_e1
# was done outside of any context for Learning Package 2. Instead of
# using Learning Package 1's context, it should create its own
# DraftChangeLog:
assert DraftChangeLog.objects.count() == 3
implicit_dcl = DraftChangeLog.objects.order_by('id').last()
assert implicit_dcl is not None
assert implicit_dcl.records.count() == 1
implicit_lp2_e1_record = implicit_dcl.records.get(entity=lp2_e1)
assert implicit_lp2_e1_record.old_version == lp2_e1_v1
assert implicit_lp2_e1_record.new_version == lp2_e1_v2
def test_multiple_draft_changes(self) -> None:
"""
Test that multiple changes to the same entity are collapsed.
"""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴 v1",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=2,
title="An Entity 🌴 v2",
created=self.now,
created_by=None,
)
draft_sets = list(DraftChangeLog.objects.all().order_by('id'))
assert len(draft_sets) == 1
changes = list(draft_sets[0].records.all())
assert len(changes) == 1
change = changes[0]
assert change.old_version is None
assert change.new_version is not None
assert change.new_version.version_num == 2
def test_some_draft_changes_cancel_out(self) -> None:
"""Test that re remove redundant changes from our DraftChangeLog."""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
# This change will get cancelled out (because we create a draft and
# then delete it), so changes related to entity_1 will be removed
# after the context ends.
entity_1 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"Entity-1",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity_1.id,
version_num=1,
title="An Entity 🌴 v1",
created=self.now,
created_by=None,
)
publishing_api.soft_delete_draft(entity_1.id)
# The change to entity_2 will persist
entity_2 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"Entity-2",
created=self.now,
created_by=None,
)
e2_v1 = publishing_api.create_publishable_entity_version(
entity_2.id,
version_num=1,
title="E2 title",
created=self.now,
created_by=None,
)
assert DraftChangeLog.objects.all().count() == 1
change_log = DraftChangeLog.objects.first()
assert change_log is not None
assert change_log.records.count() == 1
change = change_log.records.get(entity_id=entity_2.id)
assert change.old_version is None
assert change.new_version == e2_v1
def test_multiple_draft_changes_all_cancel_out(self) -> None:
"""
If all changes made cancel out, the entire DraftRecord gets deleted.
"""
# Make sure a version change from (None -> None) gets removed.
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
entity = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
v1 = publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴 v1",
created=self.now,
created_by=None,
)
publishing_api.soft_delete_draft(entity.id)
assert not DraftChangeLog.objects.all().exists()
# This next call implicitly makes a DraftChangeLog
publishing_api.set_draft_version(entity.id, v1.pk)
assert DraftChangeLog.objects.all().count() == 1
# Make sure a change from v1 -> v2 -> v1 gets removed.
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
for i in range(2, 5):
# Make a few new versions
publishing_api.create_publishable_entity_version(
entity.id,
version_num=i,
title=f"An Entity v{i}",
created=self.now,
created_by=None,
)
# Reset to version 1
publishing_api.set_draft_version(entity.id, v1.pk)
assert DraftChangeLog.objects.all().count() == 1
class PublishLogTestCase(TestCase):
"""
Test basic operations with PublishLogs and PublishSideEffects.
"""
now: datetime
learning_package_1: LearningPackage
learning_package_2: LearningPackage
@classmethod
def setUpTestData(cls) -> None:
cls.now = datetime(2024, 1, 28, 16, 45, 30, tzinfo=timezone.utc)
cls.learning_package_1 = publishing_api.create_learning_package(
"my_package_key_1",
"PublishLog Testing LearningPackage 🔥 1",
created=cls.now,
)
cls.learning_package_2 = publishing_api.create_learning_package(
"my_package_key_2",
"PublishLog Testing LearningPackage 🔥 2",
created=cls.now,
)
def test_simple_publish_log(self) -> None:
"""
Simplest test that multiple writes make it into one PublishLog.
"""
with publishing_api.bulk_draft_changes_for(self.learning_package_1.id):
entity1 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity",
created=self.now,
created_by=None,
)
entity1_v1 = publishing_api.create_publishable_entity_version(
entity1.id,
version_num=1,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
entity2 = publishing_api.create_publishable_entity(
self.learning_package_1.id,
"my_entity2",
created=self.now,
created_by=None,
)
entity2_v1 = publishing_api.create_publishable_entity_version(
entity2.id,
version_num=1,
title="An Entity 🌴 2",
created=self.now,
created_by=None,
)
# Check simplest publish of two things...
publish_log = publishing_api.publish_all_drafts(self.learning_package_1.id)
assert PublishLog.objects.all().count() == 1
assert publish_log.records.all().count() == 2
record_1 = publish_log.records.get(entity=entity1)
assert record_1 is not None
assert record_1.old_version is None
assert record_1.new_version == entity1_v1
record_2 = publish_log.records.get(entity=entity2)
assert record_2 is not None
assert record_2.old_version is None
assert record_2.new_version == entity2_v1
# Check that an empty publish still creates a PublishLog, just one with
# no records. This may be useful for triggering tasks that trigger off
# of publishing events, though I'm not confident about that at the
# moment.
publish_log = publishing_api.publish_all_drafts(self.learning_package_1.id)
assert publish_log.records.count() == 0
# Check that we can publish a subset...
entity1_v2 = publishing_api.create_publishable_entity_version(
entity1.id,
version_num=2,
title="An Entity 🌴",
created=self.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity2.id,
version_num=2,
title="An Entity 🌴 2",
created=self.now,
created_by=None,
)
publish_e1_log = publishing_api.publish_from_drafts(
self.learning_package_1.id,
Draft.objects.filter(pk=entity1.pk),
)
assert publish_e1_log.records.count() == 1
e1_pub_record = publish_e1_log.records.get(entity=entity1)
assert e1_pub_record is not None
assert e1_pub_record.old_version == entity1_v1
assert e1_pub_record.new_version == entity1_v2
class EntitiesQueryTestCase(TestCase):
"""
Tests for querying PublishableEntity objects.
"""
now: datetime
learning_package_1: LearningPackage
@classmethod
def setUpTestData(cls) -> None:
"""
Initialize our content data
"""
cls.now = datetime(2025, 8, 4, 12, 00, 00, tzinfo=timezone.utc)
cls.learning_package_1 = publishing_api.create_learning_package(
"my_package_key_1",
"Entities Testing LearningPackage 🔥 1",
created=cls.now,
)
with publishing_api.bulk_draft_changes_for(cls.learning_package_1.id):
entity = publishing_api.create_publishable_entity(
cls.learning_package_1.id,
"my_entity",
created=cls.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title="An Entity 🌴",
created=cls.now,
created_by=None,
)
entity2 = publishing_api.create_publishable_entity(
cls.learning_package_1.id,
"my_entity2",
created=cls.now,
created_by=None,
)
publishing_api.create_publishable_entity_version(
entity2.id,
version_num=1,
title="An Entity 🌴 2",
created=cls.now,
created_by=None,
)
publishing_api.publish_all_drafts(cls.learning_package_1.id)
def test_get_publishable_entities(self) -> None:
"""
Test that get_entities returns all entities for a learning package.
"""
entities = publishing_api.get_publishable_entities(self.learning_package_1.id)
assert entities.count() == 2
for entity in entities:
assert isinstance(entity, PublishableEntity)
assert entity.learning_package_id == self.learning_package_1.id
assert entity.created == self.now
def test_get_publishable_entities_n_plus_problem(self) -> None:
"""