| 481 | | ## def test_7_Multirecall(self): |
|---|
| 482 | | ## box = arena.new_sandbox() |
|---|
| 483 | | ## try: |
|---|
| 484 | | ## f = (lambda z, a: z.Name == 'San Diego Zoo') |
|---|
| 485 | | ## zooed_animals = box.recall(Zoo & Animal, f) |
|---|
| 486 | | ## self.assertEqual(len(zooed_animals), 2) |
|---|
| 487 | | ## |
|---|
| 488 | | ## SDZ = box.unit(Zoo, Name='San Diego Zoo') |
|---|
| 489 | | ## aid = 0 |
|---|
| 490 | | ## for z, a in zooed_animals: |
|---|
| 491 | | ## self.assertEqual(id(z), id(SDZ)) |
|---|
| 492 | | ## self.assertNotEqual(id(a), aid) |
|---|
| 493 | | ## aid = id(a) |
|---|
| 494 | | ## |
|---|
| 495 | | ## # Assert that multirecalls with no matching related units returns |
|---|
| 496 | | ## # no matches for the initial class, since all joins are INNER. |
|---|
| 497 | | ## # We're also going to test that you can combine a one-arg expr |
|---|
| 498 | | ## # with a two-arg expr. |
|---|
| 499 | | ## sdexpr = logic.filter(Name='San Diego Zoo') |
|---|
| 500 | | ## leo = lambda z, a: a.Species == 'Leopard' |
|---|
| 501 | | ## zooed_animals = box.recall(Zoo & Animal, sdexpr + leo) |
|---|
| 502 | | ## self.assertEqual(len(zooed_animals), 0) |
|---|
| 503 | | ## |
|---|
| 504 | | ## # Now try the same expr with INNER, LEFT, and RIGHT JOINs. |
|---|
| 505 | | ## zooed_animals = box.recall(Zoo & Animal) |
|---|
| 506 | | ## self.assertEqual(len(zooed_animals), 6) |
|---|
| 507 | | ## self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), |
|---|
| 508 | | ## set([("Wild Animal Park", "Leopard"), |
|---|
| 509 | | ## ("Wild Animal Park", "Lion"), |
|---|
| 510 | | ## ("San Diego Zoo", "Tiger"), |
|---|
| 511 | | ## ("San Diego Zoo", "Millipede"), |
|---|
| 512 | | ## ("Sea_World", "Emperor Penguin"), |
|---|
| 513 | | ## ("Sea_World", "Adelie Penguin")])) |
|---|
| 514 | | ## |
|---|
| 515 | | ## zooed_animals = box.recall(Zoo >> Animal) |
|---|
| 516 | | ## self.assertEqual(len(zooed_animals), 12) |
|---|
| 517 | | ## self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), |
|---|
| 518 | | ## set([("Wild Animal Park", "Leopard"), |
|---|
| 519 | | ## ("Wild Animal Park", "Lion"), |
|---|
| 520 | | ## ("San Diego Zoo", "Tiger"), |
|---|
| 521 | | ## ("San Diego Zoo", "Millipede"), |
|---|
| 522 | | ## ("Sea_World", "Emperor Penguin"), |
|---|
| 523 | | ## ("Sea_World", "Adelie Penguin"), |
|---|
| 524 | | ## (None, "Slug"), |
|---|
| 525 | | ## (None, "Bear"), |
|---|
| 526 | | ## (None, "Ostrich"), |
|---|
| 527 | | ## (None, "Centipede"), |
|---|
| 528 | | ## (None, "Ape"), |
|---|
| 529 | | ## (None, "Ape"), |
|---|
| 530 | | ## ])) |
|---|
| 531 | | ## |
|---|
| 532 | | ## zooed_animals = box.recall(Zoo << Animal) |
|---|
| 533 | | ## self.assertEqual(len(zooed_animals), 7) |
|---|
| 534 | | ## self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), |
|---|
| 535 | | ## set([("Wild Animal Park", "Leopard"), |
|---|
| 536 | | ## ("Wild Animal Park", "Lion"), |
|---|
| 537 | | ## ("San Diego Zoo", "Tiger"), |
|---|
| 538 | | ## ("San Diego Zoo", "Millipede"), |
|---|
| 539 | | ## ("Sea_World", "Emperor Penguin"), |
|---|
| 540 | | ## ("Sea_World", "Adelie Penguin"), |
|---|
| 541 | | ## (u'Montr\xe9al Biod\xf4me', None), |
|---|
| 542 | | ## ])) |
|---|
| 543 | | ## |
|---|
| 544 | | ## # Try a multiple-arg expression |
|---|
| 545 | | ## f = (lambda a, z: a.Legs >= 4 and z.Admission < 10) |
|---|
| 546 | | ## animal_zoos = box.recall(Animal & Zoo, f) |
|---|
| 547 | | ## self.assertEqual(len(animal_zoos), 4) |
|---|
| 548 | | ## names = [a.Species for a, z in animal_zoos] |
|---|
| 549 | | ## names.sort() |
|---|
| 550 | | ## self.assertEqual(names, ['Leopard', 'Lion', 'Millipede', 'Tiger']) |
|---|
| 551 | | ## |
|---|
| 552 | | ## # Let's try three joined classes just for the sadistic fun of it. |
|---|
| 553 | | ## tree = (Animal >> Zoo) >> Vet |
|---|
| 554 | | ## f = (lambda a, z, v: z.Name == 'Sea_World') |
|---|
| 555 | | ## self.assertEqual(len(box.recall(tree, f)), 2) |
|---|
| 556 | | ## |
|---|
| | 484 | def test_7_Multiview(self): |
|---|
| | 485 | try: |
|---|
| | 486 | f = (lambda z, a: z.Name == 'San Diego Zoo') |
|---|
| | 487 | zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], |
|---|
| | 488 | [('ID', ), None], f)) |
|---|
| | 489 | self.assertEqual(len(zooed_animals), 2) |
|---|
| | 490 | |
|---|
| | 491 | SDZ = schema['Zoo'].select(Name='San Diego Zoo') |
|---|
| | 492 | d = [] |
|---|
| | 493 | for row in zooed_animals: |
|---|
| | 494 | self.assertEqual(row[0], SDZ['ID']) |
|---|
| | 495 | self.assertNotEqual(row, d) |
|---|
| | 496 | d = row |
|---|
| | 497 | |
|---|
| | 498 | # Assert that multiviews with no matching related units returns |
|---|
| | 499 | # no matches for the initial class (if joins are INNER). |
|---|
| | 500 | # We're also going to test that you can combine a one-arg expr |
|---|
| | 501 | # with a two-arg expr. |
|---|
| | 502 | sdexpr = logic.filter(Name='San Diego Zoo') |
|---|
| | 503 | leo = lambda z, a: a.Species == 'Leopard' |
|---|
| | 504 | zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], |
|---|
| | 505 | None, sdexpr + leo)) |
|---|
| | 506 | self.assertEqual(len(zooed_animals), 0) |
|---|
| | 507 | |
|---|
| | 508 | # Now try the same query with INNER, LEFT, and RIGHT JOINs. |
|---|
| | 509 | zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], |
|---|
| | 510 | [('Name', ), ('Species', )])) |
|---|
| | 511 | self.assertEqual(len(zooed_animals), 6) |
|---|
| | 512 | self.assertEqual(set([tuple(row) for row in zooed_animals]), |
|---|
| | 513 | set([("Wild Animal Park", "Leopard"), |
|---|
| | 514 | ("Wild Animal Park", "Lion"), |
|---|
| | 515 | ("San Diego Zoo", "Tiger"), |
|---|
| | 516 | ("San Diego Zoo", "Millipede"), |
|---|
| | 517 | ("Sea_World", "Emperor Penguin"), |
|---|
| | 518 | ("Sea_World", "Adelie Penguin")])) |
|---|
| | 519 | |
|---|
| | 520 | zooed_animals = list(db.select(schema['Zoo'] >> schema['Animal'], |
|---|
| | 521 | [('Name', ), ('Species', )])) |
|---|
| | 522 | self.assertEqual(len(zooed_animals), 12) |
|---|
| | 523 | self.assertEqual(set([tuple(row) for row in zooed_animals]), |
|---|
| | 524 | set([("Wild Animal Park", "Leopard"), |
|---|
| | 525 | ("Wild Animal Park", "Lion"), |
|---|
| | 526 | ("San Diego Zoo", "Tiger"), |
|---|
| | 527 | ("San Diego Zoo", "Millipede"), |
|---|
| | 528 | ("Sea_World", "Emperor Penguin"), |
|---|
| | 529 | ("Sea_World", "Adelie Penguin"), |
|---|
| | 530 | (None, "Slug"), |
|---|
| | 531 | (None, "Bear"), |
|---|
| | 532 | (None, "Ostrich"), |
|---|
| | 533 | (None, "Centipede"), |
|---|
| | 534 | (None, "Ape"), |
|---|
| | 535 | (None, "Ape"), |
|---|
| | 536 | ])) |
|---|
| | 537 | |
|---|
| | 538 | zooed_animals = list(db.select(schema['Zoo'] << schema['Animal'], |
|---|
| | 539 | [('Name', ), ('Species', )])) |
|---|
| | 540 | self.assertEqual(len(zooed_animals), 7) |
|---|
| | 541 | self.assertEqual(set([tuple(row) for row in zooed_animals]), |
|---|
| | 542 | set([("Wild Animal Park", "Leopard"), |
|---|
| | 543 | ("Wild Animal Park", "Lion"), |
|---|
| | 544 | ("San Diego Zoo", "Tiger"), |
|---|
| | 545 | ("San Diego Zoo", "Millipede"), |
|---|
| | 546 | ("Sea_World", "Emperor Penguin"), |
|---|
| | 547 | ("Sea_World", "Adelie Penguin"), |
|---|
| | 548 | (u'Montr\xe9al Biod\xf4me', None), |
|---|
| | 549 | ])) |
|---|
| | 550 | |
|---|
| | 551 | # Try a multiple-arg expression |
|---|
| | 552 | f = (lambda a, z: a.Legs >= 4 and z.Admission < 10) |
|---|
| | 553 | animal_zoos = list(db.select(schema['Animal'] & schema['Zoo'], |
|---|
| | 554 | [('Species',), None], |
|---|
| | 555 | f)) |
|---|
| | 556 | self.assertEqual(len(animal_zoos), 4) |
|---|
| | 557 | names = [row[0] for row in animal_zoos] |
|---|
| | 558 | names.sort() |
|---|
| | 559 | self.assertEqual(names, ['Leopard', 'Lion', 'Millipede', 'Tiger']) |
|---|
| | 560 | |
|---|
| | 561 | # Let's try three joined classes just for the sadistic fun of it. |
|---|
| | 562 | tree = (schema['Animal'] >> schema['Zoo']) >> schema['Vet'] |
|---|
| | 563 | f = (lambda a, z, v: z.Name == 'Sea_World') |
|---|
| | 564 | self.assertEqual(len(list(db.select(tree, None, f))), 2) |
|---|
| | 565 | |
|---|