How to use assertRaisesMessage method in pytest-django

Best Python code snippet using pytest-django_python

test_fractions.py

Source:test_fractions.py Github

copy

Full Screen

...88 def assertTypedEquals(self, expected, actual):89 """Asserts that both the types and values are the same."""90 self.assertEqual(type(expected), type(actual))91 self.assertEqual(expected, actual)92 def assertRaisesMessage(self, exc_type, message,93 callable, *args, **kwargs):94 """Asserts that callable(*args, **kwargs) raises exc_type(message)."""95 try:96 callable(*args, **kwargs)97 except exc_type, e:98 self.assertEqual(message, str(e))99 else:100 self.fail("%s not raised" % exc_type.__name__)101 def testInit(self):102 self.assertEqual((0, 1), _components(F()))103 self.assertEqual((7, 1), _components(F(7)))104 self.assertEqual((7, 3), _components(F(F(7, 3))))105 self.assertEqual((-1, 1), _components(F(-1, 1)))106 self.assertEqual((-1, 1), _components(F(1, -1)))107 self.assertEqual((1, 1), _components(F(-2, -2)))108 self.assertEqual((1, 2), _components(F(5, 10)))109 self.assertEqual((7, 15), _components(F(7, 15)))110 self.assertEqual((10**23, 1), _components(F(10**23)))111 self.assertEqual((3, 77), _components(F(F(3, 7), 11)))112 self.assertEqual((-9, 5), _components(F(2, F(-10, 9))))113 self.assertEqual((2486, 2485), _components(F(F(22, 7), F(355, 113))))114 self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",115 F, 12, 0)116 self.assertRaises(TypeError, F, 1.5 + 3j)117 self.assertRaises(TypeError, F, "3/2", 3)118 self.assertRaises(TypeError, F, 3, 0j)119 self.assertRaises(TypeError, F, 3, 1j)120 @requires_IEEE_754121 def testInitFromFloat(self):122 self.assertEqual((5, 2), _components(F(2.5)))123 self.assertEqual((0, 1), _components(F(-0.0)))124 self.assertEqual((3602879701896397, 36028797018963968),125 _components(F(0.1)))126 self.assertRaises(TypeError, F, float('nan'))127 self.assertRaises(TypeError, F, float('inf'))128 self.assertRaises(TypeError, F, float('-inf'))129 def testInitFromDecimal(self):130 self.assertEqual((11, 10),131 _components(F(Decimal('1.1'))))132 self.assertEqual((7, 200),133 _components(F(Decimal('3.5e-2'))))134 self.assertEqual((0, 1),135 _components(F(Decimal('.000e20'))))136 self.assertRaises(TypeError, F, Decimal('nan'))137 self.assertRaises(TypeError, F, Decimal('snan'))138 self.assertRaises(TypeError, F, Decimal('inf'))139 self.assertRaises(TypeError, F, Decimal('-inf'))140 def testFromString(self):141 self.assertEqual((5, 1), _components(F("5")))142 self.assertEqual((3, 2), _components(F("3/2")))143 self.assertEqual((3, 2), _components(F(" \n +3/2")))144 self.assertEqual((-3, 2), _components(F("-3/2 ")))145 self.assertEqual((13, 2), _components(F(" 013/02 \n ")))146 self.assertEqual((13, 2), _components(F(u" 013/02 \n ")))147 self.assertEqual((16, 5), _components(F(" 3.2 ")))148 self.assertEqual((-16, 5), _components(F(u" -3.2 ")))149 self.assertEqual((-3, 1), _components(F(u" -3. ")))150 self.assertEqual((3, 5), _components(F(u" .6 ")))151 self.assertEqual((1, 3125), _components(F("32.e-5")))152 self.assertEqual((1000000, 1), _components(F("1E+06")))153 self.assertEqual((-12300, 1), _components(F("-1.23e4")))154 self.assertEqual((0, 1), _components(F(" .0e+0\t")))155 self.assertEqual((0, 1), _components(F("-0.000e0")))156 self.assertRaisesMessage(157 ZeroDivisionError, "Fraction(3, 0)",158 F, "3/0")159 self.assertRaisesMessage(160 ValueError, "Invalid literal for Fraction: '3/'",161 F, "3/")162 self.assertRaisesMessage(163 ValueError, "Invalid literal for Fraction: '/2'",164 F, "/2")165 self.assertRaisesMessage(166 ValueError, "Invalid literal for Fraction: '3 /2'",167 F, "3 /2")168 self.assertRaisesMessage(169 # Denominators don't need a sign.170 ValueError, "Invalid literal for Fraction: '3/+2'",171 F, "3/+2")172 self.assertRaisesMessage(173 # Imitate float's parsing.174 ValueError, "Invalid literal for Fraction: '+ 3/2'",175 F, "+ 3/2")176 self.assertRaisesMessage(177 # Avoid treating '.' as a regex special character.178 ValueError, "Invalid literal for Fraction: '3a2'",179 F, "3a2")180 self.assertRaisesMessage(181 # Don't accept combinations of decimals and fractions.182 ValueError, "Invalid literal for Fraction: '3/7.2'",183 F, "3/7.2")184 self.assertRaisesMessage(185 # Don't accept combinations of decimals and fractions.186 ValueError, "Invalid literal for Fraction: '3.2/7'",187 F, "3.2/7")188 self.assertRaisesMessage(189 # Allow 3. and .3, but not .190 ValueError, "Invalid literal for Fraction: '.'",191 F, ".")192 def testImmutable(self):193 r = F(7, 3)194 r.__init__(2, 15)195 self.assertEqual((7, 3), _components(r))196 self.assertRaises(AttributeError, setattr, r, 'numerator', 12)197 self.assertRaises(AttributeError, setattr, r, 'denominator', 6)198 self.assertEqual((7, 3), _components(r))199 # But if you _really_ need to:200 r._numerator = 4201 r._denominator = 2202 self.assertEqual((4, 2), _components(r))203 # Which breaks some important operations:204 self.assertNotEqual(F(4, 2), r)205 def testFromFloat(self):206 self.assertRaises(TypeError, F.from_float, 3+4j)207 self.assertEqual((10, 1), _components(F.from_float(10)))208 bigint = 1234567890123456789209 self.assertEqual((bigint, 1), _components(F.from_float(bigint)))210 self.assertEqual((0, 1), _components(F.from_float(-0.0)))211 self.assertEqual((10, 1), _components(F.from_float(10.0)))212 self.assertEqual((-5, 2), _components(F.from_float(-2.5)))213 self.assertEqual((99999999999999991611392, 1),214 _components(F.from_float(1e23)))215 self.assertEqual(float(10**23), float(F.from_float(1e23)))216 self.assertEqual((3602879701896397, 1125899906842624),217 _components(F.from_float(3.2)))218 self.assertEqual(3.2, float(F.from_float(3.2)))219 inf = 1e1000220 nan = inf - inf221 self.assertRaisesMessage(222 TypeError, "Cannot convert inf to Fraction.",223 F.from_float, inf)224 self.assertRaisesMessage(225 TypeError, "Cannot convert -inf to Fraction.",226 F.from_float, -inf)227 self.assertRaisesMessage(228 TypeError, "Cannot convert nan to Fraction.",229 F.from_float, nan)230 def testFromDecimal(self):231 self.assertRaises(TypeError, F.from_decimal, 3+4j)232 self.assertEqual(F(10, 1), F.from_decimal(10))233 self.assertEqual(F(0), F.from_decimal(Decimal("-0")))234 self.assertEqual(F(5, 10), F.from_decimal(Decimal("0.5")))235 self.assertEqual(F(5, 1000), F.from_decimal(Decimal("5e-3")))236 self.assertEqual(F(5000), F.from_decimal(Decimal("5e3")))237 self.assertEqual(1 - F(1, 10**30),238 F.from_decimal(Decimal("0." + "9" * 30)))239 self.assertRaisesMessage(240 TypeError, "Cannot convert Infinity to Fraction.",241 F.from_decimal, Decimal("inf"))242 self.assertRaisesMessage(243 TypeError, "Cannot convert -Infinity to Fraction.",244 F.from_decimal, Decimal("-inf"))245 self.assertRaisesMessage(246 TypeError, "Cannot convert NaN to Fraction.",247 F.from_decimal, Decimal("nan"))248 self.assertRaisesMessage(249 TypeError, "Cannot convert sNaN to Fraction.",250 F.from_decimal, Decimal("snan"))251 def testLimitDenominator(self):252 rpi = F('3.1415926535897932')253 self.assertEqual(rpi.limit_denominator(10000), F(355, 113))254 self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113))255 self.assertEqual(rpi.limit_denominator(113), F(355, 113))256 self.assertEqual(rpi.limit_denominator(112), F(333, 106))257 self.assertEqual(F(201, 200).limit_denominator(100), F(1))258 self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101))259 self.assertEqual(F(0).limit_denominator(10000), F(0))260 for i in (0, -1):261 self.assertRaisesMessage(262 ValueError, "max_denominator should be at least 1",263 F(1).limit_denominator, i)264 def testConversions(self):265 self.assertTypedEquals(-1, math.trunc(F(-11, 10)))266 self.assertTypedEquals(-1, int(F(-11, 10)))267 self.assertTypedEquals(1, math.trunc(F(11, 10)))268 self.assertEqual(False, bool(F(0, 1)))269 self.assertEqual(True, bool(F(3, 2)))270 self.assertTypedEquals(0.1, float(F(1, 10)))271 # Check that __float__ isn't implemented by converting the272 # numerator and denominator to float before dividing.273 self.assertRaises(OverflowError, float, long('2'*400+'7'))274 self.assertAlmostEqual(2.0/3,275 float(F(long('2'*400+'7'), long('3'*400+'1'))))276 self.assertTypedEquals(0.1+0j, complex(F(1,10)))277 def testArithmetic(self):278 self.assertEqual(F(1, 2), F(1, 10) + F(2, 5))279 self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5))280 self.assertEqual(F(1, 25), F(1, 10) * F(2, 5))281 self.assertEqual(F(1, 4), F(1, 10) / F(2, 5))282 self.assertTypedEquals(2, F(9, 10) // F(2, 5))283 self.assertTypedEquals(10**23, F(10**23, 1) // F(1))284 self.assertEqual(F(2, 3), F(-7, 3) % F(3, 2))285 self.assertEqual(F(8, 27), F(2, 3) ** F(3))286 self.assertEqual(F(27, 8), F(2, 3) ** F(-3))287 self.assertTypedEquals(2.0, F(4) ** F(1, 2))288 self.assertEqual(F(1, 1), +F(1, 1))289 # Will return 1j in 3.0:290 self.assertRaises(ValueError, pow, F(-1), F(1, 2))291 def testMixedArithmetic(self):292 self.assertTypedEquals(F(11, 10), F(1, 10) + 1)293 self.assertTypedEquals(1.1, F(1, 10) + 1.0)294 self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j))295 self.assertTypedEquals(F(11, 10), 1 + F(1, 10))296 self.assertTypedEquals(1.1, 1.0 + F(1, 10))297 self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10))298 self.assertTypedEquals(F(-9, 10), F(1, 10) - 1)299 self.assertTypedEquals(-0.9, F(1, 10) - 1.0)300 self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j))301 self.assertTypedEquals(F(9, 10), 1 - F(1, 10))302 self.assertTypedEquals(0.9, 1.0 - F(1, 10))303 self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10))304 self.assertTypedEquals(F(1, 10), F(1, 10) * 1)305 self.assertTypedEquals(0.1, F(1, 10) * 1.0)306 self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j))307 self.assertTypedEquals(F(1, 10), 1 * F(1, 10))308 self.assertTypedEquals(0.1, 1.0 * F(1, 10))309 self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10))310 self.assertTypedEquals(F(1, 10), F(1, 10) / 1)311 self.assertTypedEquals(0.1, F(1, 10) / 1.0)312 self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j))313 self.assertTypedEquals(F(10, 1), 1 / F(1, 10))314 self.assertTypedEquals(10.0, 1.0 / F(1, 10))315 self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10))316 self.assertTypedEquals(0, F(1, 10) // 1)317 self.assertTypedEquals(0.0, F(1, 10) // 1.0)318 self.assertTypedEquals(10, 1 // F(1, 10))319 self.assertTypedEquals(10**23, 10**22 // F(1, 10))320 self.assertTypedEquals(10.0, 1.0 // F(1, 10))321 self.assertTypedEquals(F(1, 10), F(1, 10) % 1)322 self.assertTypedEquals(0.1, F(1, 10) % 1.0)323 self.assertTypedEquals(F(0, 1), 1 % F(1, 10))324 self.assertTypedEquals(0.0, 1.0 % F(1, 10))325 # No need for divmod since we don't override it.326 # ** has more interesting conversion rules.327 self.assertTypedEquals(F(100, 1), F(1, 10) ** -2)328 self.assertTypedEquals(F(100, 1), F(10, 1) ** 2)329 self.assertTypedEquals(0.1, F(1, 10) ** 1.0)330 self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j))331 self.assertTypedEquals(4 , 2 ** F(2, 1))332 # Will return 1j in 3.0:333 self.assertRaises(ValueError, pow, (-1), F(1, 2))334 self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1))335 self.assertTypedEquals(2.0 , 4 ** F(1, 2))336 self.assertTypedEquals(0.25, 2.0 ** F(-2, 1))337 self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10))338 def testMixingWithDecimal(self):339 # Decimal refuses mixed comparisons.340 self.assertRaisesMessage(341 TypeError,342 "unsupported operand type(s) for +: 'Fraction' and 'Decimal'",343 operator.add, F(3,11), Decimal('3.1415926'))344 self.assertRaisesMessage(345 TypeError,346 "unsupported operand type(s) for +: 'Decimal' and 'Fraction'",347 operator.add, Decimal('3.1415926'), F(3,11))348 self.assertNotEqual(F(5, 2), Decimal('2.5'))349 def testComparisons(self):350 self.assertTrue(F(1, 2) < F(2, 3))351 self.assertFalse(F(1, 2) < F(1, 2))352 self.assertTrue(F(1, 2) <= F(2, 3))353 self.assertTrue(F(1, 2) <= F(1, 2))354 self.assertFalse(F(2, 3) <= F(1, 2))355 self.assertTrue(F(1, 2) == F(1, 2))356 self.assertFalse(F(1, 2) == F(1, 3))357 self.assertFalse(F(1, 2) != F(1, 2))358 self.assertTrue(F(1, 2) != F(1, 3))...

