-
Notifications
You must be signed in to change notification settings - Fork 315
/
test_nbgrader_autograde.py
1083 lines (862 loc) · 60.1 KB
/
test_nbgrader_autograde.py
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
# -*- coding: utf-8 -*-
import io
import os
import sys
import json
import pytest
from os.path import join
from textwrap import dedent
from nbformat import current_nbformat
from ...api import Gradebook, MissingEntry
from ...utils import remove
from ...nbgraderformat import reads
from .. import run_nbgrader
from .base import BaseTestApp
class TestNbGraderAutograde(BaseTestApp):
def test_help(self):
"""Does the help display without error?"""
run_nbgrader(["autograde", "--help-all"])
def test_missing_student(self, db, course_dir):
"""Is a missing student automatically created?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate", "2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "baz", "ps1", "p1.ipynb"))
# If we explicitly disable creating students, autograde should fail
run_nbgrader(["autograde", "ps1", "--db", db, "--Autograde.create_student=False"], retcode=1)
# The default is now to create missing students (formerly --create)
run_nbgrader(["autograde", "ps1", "--db", db])
def test_missing_assignment(self, db, course_dir):
"""Is an error thrown when the assignment is missing?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate", "2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "ps2", "foo", "p1.ipynb"))
run_nbgrader(["autograde", "ps2", "--db", db], retcode=1)
def test_grade(self, db, course_dir):
"""Can files be graded?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate", "2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
notebook = gb.find_submission_notebook("p1", "ps1", "foo")
assert notebook.score == 1
assert notebook.max_score == 7
assert notebook.needs_manual_grade == False
comment1 = gb.find_comment("set_a", "p1", "ps1", "foo")
comment2 = gb.find_comment("baz", "p1", "ps1", "foo")
comment3 = gb.find_comment("quux", "p1", "ps1", "foo")
assert comment1.comment == "No response."
assert comment2.comment == "No response."
assert comment3.comment == "No response."
notebook = gb.find_submission_notebook("p1", "ps1", "bar")
assert notebook.score == 2
assert notebook.max_score == 7
assert notebook.needs_manual_grade == True
comment1 = gb.find_comment("set_a", "p1", "ps1", "bar")
comment2 = gb.find_comment("baz", "p1", "ps1", "bar")
comment2 = gb.find_comment("quux", "p1", "ps1", "bar")
assert comment1.comment == None
assert comment2.comment == None
def test_showtraceback_exploit(self, db, course_dir):
"""Can students exploit showtraceback to hide errors from all future cell outputs to receive free points for incorrect cells?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate", "2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
run_nbgrader(["db", "student", "add", "spam", "--db", db])
run_nbgrader(["db", "student", "add", "eggs", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
# This exploit previously caused cell executions that would indefinitely hang.
# See: https://github.com/ipython/ipython/commit/fd34cf5
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.Execute.timeout = None""")
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-cheat-attempt.ipynb"), join(course_dir, "submitted", "spam", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-cheat-attempt-alternative.ipynb"), join(course_dir, "submitted", "eggs", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "spam", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "spam", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "eggs", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "eggs", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
notebook = gb.find_submission_notebook("p1", "ps1", "foo")
assert notebook.score == 1
assert notebook.max_score == 7
assert notebook.needs_manual_grade == False
notebook = gb.find_submission_notebook("p1", "ps1", "bar")
assert notebook.score == 2
assert notebook.max_score == 7
assert notebook.needs_manual_grade == True
notebook = gb.find_submission_notebook("p1", "ps1", "spam")
assert notebook.score == 1
assert notebook.max_score == 7
assert notebook.needs_manual_grade == True
notebook = gb.find_submission_notebook("p1", "ps1", "eggs")
assert notebook.score == 1
assert notebook.max_score == 7
assert notebook.needs_manual_grade == True
def test_student_id_exclude(self, db, course_dir):
"""Does --CourseDirectory.student_id_exclude=X exclude students?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "baz", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db, '--CourseDirectory.student_id_exclude=bar,baz'])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "baz", "ps1", "p1.ipynb"))
with Gradebook(db) as gb:
notebook = gb.find_submission_notebook("p1", "ps1", "foo")
assert notebook.score == 1
with pytest.raises(MissingEntry):
notebook = gb.find_submission_notebook("p1", "ps1", "bar")
with pytest.raises(MissingEntry):
notebook = gb.find_submission_notebook("p1", "ps1", "baz")
def test_grade_timestamp(self, db: str, course_dir: str) -> None:
"""Is a timestamp correctly read in?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), "2015-02-01 14:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
submission = gb.find_submission("ps1", "foo")
assert submission.total_seconds_late > 0
submission = gb.find_submission("ps1", "bar")
assert submission.total_seconds_late == 0
# make sure it still works to run it a second time
run_nbgrader(["autograde", "ps1", "--db", db])
def test_grade_empty_timestamp(self, db: str, course_dir: str) -> None:
"""Issue #580 - Does the autograder handle empty or invalid timestamp
strings"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
submission = gb.find_submission("ps1", "foo")
assert submission.total_seconds_late == 0
invalid_timestamp = "But I want to be a timestamp string :("
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), invalid_timestamp)
run_nbgrader(["autograde", "ps1", "--db", db], retcode=1)
def test_late_submission_penalty_none(self, db: str, course_dir: str) -> None:
"""Does 'none' method do nothing?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
# not late
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 14:58:23.948203 America/Los_Angeles")
# 1h late
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
# not late
submission = gb.find_submission("ps1", "foo")
nb = submission.notebooks[0]
assert nb.score == 1
assert submission.total_seconds_late == 0
assert nb.late_submission_penalty == None
# 1h late
submission = gb.find_submission("ps1", "bar")
nb = submission.notebooks[0]
assert nb.score == 2
assert submission.total_seconds_late > 0
assert nb.late_submission_penalty == None
def test_late_submission_penalty_zero(self, db: str, course_dir: str) -> None:
"""Does 'zero' method assign notebook.score as penalty if late?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.LateSubmissionPlugin.penalty_method = 'zero'""")
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
# not late
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 14:58:23.948203 America/Los_Angeles")
# 1h late
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
# not late
submission = gb.find_submission("ps1", "foo")
nb = submission.notebooks[0]
assert nb.score == 1
assert submission.total_seconds_late == 0
assert nb.late_submission_penalty == None
# 1h late
submission = gb.find_submission("ps1", "bar")
nb = submission.notebooks[0]
assert nb.score == 2
assert submission.total_seconds_late > 0
assert nb.late_submission_penalty == nb.score
# Issue 723 - check penalty is reset if timestamp changed
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), "2015-02-02 14:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "--force", "ps1", "--db", db])
with Gradebook(db) as gb:
# no longer late
submission = gb.find_submission("ps1", "bar")
nb = submission.notebooks[0]
assert nb.score == 2
assert submission.total_seconds_late == 0
assert nb.late_submission_penalty == None
def test_late_submission_penalty_plugin(self, db: str, course_dir: str) -> None:
"""Does plugin set 1 point per hour late penalty?"""
plugin = dedent("""
from __future__ import division
from nbgrader.plugins import BasePlugin
class Blarg(BasePlugin):
def late_submission_penalty(self, student_id, score, total_seconds_late):
# loss 1 mark per hour late
hours_late = total_seconds_late / 3600
return int(hours_late)
""")
with open("late_plugin.py", 'w') as fh:
fh.write(plugin)
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.AssignLatePenalties.plugin_class = 'late_plugin.Blarg'""")
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
# 4h late
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 18:58:23.948203 America/Los_Angeles")
# 1h late
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "bar", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "bar", "ps1", "timestamp.txt"))
with Gradebook(db) as gb:
# 4h late
submission = gb.find_submission("ps1", "foo")
nb = submission.notebooks[0]
assert nb.score == 1
assert submission.total_seconds_late > 0
assert nb.late_submission_penalty == nb.score
# 1h late
submission = gb.find_submission("ps1", "bar")
nb = submission.notebooks[0]
assert nb.score == 2
assert submission.total_seconds_late > 0
assert nb.late_submission_penalty == 1
def test_force(self, db: str, course_dir: str) -> None:
"""Ensure the force option works properly"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "source", "ps1", "data", "bar.txt"), "bar")
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "data", "bar.txt"), "bar")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "blah.pyc"), "asdf")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
# check that it skips the existing directory
remove(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
# force overwrite the supplemental files
run_nbgrader(["autograde", "ps1", "--db", db, "--force"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
# force overwrite
remove(join(course_dir, "source", "ps1", "foo.txt"))
remove(join(course_dir, "submitted", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db, "--force"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
def test_force_f(self, db: str, course_dir: str) -> None:
"""Ensure the force option works properly"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "source", "ps1", "data", "bar.txt"), "bar")
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "data", "bar.txt"), "bar")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "blah.pyc"), "asdf")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
# check that it skips the existing directory
remove(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
# force overwrite the supplemental files
run_nbgrader(["autograde", "ps1", "--db", db, "-f"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
# force overwrite
remove(join(course_dir, "source", "ps1", "foo.txt"))
remove(join(course_dir, "submitted", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db, "-f"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
def test_filter_notebook(self, db: str, course_dir: str) -> None:
"""Does autograding filter by notebook properly?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "source", "ps1", "data", "bar.txt"), "bar")
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "foo.txt"), "foo")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "data", "bar.txt"), "bar")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "blah.pyc"), "asdf")
run_nbgrader(["autograde", "ps1", "--db", db, "--notebook", "p1"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
# check that removing the notebook still causes the autograder to run
remove(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
remove(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db, "--notebook", "p1"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
# check that running it again doesn"t do anything
remove(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
run_nbgrader(["autograde", "ps1", "--db", db, "--notebook", "p1"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
# check that removing the notebook doesn"t caus the autograder to run
remove(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data", "bar.txt"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "blah.pyc"))
def test_grade_overwrite_files(self, db: str, course_dir: str) -> None:
"""Are dependent files properly linked and overwritten?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.Autograde.exclude_overwriting = {"ps1": ["helper.py"]}\n""")
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "data.csv"), "some,data\n")
self._make_file(join(course_dir, "source", "ps1", "helper.py"), "print('hello!')\n")
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "data.csv"), "some,other,data\n")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "helper.py"), "print('this is different!')\n")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "data.csv"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "helper.py"))
with open(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"), "r") as fh:
contents = fh.read()
assert contents == "2015-02-02 15:58:23.948203 America/Los_Angeles"
with open(join(course_dir, "autograded", "foo", "ps1", "data.csv"), "r") as fh:
contents = fh.read()
assert contents == "some,data\n"
with open(join(course_dir, "autograded", "foo", "ps1", "helper.py"), "r") as fh:
contents = fh.read()
assert contents == "print('this is different!')\n"
def test_grade_overwrite_files_subdirs(self, db: str, course_dir: str) -> None:
"""Are dependent files properly linked and overwritten?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.Autograde.exclude_overwriting = {{"ps1": ["{}"]}}\n""".format(os.path.join("subdir", "helper.py")))
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "subdir", "data.csv"), "some,data\n")
self._make_file(join(course_dir, "source", "ps1", "subdir", "helper.py"), "print('hello!')\n")
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "subdir", "data.csv"), "some,other,data\n")
self._make_file(join(course_dir, "submitted", "foo", "ps1", "subdir", "helper.py"), "print('this is different!')\n")
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "subdir", "data.csv"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "subdir", "helper.py"))
with open(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"), "r") as fh:
contents = fh.read()
assert contents == "2015-02-02 15:58:23.948203 America/Los_Angeles"
with open(join(course_dir, "autograded", "foo", "ps1", "subdir", "data.csv"), "r") as fh:
contents = fh.read()
assert contents == "some,data\n"
with open(join(course_dir, "autograded", "foo", "ps1", "subdir", "helper.py"), "r") as fh:
contents = fh.read()
assert contents == "print('this is different!')\n"
def test_side_effects(self, db: str, course_dir: str) -> None:
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "side-effects.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "side-effects.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "side-effect.txt"))
assert not os.path.isfile(join(course_dir, "submitted", "foo", "ps1", "side-effect.txt"))
def test_skip_extra_notebooks(self, db, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1 copy.ipynb"))
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1 copy.ipynb"))
@pytest.mark.parametrize("groupshared", [False, True])
def test_permissions(self, course_dir, groupshared):
"""Are permissions properly set?"""
run_nbgrader(["db", "assignment", "add", "ps1"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
with open("nbgrader_config.py", "a") as fh:
if groupshared:
fh.write("""c.CourseDirectory.groupshared = True\n""")
self._empty_notebook(join(course_dir, "source", "ps1", "foo.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "foo.txt"), "foo")
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "foo.ipynb"))
self._make_file(join(course_dir, "source", "foo", "ps1", "foo.txt"), "foo")
run_nbgrader(["autograde", "ps1"])
if not groupshared:
if sys.platform == 'win32':
perms = '444'
else:
perms = '444'
else:
if sys.platform == 'win32':
perms = '666'
dirperms = '777'
else:
perms = '664'
dirperms = '2775'
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
if groupshared:
# non-groupshared doesn't make guarantees about directory perms
assert self._get_permissions(join(course_dir, "autograded", "foo", "ps1")) == dirperms
assert self._get_permissions(join(course_dir, "autograded", "foo", "ps1", "foo.ipynb")) == perms
assert self._get_permissions(join(course_dir, "autograded", "foo", "ps1", "foo.txt")) == perms
def test_custom_permissions(self, course_dir):
"""Are custom permissions properly set?"""
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._empty_notebook(join(course_dir, "source", "ps1", "foo.ipynb"))
self._make_file(join(course_dir, "source", "ps1", "foo.txt"), "foo")
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "foo.ipynb"))
self._make_file(join(course_dir, "source", "foo", "ps1", "foo.txt"), "foo")
run_nbgrader(["autograde", "ps1", "--AutogradeApp.permissions=644"])
if sys.platform == 'win32':
perms = '666'
else:
perms = '644'
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "foo.txt"))
assert self._get_permissions(join(course_dir, "autograded", "foo", "ps1", "foo.ipynb")) == perms
assert self._get_permissions(join(course_dir, "autograded", "foo", "ps1", "foo.txt")) == perms
def test_force_single_notebook(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p2.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
run_nbgrader(["autograde", "ps1",
"--ClearMetadataPreprocessor.enabled=True",
"--ClearMetadataPreprocessor.clear_notebook_metadata=False",
"--ClearMetadataPreprocessor.preserve_cell_metadata_mask=[('nbgrader')]"
])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
p1 = self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
p2 = self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
assert p1 == p2
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
run_nbgrader(["autograde", "ps1", "--notebook", "p1", "--force"])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
assert p1 != self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert p2 == self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
def test_update_newer(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert self._file_contents(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt")) == "2015-02-02 15:58:23.948203 America/Los_Angeles"
p = self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 16:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1"])
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert self._file_contents(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt")) == "2015-02-02 16:58:23.948203 America/Los_Angeles"
assert p != self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
def test_update_newer_single_notebook(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p2.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 15:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1",
"--ClearMetadataPreprocessor.enabled=True",
"--ClearMetadataPreprocessor.clear_notebook_metadata=False",
"--ClearMetadataPreprocessor.preserve_cell_metadata_mask=[('nbgrader')]"
])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert self._file_contents(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt")) == "2015-02-02 15:58:23.948203 America/Los_Angeles"
p1 = self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
p2 = self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
assert p1 == p2
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
self._make_file(join(course_dir, "submitted", "foo", "ps1", "timestamp.txt"), "2015-02-02 16:58:23.948203 America/Los_Angeles")
run_nbgrader(["autograde", "ps1", "--notebook", "p1"])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt"))
assert self._file_contents(join(course_dir, "autograded", "foo", "ps1", "timestamp.txt")) == "2015-02-02 16:58:23.948203 America/Los_Angeles"
assert p1 != self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert p2 == self._file_contents(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
def test_hidden_tests_single_notebook(self, db, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.ClearSolutions.code_stub=dict(python="# YOUR CODE HERE")""")
self._copy_file(
join("files", "test-hidden-tests.ipynb"),
join(course_dir, "source", "ps1", "p1.ipynb")
)
# test-hidden-tests.ipynb contains vizable solutions that pass
# vizable tests, but fail on hidden tests
run_nbgrader(["generate_assignment", "ps1", "--db", db])
# make sure hidden tests are removed in release
with io.open(join(course_dir, "release", "ps1", "p1.ipynb"), mode='r', encoding='utf-8') as nb:
source = nb.read()
assert "BEGIN HIDDEN TESTS" not in source
self._copy_file(
join(course_dir, "release", "ps1", "p1.ipynb"),
join(course_dir, "submitted", "foo", "ps1", "p1.ipynb")
)
# make sure submitted validates, should only fail on hidden tests
output = run_nbgrader([
"validate", join(course_dir, "submitted", "foo", "ps1", "p1.ipynb")
], stdout=True)
assert output.strip() == "Success! Your notebook passes all the tests."
run_nbgrader(["autograde", "ps1", "--db", db])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
# make sure hidden tests are placed back in autograded
sub_nb = join(course_dir, "autograded", "foo", "ps1", "p1.ipynb")
with io.open(sub_nb, mode='r', encoding='utf-8') as nb:
source = nb.read()
assert "BEGIN HIDDEN TESTS" in source
# make sure autograded does not validate, should fail on hidden tests
output = run_nbgrader([
"validate", join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"),
], stdout=True)
assert output.splitlines()[0] == (
"VALIDATION FAILED ON 2 CELL(S)! If you submit your assignment "
"as it is, you WILL NOT"
)
with Gradebook(db) as gb:
submission = gb.find_submission("ps1", "foo")
nb1 = submission.notebooks[0]
assert nb1.score == 1.5
def test_handle_failure(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._empty_notebook(join(course_dir, "source", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "source", "ps1", "p2.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "bar", "ps1", "p2.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
run_nbgrader(["autograde", "ps1"], retcode=1)
assert not os.path.exists(join(course_dir, "autograded", "bar", "ps1"))
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1"))
def test_handle_failure_single_notebook(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._empty_notebook(join(course_dir, "source", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "source", "ps1", "p2.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
self._copy_file(join("files", "test.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p2.ipynb"))
run_nbgrader(["autograde", "ps1", "--notebook", "p*"], retcode=1)
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
assert not os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p2.ipynb"))
def test_missing_source_kernelspec(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.ClearSolutions.code_stub = {'python': '## Answer', 'blah': '## Answer'}""")
self._empty_notebook(join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"), kernel="python")
run_nbgrader(["autograde", "ps1"])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
self._empty_notebook(join(course_dir, "submitted", "bar", "ps1", "p1.ipynb"), kernel="blarg")
run_nbgrader(["autograde", "ps1"], retcode=1)
assert not os.path.exists(join(course_dir, "autograded", "bar", "ps1"))
def test_incorrect_source_kernelspec(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
with open("nbgrader_config.py", "a") as fh:
fh.write("""c.ClearSolutions.code_stub = {'python': '## Answer', 'blah': '## Answer'}""")
self._empty_notebook(join(course_dir, "source", "ps1", "p1.ipynb"), kernel="blah")
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"), kernel="python")
run_nbgrader(["autograde", "ps1"], retcode=1)
assert not os.path.exists(join(course_dir, "autograded", "foo", "ps1"))
def test_incorrect_submitted_kernelspec(self, db, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])
run_nbgrader(["db", "student", "add", "bar", "--db", db])
self._empty_notebook(join(course_dir, "source", "ps1", "p1.ipynb"), kernel="python")
run_nbgrader(["generate_assignment", "ps1"])
self._empty_notebook(join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"), kernel="blah")
run_nbgrader(["autograde", "ps1"])
assert os.path.exists(join(course_dir, "autograded", "foo", "ps1"))
assert os.path.isfile(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"))
def test_no_execute(self, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo"])
run_nbgrader(["db", "student", "add", "bar"])
self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1"])
self._copy_file(join("files", "test-with-output.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
with io.open(join(os.path.dirname(__file__), "files", "test-with-output.ipynb"), mode="r", encoding='utf-8') as fh:
orig_contents = reads(fh.read(), as_version=current_nbformat)
run_nbgrader(["autograde", "ps1"])
with io.open(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"), mode="r", encoding="utf-8") as fh:
new_contents = reads(fh.read(), as_version=current_nbformat)
different = False
for i in range(len(orig_contents.cells)):
orig_cell = orig_contents.cells[i]
new_cell = new_contents.cells[i]
if 'outputs' in orig_cell:
if orig_cell.outputs != new_cell.outputs:
different = True
break
elif 'outputs' in new_cell:
different = True
assert different
run_nbgrader(["autograde", "ps1", "--force", "--no-execute"])
with io.open(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"), mode="r", encoding="utf-8") as fh:
new_contents = reads(fh.read(), as_version=current_nbformat)
for i in range(len(orig_contents.cells)):
orig_cell = orig_contents.cells[i]
new_cell = new_contents.cells[i]
if 'outputs' in orig_cell:
assert orig_cell.outputs == new_cell.outputs
else:
assert 'outputs' not in new_cell
def test_many_students(self, course_dir):
pytest.skip("this test takes too long to run and requires manual configuration")
# NOTE: to test this, you will manually have to configure the postgres
# database. In the postgresql.conf file in the postgres data directory,
# set max_connections to something low (like 5). Then, create the gradebook
# database and run this test.
db = "postgresql://localhost:5432/gradebook"
run_nbgrader(["db", "assignment", "add", "ps1", "--duedate", "--db", db,
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
student_fmt = "student{:03d}"
num_students = 50
for i in range(num_students):
run_nbgrader(["db", "student", "add", student_fmt.format(i), "--db", db])
self._copy_file(join("files", "submitted-unchanged.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
run_nbgrader(["generate_assignment", "ps1", "--db", db])
for i in range(num_students):
self._copy_file(join("files", "submitted-changed.ipynb"), join(course_dir, "submitted", student_fmt.format(i), "ps1", "p1.ipynb"))
run_nbgrader(["autograde", "ps1", "--db", db])
def test_infinite_loop(self, db, course_dir):
run_nbgrader(["db", "assignment", "add", "ps1", "--db", db, "--duedate",
"2015-02-02 14:58:23.948203 America/Los_Angeles"])
run_nbgrader(["db", "student", "add", "foo", "--db", db])