Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# noinspection PyPackageRequirements 

import re 

 

import pytest 

from django.db.models import Count, OuterRef, Exists, ExpressionWrapper, Value, BooleanField 

 

from .app.models import Container, ItemA 

 

 

@pytest.mark.django_db(transaction=True) 

class TestExistsQuery: 

# region Tests 

 

def test_forward_foreign_key(self): 

inner_query = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query.filter(pk=OuterRef('parent')))) \ 

.filter(__ex=True) 

assert str(outer_query.query), """ 

SELECT "app_itema"."id", "app_itema"."name", "app_itema"."parent_id",  

EXISTS( 

SELECT U0."id", U0."name", U0."item_c_id", True AS "__ex" FROM "app_container" U0  

WHERE (U0."id" IN (1, 2, 3, 4, 5) AND U0."id" = ("app_itema"."parent_id"))) AS "__ex"  

FROM "app_itema"  

WHERE EXISTS( 

SELECT U0."id", U0."name", U0."item_c_id", True AS "__ex" FROM "app_container" U0  

WHERE (U0."id" IN (1, 2, 3, 4, 5) AND U0."id" = ("app_itema"."parent_id")) 

) = True  

""".strip() 

 

@pytest.mark.xfail(reason=""" 

Currently OuterRef fails silently on union,  

just marking that fact to be notified when this is fixed in django 

""", strict=True) 

def test_outer_ref_on_union(self): 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query1.union(inner_query2).filter(pk=OuterRef('parent')))) \ 

.filter(__ex=True) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query)) 

 

@pytest.mark.xfail(reason=""" 

Currently OuterRef fails silently on union,  

just marking that fact to be notified when this is fixed in django 

""", strict=True) 

def test_outer_ref_on_union_parts(self): 

 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query1.union(inner_query2))) \ 

.filter(__ex=True) 

 

print(outer_query.query) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query)) 

 

def test_outer_ref_on_join_parts(self): 

 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query1 | inner_query2)) \ 

.filter(__ex=True) 

 

print(outer_query.query) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query)) 

 

def test_outer_ref_on_join(self): 

 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

 

outer_query = ItemA.objects.annotate(__ex=Exists((inner_query1 | inner_query2).filter(pk=OuterRef('parent')))) \ 

.filter(__ex=True) 

 

print(outer_query.query) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query)) 

 

@pytest.mark.xfail(reason=""" 

Currently OuterRef fails silently on intersection,  

just marking that fact to be notified when this is fixed in django 

""", strict=True) 

def test_outer_ref_on_intersection(self): 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query1.intersection(inner_query2).filter(pk=OuterRef('parent')))) \ 

.filter(__ex=True) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query)) 

 

@pytest.mark.xfail(reason=""" 

Currently OuterRef fails silently on intersection,  

just marking that fact to be notified when this is fixed in django 

""", strict=True) 

def test_outer_ref_on_intersection_parts(self): 

 

inner_query1 = Container.objects.filter(id__in=(1, 2, 3, 4, 5)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

inner_query2 = Container.objects.filter(id__in=(6,)) \ 

.annotate(__ex=ExpressionWrapper(Value(True), output_field=BooleanField())) \ 

.filter(pk=OuterRef('parent')) 

 

outer_query = ItemA.objects.annotate(__ex=Exists(inner_query1.intersection(inner_query2))) \ 

.filter(__ex=True) 

 

print(outer_query.query) 

 

assert re.search(r'.*."id" = (.*."parent_id")', str(outer_query.query))