Full Screen

Full Screen

test_utils.py

Source:test_utils.py Github

copy

Full Screen

...37 def check_if_group_is_lowercase(self):38 params = {39 'group': 'Sopas',40 }41 with self.assertRaisesMessage(Exception,"Group string should be lowercase"): 42 FilterUtils.generate_filters(params) 43 def test_if_time_max_is_bigger_than_time_min(self):44 params = {45 'time_min': 2,46 'time_max': 1,47 }48 with self.assertRaisesMessage(Exception,"time_min is bigger than time_max"): 49 FilterUtils.generate_filters(params) 50 def test_if_portions_max_is_bigger_than_portions_min(self):51 params = {52 'portions_min': 4,53 'portions_max': 3,54 }55 with self.assertRaisesMessage(Exception,"portions_min is bigger than portions_max"): 56 FilterUtils.generate_filters(params) 57 def test_if_favorites_max_is_bigger_than_favorites_min(self):58 params = {59 'favorites_min': 6,60 'favorites_max': 5,61 }62 with self.assertRaisesMessage(Exception,"favorites_min is bigger than favorites_max"): 63 FilterUtils.generate_filters(params) 64 def test_if_time_min_is_numeric(self):65 params = {66 'time_min': 'um',67 'time_max': 2,68 }69 with self.assertRaisesMessage(Exception,"time_min isn't numeric"): 70 FilterUtils.generate_filters(params) 71 72 def test_if_time_max_is_numeric(self):73 params = {74 'time_min': 1,75 'time_max': 'dois',76 }77 with self.assertRaisesMessage(Exception,"time_max isn't numeric"): 78 FilterUtils.generate_filters(params) 79 def test_if_time_min_is_positive(self):80 params = {81 'time_min': -1,82 'time_max': 2,83 }84 with self.assertRaisesMessage(Exception,"time_min is negative"): 85 FilterUtils.generate_filters(params) 86 def test_if_time_max_is_positive(self):87 params = {88 'time_min': 1,89 'time_max': -2,90 }91 with self.assertRaisesMessage(Exception,"time_max is negative"): 92 FilterUtils.generate_filters(params) 93 def test_if_portions_min_is_numeric(self):94 params = {95 'portions_min': 'tres',96 'portions_max': 4,97 }98 with self.assertRaisesMessage(Exception,"portions_min isn't numeric"): 99 FilterUtils.generate_filters(params) 100 def test_if_portions_max_is_numeric(self):101 params = {102 'portions_min': 3,103 'portions_max': 'quatro',104 }105 with self.assertRaisesMessage(Exception,"portions_max isn't numeric"): 106 FilterUtils.generate_filters(params) 107 def test_if_portions_min_is_positive(self):108 params = {109 'portions_min': -3,110 'portions_max': 4,111 }112 with self.assertRaisesMessage(Exception,"portions_min is negative"): 113 FilterUtils.generate_filters(params) 114 def test_if_portions_max_is_positive(self):115 params = {116 'portions_min': 3,117 'portions_max': -4,118 }119 with self.assertRaisesMessage(Exception,"portions_max is negative"): 120 FilterUtils.generate_filters(params) 121 def test_if_favorites_min_is_numeric(self):122 params = {123 'favorites_min': 'cinco',124 'favorites_max': 6,125 }126 with self.assertRaisesMessage(Exception,"favorites_min isn't numeric"): 127 FilterUtils.generate_filters(params) 128 def test_if_favorites_min_is_numeric(self):129 params = {130 'favorites_min': 5,131 'favorites_max': 'seis',132 }133 with self.assertRaisesMessage(Exception,"favorites_max isn't numeric"): 134 FilterUtils.generate_filters(params) 135 def test_if_favorites_min_is_positive(self):136 params = {137 'favorites_min': -5,138 'favorites_max': 6,139 }140 with self.assertRaisesMessage(Exception,"favorites_min is negative"): 141 FilterUtils.generate_filters(params) 142 def test_if_favorites_max_is_positive(self):143 params = {144 'favorites_min': 5,145 'favorites_max': -6,146 }147 with self.assertRaisesMessage(Exception,"favorites_max is negative"): 148 FilterUtils.generate_filters(params) 149 150 def test_get_query_by_name_without_filters(self):151 must = [152 {153 "multi_match": {154 "query": 'bolo de limão',155 "fields": [156 "recipe_title^2",157 "ingredients^2",158 "raw_text"159 ],160 "type": "most_fields",161 "fuzziness": 1...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-django automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful