tests_draganddrop.py 31.8 KB
Newer Older
1 2 3
import unittest

import draganddrop
4
from .draganddrop import PositionsCompare
5
import json
Alexander Kryklia committed
6 7 8


class Test_PositionsCompare(unittest.TestCase):
9
    """ describe"""
Alexander Kryklia committed
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

    def test_nested_list_and_list1(self):
        self.assertEqual(PositionsCompare([[1, 2], 40]), PositionsCompare([1, 3]))

    def test_nested_list_and_list2(self):
        self.assertNotEqual(PositionsCompare([1, 12]), PositionsCompare([1, 1]))

    def test_list_and_list1(self):
        self.assertNotEqual(PositionsCompare([[1, 2], 12]), PositionsCompare([1, 15]))

    def test_list_and_list2(self):
        self.assertEqual(PositionsCompare([1, 11]), PositionsCompare([1, 1]))

    def test_numerical_list_and_string_list(self):
        self.assertNotEqual(PositionsCompare([1, 2]), PositionsCompare(["1"]))

    def test_string_and_string_list1(self):
        self.assertEqual(PositionsCompare("1"), PositionsCompare(["1"]))

    def test_string_and_string_list2(self):
        self.assertEqual(PositionsCompare("abc"), PositionsCompare("abc"))

    def test_string_and_string_list3(self):
        self.assertNotEqual(PositionsCompare("abd"), PositionsCompare("abe"))

    def test_float_and_string(self):
        self.assertNotEqual(PositionsCompare([3.5, 5.7]), PositionsCompare(["1"]))

    def test_floats_and_ints(self):
        self.assertEqual(PositionsCompare([3.5, 4.5]), PositionsCompare([5, 7]))
40 41


Alexander Kryklia committed
42
class Test_DragAndDrop_Grade(unittest.TestCase):
43

