Coverage for databases/tests/arrays/test_decimal.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from decimal import Decimal 

2 

3import pytest 

4 

5from prisma import Prisma 

6 

7 

8@pytest.mark.asyncio 

9async def test_updating_decimal(client: Prisma) -> None: 

10 """Updating a Decimal[] value""" 

11 models = [ 

12 await client.lists.create({}), 

13 await client.lists.create( 

14 data={ 

15 'decimals': [Decimal('22.99'), Decimal('30.01')], 

16 }, 

17 ), 

18 ] 

19 

20 model = await client.lists.update( 

21 where={ 

22 'id': models[1].id, 

23 }, 

24 data={ 

25 'decimals': { 

26 'set': [Decimal('3')], 

27 }, 

28 }, 

29 ) 

30 assert model is not None 

31 assert model.decimals == [Decimal('3')] 

32 

33 model = await client.lists.update( 

34 where={ 

35 'id': models[1].id, 

36 }, 

37 data={ 

38 'decimals': [Decimal('7')], 

39 }, 

40 ) 

41 assert model is not None 

42 assert model.decimals == [Decimal('7')] 

43 

44 

45@pytest.mark.asyncio 

46async def test_filtering_decimal(client: Prisma) -> None: 

47 """Searching for records by a Decimal[] value""" 

48 async with client.batch_() as batcher: 

49 batcher.lists.create({}) 

50 batcher.lists.create( 

51 data={ 

52 'decimals': [], 

53 }, 

54 ) 

55 batcher.lists.create( 

56 data={ 

57 'decimals': [Decimal('1'), Decimal('2')], 

58 }, 

59 ) 

60 

61 model = await client.lists.find_first( 

62 where={ 

63 'decimals': { 

64 'equals': None, 

65 }, 

66 }, 

67 ) 

68 assert model is not None 

69 assert model.decimals == [] 

70 

71 model = await client.lists.find_first( 

72 where={ 

73 'decimals': { 

74 'equals': [Decimal('1'), Decimal('2')], 

75 }, 

76 }, 

77 ) 

78 assert model is not None 

79 assert model.decimals == [Decimal(1), Decimal(2)] 

80 

81 model = await client.lists.find_first( 

82 where={ 

83 'decimals': { 

84 'has': Decimal('1'), 

85 }, 

86 }, 

87 ) 

88 assert model is not None 

89 assert model.decimals == [Decimal(1), Decimal(2)] 

90 

91 model = await client.lists.find_first( 

92 where={ 

93 'decimals': { 

94 'has': Decimal(3), 

95 }, 

96 }, 

97 ) 

98 assert model is None 

99 

100 model = await client.lists.find_first( 

101 where={ 

102 'decimals': { 

103 'has_some': [Decimal(1), Decimal(3)], 

104 }, 

105 }, 

106 ) 

107 assert model is not None 

108 assert model.decimals == [Decimal(1), Decimal(2)] 

109 

110 model = await client.lists.find_first( 

111 where={ 

112 'decimals': { 

113 'has_every': [Decimal(1), Decimal(2), Decimal(3)], 

114 }, 

115 }, 

116 ) 

117 assert model is None 

118 

119 model = await client.lists.find_first( 

120 where={ 

121 'decimals': { 

122 'has_every': [Decimal(1)], 

123 }, 

124 }, 

125 ) 

126 assert model is not None 

127 assert model.decimals == [Decimal(1), Decimal(2)] 

128 

129 count = await client.lists.count( 

130 where={ 

131 'decimals': { 

132 'is_empty': True, 

133 }, 

134 }, 

135 ) 

136 assert count == 1