44 45 46 47 48 49 50 51 52
    def test_targets_are_draggable_1(self):
        user_input = json.dumps([
            {'p': 'p_l'},
            {'up': {'first': {'p': 'p_l'}}}
        ])

        correct_answer = [
            {
                'draggables': ['p'],
53
                'targets': ['p_l', 'p_r'],
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
                'rule': 'anyof'
            },
            {
                'draggables': ['up'],
                'targets': [
                    'p_l[p][first]'
                ],
                'rule': 'anyof'
            }
        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_targets_are_draggable_2(self):
        user_input = json.dumps([
            {'p': 'p_l'},
            {'p': 'p_r'},
            {'s': 's_l'},
            {'s': 's_r'},
            {'up': {'1': {'p': 'p_l'}}},
            {'up': {'3': {'p': 'p_l'}}},
            {'up': {'1': {'p': 'p_r'}}},
            {'up': {'3': {'p': 'p_r'}}},
            {'up_and_down': {'1': {'s': 's_l'}}},
            {'up_and_down': {'1': {'s': 's_r'}}}
        ])

        correct_answer = [
            {
                'draggables': ['p'],
                'targets': ['p_l', 'p_r'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['s'],
                'targets': ['s_l', 's_r'],
                'rule': 'unordered_equal'
            },
            {
92 93
                'draggables': ['up_and_down'],
                'targets': [
94
                's_l[s][1]', 's_r[s][1]'
95 96
                ],
                'rule': 'unordered_equal'
97 98
            },
            {
99 100
                'draggables': ['up'],
                'targets': [
101
                'p_l[p][1]', 'p_l[p][3]', 'p_r[p][1]', 'p_r[p][3]'
102 103
                ],
                'rule': 'unordered_equal'
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
            }
        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_targets_are_draggable_2_manual_parsing(self):
        user_input = json.dumps([
            {'up': 'p_l[p][1]'},
            {'p': 'p_l'},
            {'up': 'p_l[p][3]'},
            {'up': 'p_r[p][1]'},
            {'p': 'p_r'},
            {'up': 'p_r[p][3]'},
            {'up_and_down': 's_l[s][1]'},
            {'s': 's_l'},
            {'up_and_down': 's_r[s][1]'},
            {'s': 's_r'}
        ])

        correct_answer = [
            {
                'draggables': ['p'],
                'targets': ['p_l', 'p_r'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['s'],
                'targets': ['s_l', 's_r'],
                'rule': 'unordered_equal'
            },
            {
134 135
                'draggables': ['up_and_down'],
                'targets': [
136
                's_l[s][1]', 's_r[s][1]'
137 138
                ],
                'rule': 'unordered_equal'
139 140
            },
            {
141 142
                'draggables': ['up'],
                'targets': [
143
                'p_l[p][1]', 'p_l[p][3]', 'p_r[p][1]', 'p_r[p][3]'
144 145
                ],
                'rule': 'unordered_equal'
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
            }
        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_targets_are_draggable_3_nested(self):
        user_input = json.dumps([
            {'molecule': 'left_side_tagret'},
            {'molecule': 'right_side_tagret'},
            {'p': {'p_target': {'molecule': 'left_side_tagret'}}},
            {'p': {'p_target': {'molecule': 'right_side_tagret'}}},
            {'s': {'s_target': {'molecule': 'left_side_tagret'}}},
            {'s': {'s_target': {'molecule': 'right_side_tagret'}}},
            {'up': {'1': {'p': {'p_target': {'molecule': 'left_side_tagret'}}}}},
            {'up': {'3': {'p': {'p_target': {'molecule': 'left_side_tagret'}}}}},
            {'up': {'1': {'p': {'p_target': {'molecule': 'right_side_tagret'}}}}},
            {'up': {'3': {'p': {'p_target': {'molecule': 'right_side_tagret'}}}}},
            {'up_and_down': {'1': {'s': {'s_target': {'molecule': 'left_side_tagret'}}}}},
            {'up_and_down': {'1': {'s': {'s_target': {'molecule': 'right_side_tagret'}}}}}
        ])

        correct_answer = [
            {
                'draggables': ['molecule'],
                'targets': ['left_side_tagret', 'right_side_tagret'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['p'],
                'targets': [
                    'left_side_tagret[molecule][p_target]',
                    'right_side_tagret[molecule][p_target]'
                ],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['s'],
                'targets': [
                    'left_side_tagret[molecule][s_target]',
                    'right_side_tagret[molecule][s_target]'
                ],
                'rule': 'unordered_equal'
            },
            {
189 190 191 192 193 194
                'draggables': ['up_and_down'],
                'targets': [
                    'left_side_tagret[molecule][s_target][s][1]',
                    'right_side_tagret[molecule][s_target][s][1]'
                ],
                'rule': 'unordered_equal'
195 196
            },
            {
197 198 199 200 201 202 203
                'draggables': ['up'],
                'targets': [
                    'left_side_tagret[molecule][p_target][p][1]',
                    'left_side_tagret[molecule][p_target][p][3]',
                    'right_side_tagret[molecule][p_target][p][1]',
                    'right_side_tagret[molecule][p_target][p][3]'],
                'rule': 'unordered_equal'
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
            }
        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_targets_are_draggable_4_real_example(self):
        user_input = json.dumps([
            {'single_draggable': 's_l'},
            {'single_draggable': 's_r'},
            {'single_draggable': 'p_sigma'},
            {'single_draggable': 'p_sigma*'},
            {'single_draggable': 's_sigma'},
            {'single_draggable': 's_sigma*'},
            {'double_draggable': 'p_pi*'},
            {'double_draggable': 'p_pi'},
            {'triple_draggable': 'p_l'},
            {'triple_draggable': 'p_r'},
            {'up': {'1': {'triple_draggable': 'p_l'}}},
            {'up': {'2': {'triple_draggable': 'p_l'}}},
            {'up': {'2': {'triple_draggable': 'p_r'}}},
            {'up': {'3': {'triple_draggable': 'p_r'}}},
            {'up_and_down': {'1': {'single_draggable': 's_l'}}},
            {'up_and_down': {'1': {'single_draggable': 's_r'}}},
            {'up_and_down': {'1': {'single_draggable': 's_sigma'}}},
            {'up_and_down': {'1': {'single_draggable': 's_sigma*'}}},
            {'up_and_down': {'1': {'double_draggable': 'p_pi'}}},
            {'up_and_down': {'2': {'double_draggable': 'p_pi'}}}
        ])

        # 10 targets:
        # s_l, s_r, p_l, p_r, s_sigma, s_sigma*, p_pi, p_sigma, p_pi*, p_sigma*
        #
        # 3 draggable objects, which have targets (internal target ids - 1, 2, 3):
        # single_draggable, double_draggable, triple_draggable
        #
        # 2 draggable objects:
        # up, up_and_down
        correct_answer = [
            {
242 243 244
                'draggables': ['triple_draggable'],
                'targets': ['p_l', 'p_r'],
                'rule': 'unordered_equal'
245 246
            },
            {
247 248 249
                'draggables': ['double_draggable'],
                'targets': ['p_pi', 'p_pi*'],
                'rule': 'unordered_equal'
250 251
            },
            {
252 253 254
                'draggables': ['single_draggable'],
                'targets': ['s_l', 's_r', 's_sigma', 's_sigma*', 'p_sigma', 'p_sigma*'],
                'rule': 'unordered_equal'
255 256
            },
            {
257 258 259 260
                'draggables': ['up'],
                'targets': ['p_l[triple_draggable][1]', 'p_l[triple_draggable][2]',
                'p_r[triple_draggable][2]', 'p_r[triple_draggable][3]'],
                'rule': 'unordered_equal'
261 262
            },
            {
263 264 265 266 267
                'draggables': ['up_and_down'],
                'targets': ['s_l[single_draggable][1]', 's_r[single_draggable][1]',
                's_sigma[single_draggable][1]', 's_sigma*[single_draggable][1]',
                'p_pi[double_draggable][1]', 'p_pi[double_draggable][2]'],
                'rule': 'unordered_equal'
268 269 270 271 272
            },

        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

273
    def test_targets_true(self):
274 275 276
        user_input = '[{"1": "t1"}, \
         {"name_with_icon": "t2"}]'
        correct_answer = {'1': 't1', 'name_with_icon': 't2'}
277 278
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

279
    def test_expect_no_actions_wrong(self):
Vasyl Nakvasiuk committed
280 281
        user_input = '[{"1": "t1"}, \
         {"name_with_icon": "t2"}]'
282 283 284 285
        correct_answer = []
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_expect_no_actions_right(self):
Vasyl Nakvasiuk committed
286
        user_input = '[]'
287 288 289
        correct_answer = []
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

290
    def test_targets_false(self):
291 292 293
        user_input = '[{"1": "t1"}, \
        {"name_with_icon": "t2"}]'
        correct_answer = {'1': 't3', 'name_with_icon': 't2'}
294 295 296
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_multiple_images_per_target_true(self):
297 298
        user_input = '[{"1": "t1"}, {"name_with_icon": "t2"}, \
        {"2": "t1"}]'
299
        correct_answer = {'1': 't1', 'name_with_icon': 't2', '2': 't1'}
300 301 302
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_multiple_images_per_target_false(self):
303 304
        user_input = '[{"1": "t1"}, {"name_with_icon": "t2"}, \
        {"2": "t1"}]'
305
        correct_answer = {'1': 't2', 'name_with_icon': 't2', '2': 't1'}
306 307 308
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_targets_and_positions(self):
309 310
        user_input = '[{"1": [10,10]}, \
         {"name_with_icon": [[10,10],4]}]'
311
        correct_answer = {'1': [10, 10], 'name_with_icon': [[10, 10], 4]}
312
        self.assertTrue(draganddrop.grade(user_input, correct_answer))
313 314

    def test_position_and_targets(self):
315
        user_input = '[{"1": "t1"}, {"name_with_icon": "t2"}]'
316
        correct_answer = {'1': 't1', 'name_with_icon': 't2'}
317
        self.assertTrue(draganddrop.grade(user_input, correct_answer))
318 319

    def test_positions_exact(self):
320
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
321
        correct_answer = {'1': [10, 10], 'name_with_icon': [20, 20]}
322 323
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

324
    def test_positions_false(self):
325
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
326
        correct_answer = {'1': [25, 25], 'name_with_icon': [20, 20]}
327 328 329
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_positions_true_in_radius(self):
330
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
331
        correct_answer = {'1': [14, 14], 'name_with_icon': [20, 20]}
332 333 334
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_positions_true_in_manual_radius(self):
335
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
336
        correct_answer = {'1': [[40, 10], 30], 'name_with_icon': [20, 20]}
337 338 339
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_positions_false_in_manual_radius(self):
340
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
341 342 343
        correct_answer = {'1': [[40, 10], 29], 'name_with_icon': [20, 20]}
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

344
    def test_correct_answer_not_has_key_from_user_answer(self):
345
        user_input = '[{"1": "t1"}, {"name_with_icon": "t2"}]'
346
        correct_answer = {'3': 't3', 'name_with_icon': 't2'}
347 348
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

Alexander Kryklia committed
349 350 351 352
    def test_anywhere(self):
        """Draggables can be places anywhere on base image.
            Place grass in the middle of the image and ant in the
            right upper corner."""
353 354
        user_input = '[{"ant":[610.5,57.449951171875]},\
            {"grass":[322.5,199.449951171875]}]'
355

356
        correct_answer = {'grass': [[300, 200], 200], 'ant': [[500, 0], 200]}
Alexander Kryklia committed
357 358
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

359 360
    def test_lcao_correct(self):
        """Describe carbon molecule in LCAO-MO"""
361
        user_input = '[{"1":"s_left"}, \
362 363 364 365
        {"5":"s_right"},{"4":"s_sigma"},{"6":"s_sigma_star"},{"7":"p_left_1"}, \
        {"8":"p_left_2"},{"10":"p_right_1"},{"9":"p_right_2"}, \
        {"2":"p_pi_1"},{"3":"p_pi_2"},{"11":"s_sigma_name"}, \
        {"13":"s_sigma_star_name"},{"15":"p_pi_name"},{"16":"p_pi_star_name"}, \
366
        {"12":"p_sigma_name"},{"14":"p_sigma_star_name"}]'
367

Alexander Kryklia committed
368
        correct_answer = [{
369 370 371 372
            'draggables': ['1', '2', '3', '4', '5', '6'],
            'targets': [
                's_left', 's_right', 's_sigma', 's_sigma_star', 'p_pi_1', 'p_pi_2'
            ],
Alexander Kryklia committed
373 374 375
            'rule': 'anyof'
        }, {
            'draggables': ['7', '8', '9', '10'],
376
            'targets': ['p_left_1', 'p_left_2', 'p_right_1', 'p_right_2'],
Alexander Kryklia committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
            'rule': 'anyof'
        }, {
            'draggables': ['11', '12'],
            'targets': ['s_sigma_name', 'p_sigma_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['13', '14'],
            'targets': ['s_sigma_star_name', 'p_sigma_star_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['15'],
            'targets': ['p_pi_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['16'],
            'targets': ['p_pi_star_name'],
            'rule': 'anyof'
        }]
395 396 397 398 399

        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_lcao_extra_element_incorrect(self):
        """Describe carbon molecule in LCAO-MO"""
400
        user_input = '[{"1":"s_left"}, \
401 402 403 404
        {"5":"s_right"},{"4":"s_sigma"},{"6":"s_sigma_star"},{"7":"p_left_1"}, \
        {"8":"p_left_2"},{"17":"p_left_3"},{"10":"p_right_1"},{"9":"p_right_2"}, \
        {"2":"p_pi_1"},{"3":"p_pi_2"},{"11":"s_sigma_name"}, \
        {"13":"s_sigma_star_name"},{"15":"p_pi_name"},{"16":"p_pi_star_name"}, \
405
        {"12":"p_sigma_name"},{"14":"p_sigma_star_name"}]'
406

Alexander Kryklia committed
407
        correct_answer = [{
408 409 410
            'draggables': ['1', '2', '3', '4', '5', '6'],
            'targets': [
                's_left', 's_right', 's_sigma', 's_sigma_star', 'p_pi_1', 'p_pi_2'
Alexander Kryklia committed
411 412 413 414
            ],
            'rule': 'anyof'
        }, {
            'draggables': ['7', '8', '9', '10'],
415
            'targets': ['p_left_1', 'p_left_2', 'p_right_1', 'p_right_2'],
Alexander Kryklia committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
            'rule': 'anyof'
        }, {
            'draggables': ['11', '12'],
            'targets': ['s_sigma_name', 'p_sigma_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['13', '14'],
            'targets': ['s_sigma_star_name', 'p_sigma_star_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['15'],
            'targets': ['p_pi_name'],
            'rule': 'anyof'
        }, {
            'draggables': ['16'],
            'targets': ['p_pi_star_name'],
            'rule': 'anyof'
        }]
434 435 436

        self.assertFalse(draganddrop.grade(user_input, correct_answer))

437 438
    def test_reuse_draggable_no_mupliples(self):
        """Test reusable draggables (no mupltiple draggables per target)"""
439
        user_input = '[{"1":"target1"}, \
440
        {"2":"target2"},{"1":"target3"},{"2":"target4"},{"2":"target5"}, \
441
        {"3":"target6"}]'
442
        correct_answer = [
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
            {
                'draggables': ['1'],
                'targets': ['target1', 'target3'],
                'rule': 'anyof'
            },
            {
                'draggables': ['2'],
                'targets': ['target2', 'target4', 'target5'],
                'rule': 'anyof'
            },
            {
                'draggables': ['3'],
                'targets': ['target6'],
                'rule': 'anyof'
            }
        ]
459 460 461 462
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_reuse_draggable_with_mupliples(self):
        """Test reusable draggables with mupltiple draggables per target"""
463
        user_input = '[{"1":"target1"}, \
464
        {"2":"target2"},{"1":"target1"},{"2":"target4"},{"2":"target4"}, \
465
        {"3":"target6"}]'
466
        correct_answer = [
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
            {
                'draggables': ['1'],
                'targets': ['target1', 'target3'],
                'rule': 'anyof'
            },
            {
                'draggables': ['2'],
                'targets': ['target2', 'target4'],
                'rule': 'anyof'
            },
            {
                'draggables': ['3'],
                'targets': ['target6'],
                'rule': 'anyof'
            }
        ]
483 484 485 486
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_reuse_many_draggable_with_mupliples(self):
        """Test reusable draggables with mupltiple draggables per target"""
487
        user_input = '[{"1":"target1"}, \
488 489
        {"2":"target2"},{"1":"target1"},{"2":"target4"},{"2":"target4"}, \
        {"3":"target6"}, {"4": "target3"}, {"5": "target4"}, \
490
        {"5": "target5"}, {"6": "target2"}]'
491
        correct_answer = [
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
            {
                'draggables': ['1', '4'],
                'targets': ['target1', 'target3'],
                'rule': 'anyof'
            },
            {
                'draggables': ['2', '6'],
                'targets': ['target2', 'target4'],
                'rule': 'anyof'
            },
            {
                'draggables': ['5'],
                'targets': ['target4', 'target5'],
                'rule': 'anyof'
            },
            {
                'draggables': ['3'],
                'targets': ['target6'],
                'rule': 'anyof'
            }
        ]
513 514 515 516
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_reuse_many_draggable_with_mupliples_wrong(self):
        """Test reusable draggables with mupltiple draggables per target"""
517
        user_input = '[{"1":"target1"}, \
518 519 520 521
        {"2":"target2"},{"1":"target1"}, \
        {"2":"target3"}, \
        {"2":"target4"}, \
        {"3":"target6"}, {"4": "target3"}, {"5": "target4"}, \
522
        {"5": "target5"}, {"6": "target2"}]'
523
        correct_answer = [
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            {
                'draggables': ['1', '4'],
                'targets': ['target1', 'target3'],
                'rule': 'anyof'
            },
            {
                'draggables': ['2', '6'],
                'targets': ['target2', 'target4'],
                'rule': 'anyof'
            },
            {
                'draggables': ['5'],
                'targets': ['target4', 'target5'],
                'rule': 'anyof'
            },
            {
                'draggables': ['3'],
                'targets': ['target6'],
                'rule': 'anyof'
            }]
544 545 546 547
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_false(self):
        """Test reusable draggables (no mupltiple draggables per target)"""
548
        user_input = '[{"a":"target1"}, \
549 550
        {"b":"target2"},{"c":"target3"},{"a":"target4"},{"b":"target5"}, \
        {"c":"target6"}, {"a":"target7"},{"b":"target8"},{"c":"target9"}, \
551
         {"a":"target1"}]'
552
        correct_answer = [
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
            {
                'draggables': ['a'],
                'targets': ['target1', 'target4', 'target7', 'target10'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'unordered_equal'
            }
        ]
569 570 571 572
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_(self):
        """Test reusable draggables (no mupltiple draggables per target)"""
573
        user_input = '[{"a":"target1"}, \
574 575
        {"b":"target2"},{"c":"target3"},{"a":"target4"},{"b":"target5"}, \
        {"c":"target6"}, {"a":"target7"},{"b":"target8"},{"c":"target9"}, \
576
         {"a":"target10"}]'
577
        correct_answer = [
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
            {
                'draggables': ['a'],
                'targets': ['target1', 'target4', 'target7', 'target10'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'unordered_equal'
            },
            {
                'draggables': ['c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'unordered_equal'
            }
        ]
594 595 596 597
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_multiple(self):
        """Test reusable draggables  (mupltiple draggables per target)"""
598
        user_input = '[{"a":"target1"}, \
599 600
        {"b":"target2"},{"c":"target3"},{"b":"target5"}, \
        {"c":"target6"}, {"a":"target7"},{"b":"target8"},{"c":"target9"}, \
601
         {"a":"target1"}]'
602
        correct_answer = [
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
            {
                'draggables': ['a', 'a', 'a'],
                'targets': ['target1', 'target4', 'target7', 'target10'],
                'rule': 'anyof+number'
            },
            {
                'draggables': ['b', 'b', 'b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'anyof+number'
            },
            {
                'draggables': ['c', 'c', 'c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'anyof+number'
            }
        ]
619 620 621 622
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_multiple_false(self):
        """Test reusable draggables  (mupltiple draggables per target)"""
623
        user_input = '[{"a":"target1"}, \
624 625
        {"b":"target2"},{"c":"target3"},{"a":"target4"},{"b":"target5"}, \
        {"c":"target6"}, {"a":"target7"},{"b":"target8"},{"c":"target9"}, \
626
         {"a":"target1"}]'
627
        correct_answer = [
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
            {
                'draggables': ['a', 'a', 'a'],
                'targets': ['target1', 'target4', 'target7', 'target10'],
                'rule': 'anyof+number'
            },
            {
                'draggables': ['b', 'b', 'b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'anyof+number'
            },
            {
                'draggables': ['c', 'c', 'c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'anyof+number'
            }
        ]
644 645 646
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_reused(self):
Alexander Kryklia committed
647
        """Test a b c in 10 labels reused"""
648
        user_input = '[{"a":"target1"}, \
649 650
        {"b":"target2"},{"c":"target3"},{"b":"target5"}, \
        {"c":"target6"}, {"b":"target8"},{"c":"target9"}, \
651
         {"a":"target10"}]'
652
        correct_answer = [
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
            {
                'draggables': ['a', 'a'],
                'targets': ['target1', 'target10'],
                'rule': 'unordered_equal+number'
            },
            {
                'draggables': ['b', 'b', 'b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'unordered_equal+number'
            },
            {
                'draggables': ['c', 'c', 'c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'unordered_equal+number'
            }
        ]
669 670 671
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_label_10_targets_with_a_b_c_reused_false(self):
Alexander Kryklia committed
672
        """Test a b c in 10 labels reused false"""
673
        user_input = '[{"a":"target1"}, \
674 675
        {"b":"target2"},{"c":"target3"},{"b":"target5"}, {"a":"target8"},\
        {"c":"target6"}, {"b":"target8"},{"c":"target9"}, \
676
         {"a":"target10"}]'
677
        correct_answer = [
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
            {
                'draggables': ['a', 'a'],
                'targets': ['target1', 'target10'],
                'rule': 'unordered_equal+number'
            },
            {
                'draggables': ['b', 'b', 'b'],
                'targets': ['target2', 'target5', 'target8'],
                'rule': 'unordered_equal+number'
            },
            {
                'draggables': ['c', 'c', 'c'],
                'targets': ['target3', 'target6', 'target9'],
                'rule': 'unordered_equal+number'
            }
        ]
694
        self.assertFalse(draganddrop.grade(user_input, correct_answer))
695

Alexander Kryklia committed
696 697
    def test_mixed_reuse_and_not_reuse(self):
        """Test reusable draggables """
698
        user_input = '[{"a":"target1"}, \
Alexander Kryklia committed
699
        {"b":"target2"},{"c":"target3"}, {"a":"target4"},\
700
         {"a":"target5"}]'
Alexander Kryklia committed
701
        correct_answer = [
702 703 704 705 706 707 708 709 710 711 712
            {
                'draggables': ['a', 'b'],
                'targets': ['target1', 'target2', 'target4', 'target5'],
                'rule': 'anyof'
            },
            {
                'draggables': ['c'],
                'targets': ['target3'],
                'rule': 'exact'
            }
        ]
Alexander Kryklia committed
713 714 715 716
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_mixed_reuse_and_not_reuse_number(self):
        """Test reusable draggables with number """
717 718
        user_input = '[{"a":"target1"}, \
        {"b":"target2"},{"c":"target3"}, {"a":"target4"}]'
Alexander Kryklia committed
719
        correct_answer = [
720 721 722 723 724 725 726 727 728 729 730
            {
                'draggables': ['a', 'a', 'b'],
                'targets': ['target1', 'target2', 'target4'],
                'rule': 'anyof+number'
            },
            {
                'draggables': ['c'],
                'targets': ['target3'],
                'rule': 'exact'
            }
        ]
Alexander Kryklia committed
731 732 733 734
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

    def test_mixed_reuse_and_not_reuse_number_false(self):
        """Test reusable draggables with numbers, but wrong"""
735 736
        user_input = '[{"a":"target1"}, \
        {"b":"target2"},{"c":"target3"}, {"a":"target4"}, {"a":"target10"}]'
Alexander Kryklia committed
737
        correct_answer = [
738 739 740 741 742 743 744 745 746 747 748
            {
                'draggables': ['a', 'a', 'b'],
                'targets': ['target1', 'target2', 'target4', 'target10'],
                'rule': 'anyof_number'
            },
            {
                'draggables': ['c'],
                'targets': ['target3'],
                'rule': 'exact'
            }
        ]
Alexander Kryklia committed
749 750
        self.assertFalse(draganddrop.grade(user_input, correct_answer))

751
    def test_alternative_correct_answer(self):
752
        user_input = '[{"name_with_icon":"t1"},\
753
        {"name_with_icon":"t1"},{"name_with_icon":"t1"},{"name4":"t1"}, \
754
        {"name4":"t1"}]'
755
        correct_answer = [
756 757 758
            {'draggables': ['name4'], 'targets': ['t1', 't1'], 'rule': 'exact'},
            {'draggables': ['name_with_icon'], 'targets': ['t1', 't1', 't1'],
                'rule': 'exact'}
759 760 761
        ]
        self.assertTrue(draganddrop.grade(user_input, correct_answer))

Alexander Kryklia committed
762

Alexander Kryklia committed
763
class Test_DragAndDrop_Populate(unittest.TestCase):
Alexander Kryklia committed
764

765 766
    def test_1(self):
        correct_answer = {'1': [[40, 10], 29], 'name_with_icon': [20, 20]}
767
        user_input = '[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]'
Alexander Kryklia committed
768
        dnd = draganddrop.DragAndDrop(correct_answer, user_input)
769

770 771 772 773
        correct_groups = [['1'], ['name_with_icon']]
        correct_positions = [{'exact': [[[40, 10], 29]]}, {'exact': [[20, 20]]}]
        user_groups = [['1'], ['name_with_icon']]
        user_positions = [{'user': [[10, 10]]}, {'user': [[20, 20]]}]
774 775 776 777 778

        self.assertEqual(correct_groups, dnd.correct_groups)
        self.assertEqual(correct_positions, dnd.correct_positions)
        self.assertEqual(user_groups, dnd.user_groups)
        self.assertEqual(user_positions, dnd.user_positions)
Alexander Kryklia committed
779 780 781 782


class Test_DraAndDrop_Compare_Positions(unittest.TestCase):

783
    def test_1(self):
784
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
785 786 787 788 789
        self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]],
                                              user=[[2, 3], [1, 1]],
                                              flag='anyof'))

    def test_2a(self):
790
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
791
        self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]],
792 793
                                              user=[[2, 3], [1, 1]],
                                              flag='exact'))
Alexander Kryklia committed
794

795
    def test_2b(self):
796
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
797 798 799
        self.assertFalse(dnd.compare_positions(correct=[[1, 1], [2, 3]],
                                               user=[[2, 13], [1, 1]],
                                               flag='exact'))
Alexander Kryklia committed
800

801
    def test_3(self):
802
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
803 804 805
        self.assertFalse(dnd.compare_positions(correct=["a", "b"],
                                               user=["a", "b", "c"],
                                               flag='anyof'))
Alexander Kryklia committed
806

807
    def test_4(self):
808
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
809 810 811
        self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"],
                                              user=["a", "b"],
                                              flag='anyof'))
Alexander Kryklia committed
812 813

    def test_5(self):
814
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
815 816 817 818 819
        self.assertFalse(dnd.compare_positions(correct=["a", "b", "c"],
                                               user=["a", "c", "b"],
                                               flag='exact'))

    def test_6(self):
820
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
821
        self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"],
822 823
                                              user=["a", "c", "b"],
                                              flag='anyof'))
824 825

    def test_7(self):
826
        dnd = draganddrop.DragAndDrop({'1': 't1'}, '[{"1": "t1"}]')
827 828 829
        self.assertFalse(dnd.compare_positions(correct=["a", "b", "b"],
                                               user=["a", "c", "b"],
                                               flag='anyof'))
Alexander Kryklia committed
830

Alexander Kryklia committed
831

832 833
def suite():

Alexander Kryklia committed
834
    testcases = [Test_PositionsCompare,
835 836 837
                 Test_DragAndDrop_Populate,
                 Test_DragAndDrop_Grade,
                 Test_DraAndDrop_Compare_Positions]
838 839 840 841 842 843 844
    suites = []
    for testcase in testcases:
        suites.append(unittest.TestLoader().loadTestsFromTestCase(testcase))
    return unittest.TestSuite(suites)

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(suite())