Best Python code snippet using hypothesis
test_widgets.py
Source:test_widgets.py
...126 <li><a href="?price=test-val1">test-label1</a></li>127 <li><a href="?price=test-val2">test-label2</a></li>128 </ul>""",129 )130 def test_widget_value_from_datadict(self):131 w = LinkWidget()132 data = {"price": "test-val1"}133 result = w.value_from_datadict(data, {}, "price")134 self.assertEqual(result, "test-val1")135class SuffixedMultiWidgetTests(TestCase):136 def test_assertions(self):137 # number of widgets must match suffixes138 with self.assertRaises(AssertionError):139 SuffixedMultiWidget(widgets=[BooleanWidget])140 # suffixes must be unique141 class W(SuffixedMultiWidget):142 suffixes = ["a", "a"]143 with self.assertRaises(AssertionError):144 W(widgets=[BooleanWidget, BooleanWidget])145 # should succeed146 class W(SuffixedMultiWidget):147 suffixes = ["a", "b"]148 W(widgets=[BooleanWidget, BooleanWidget])149 def test_render(self):150 class W(SuffixedMultiWidget):151 suffixes = ["min", "max"]152 w = W(widgets=[TextInput, TextInput])153 self.assertHTMLEqual(154 w.render("price", ""),155 """156 <input name="price_min" type="text" />157 <input name="price_max" type="text" />158 """,159 )160 # blank suffix161 class W(SuffixedMultiWidget):162 suffixes = [None, "lookup"]163 w = W(widgets=[TextInput, TextInput])164 self.assertHTMLEqual(165 w.render("price", ""),166 """167 <input name="price" type="text" />168 <input name="price_lookup" type="text" />169 """,170 )171 def test_value_from_datadict(self):172 class W(SuffixedMultiWidget):173 suffixes = ["min", "max"]174 w = W(widgets=[TextInput, TextInput])175 result = w.value_from_datadict(176 {177 "price_min": "1",178 "price_max": "2",179 },180 {},181 "price",182 )183 self.assertEqual(result, ["1", "2"])184 result = w.value_from_datadict({}, {}, "price")185 self.assertEqual(result, [None, None])186 # blank suffix187 class W(SuffixedMultiWidget):188 suffixes = ["", "lookup"]189 w = W(widgets=[TextInput, TextInput])190 result = w.value_from_datadict(191 {192 "price": "1",193 "price_lookup": "lt",194 },195 {},196 "price",197 )198 self.assertEqual(result, ["1", "lt"])199 def test_value_omitted_from_data(self):200 class A(SuffixedMultiWidget):201 suffixes = ["b"]202 a = A(widgets=[BooleanWidget])203 result = a.value_omitted_from_data([], None, "test")204 self.assertIsNotNone(result)205 def test_replace_name(self):206 class A(SuffixedMultiWidget):207 suffixes = ["test"]208 a = A(widgets=[None])209 output = '<div name="test123_0"></div>'210 index = 0211 q = a.replace_name(output, index)212 self.assertEqual(q, '<div name="test123_test"></div>')213 def test_decompress_value_none(self):214 class A(SuffixedMultiWidget):215 suffixes = [""]216 a = A(widgets=[None])217 self.assertEqual(a.decompress(None), [None, None])218class RangeWidgetTests(TestCase):219 def test_widget(self):220 w = RangeWidget()221 self.assertEqual(len(w.widgets), 2)222 self.assertHTMLEqual(223 w.render("price", ""),224 """225 <input type="text" name="price_min" />226 -227 <input type="text" name="price_max" />""",228 )229 self.assertHTMLEqual(230 w.render("price", slice(5.99, 9.99)),231 """232 <input type="text" name="price_min" value="5.99" />233 -234 <input type="text" name="price_max" value="9.99" />""",235 )236 def test_widget_attributes(self):237 w = RangeWidget(attrs={"type": "date"})238 self.assertEqual(len(w.widgets), 2)239 self.assertHTMLEqual(240 w.render("date", ""),241 """242 <input type="date" name="date_min" />243 -244 <input type="date" name="date_max" />""",245 )246class BooleanWidgetTests(TestCase):247 def test_widget_render(self):248 w = BooleanWidget()249 self.assertHTMLEqual(250 w.render("price", ""),251 """252 <select name="price">253 <option selected="selected" value="">Unknown</option>254 <option value="true">Yes</option>255 <option value="false">No</option>256 </select>""",257 )258 def test_widget_value_from_datadict(self):259 """ """260 w = BooleanWidget()261 trueActive = {"active": "true"}262 result = w.value_from_datadict(trueActive, {}, "active")263 self.assertEqual(result, True)264 falseActive = {"active": "false"}265 result = w.value_from_datadict(falseActive, {}, "active")266 self.assertEqual(result, False)267 result = w.value_from_datadict({}, {}, "active")268 self.assertEqual(result, None)269class BaseCSVWidgetTests(TestCase):270 def test_widget_render(self):271 class NumberCSVWidget(BaseCSVWidget, NumberInput):272 pass273 w = NumberCSVWidget(attrs={"test": "attr"})274 self.assertHTMLEqual(275 w.render("price", None),276 """277 <input type="number" test="attr" name="price" />""",278 )279 self.assertHTMLEqual(280 w.render("price", ""),281 """282 <input type="number" test="attr" name="price" />""",283 )284 self.assertHTMLEqual(285 w.render("price", []),286 """287 <input type="number" test="attr" name="price" />""",288 )289 self.assertHTMLEqual(290 w.render("price", "1"),291 """292 <input type="number" test="attr" name="price" value="1" />""",293 )294 self.assertHTMLEqual(295 w.render("price", "1,2"),296 """297 <input type="number" test="attr" name="price" value="1,2" />""",298 )299 self.assertHTMLEqual(300 w.render("price", ["1", "2"]),301 """302 <input type="text" name="price" value="1,2" />""",303 )304 self.assertHTMLEqual(305 w.render("price", [1, 2]),306 """307 <input type="text" name="price" value="1,2" />""",308 )309 def test_widget_value_from_datadict(self):310 class NumberCSVWidget(BaseCSVWidget, NumberInput):311 pass312 w = NumberCSVWidget()313 data = {"price": None}314 result = w.value_from_datadict(data, {}, "price")315 self.assertEqual(result, None)316 data = {"price": "1"}317 result = w.value_from_datadict(data, {}, "price")318 self.assertEqual(result, ["1"])319 data = {"price": "1,2"}320 result = w.value_from_datadict(data, {}, "price")321 self.assertEqual(result, ["1", "2"])322 data = {"price": "1,,2"}323 result = w.value_from_datadict(data, {}, "price")324 self.assertEqual(result, ["1", "", "2"])325 data = {"price": "1,"}326 result = w.value_from_datadict(data, {}, "price")327 self.assertEqual(result, ["1", ""])328 data = {"price": ","}329 result = w.value_from_datadict(data, {}, "price")330 self.assertEqual(result, ["", ""])331 data = {"price": ""}332 result = w.value_from_datadict(data, {}, "price")333 self.assertEqual(result, [])334 result = w.value_from_datadict({}, {}, "price")335 self.assertEqual(result, None)336 def test_surrogate_class(self):337 class ClassSurrogate(BaseCSVWidget, NumberInput):338 surrogate = NumberInput339 w = ClassSurrogate()340 self.assertIsInstance(w.surrogate, NumberInput)341 def test_surrogate_instance(self):342 class InstanceSurrogate(BaseCSVWidget, NumberInput):343 surrogate = NumberInput()344 w = InstanceSurrogate()345 self.assertIsInstance(w.surrogate, NumberInput)346 self.assertIsNot(InstanceSurrogate.surrogate, w.surrogate) # deepcopied347class CSVWidgetTests(TestCase):348 def test_widget_render(self):349 w = CSVWidget(attrs={"test": "attr"})350 self.assertHTMLEqual(351 w.render("price", None),352 """353 <input type="text" test="attr" name="price" />""",354 )355 self.assertHTMLEqual(356 w.render("price", ""),357 """358 <input type="text" test="attr" name="price" />""",359 )360 self.assertHTMLEqual(361 w.render("price", []),362 """363 <input type="text" test="attr" name="price" />""",364 )365 self.assertHTMLEqual(366 w.render("price", "1"),367 """368 <input type="text" test="attr" name="price" value="1" />""",369 )370 self.assertHTMLEqual(371 w.render("price", "1,2"),372 """373 <input type="text" test="attr" name="price" value="1,2" />""",374 )375 self.assertHTMLEqual(376 w.render("price", ["1", "2"]),377 """378 <input type="text" test="attr" name="price" value="1,2" />""",379 )380 self.assertHTMLEqual(381 w.render("price", [1, 2]),382 """383 <input type="text" test="attr" name="price" value="1,2" />""",384 )385class CSVSelectTests(TestCase):386 class CSVSelect(BaseCSVWidget, Select):387 pass388 def test_widget(self):389 w = self.CSVSelect(choices=((1, "a"), (2, "b")))390 self.assertHTMLEqual(391 w.render("price", None),392 """393 <select name="price">394 <option value="1">a</option>395 <option value="2">b</option>396 </select>397 """,398 )399 self.assertHTMLEqual(400 w.render("price", ""),401 """402 <select name="price">403 <option value="1">a</option>404 <option value="2">b</option>405 </select>406 """,407 )408 self.assertHTMLEqual(409 w.render("price", "1"),410 """411 <select name="price">412 <option selected="selected" value="1">a</option>413 <option value="2">b</option>414 </select>415 """,416 )417 self.assertHTMLEqual(418 w.render("price", "1,2"),419 """420 <select name="price">421 <option value="1">a</option>422 <option value="2">b</option>423 </select>424 """,425 )426 self.assertHTMLEqual(427 w.render("price", ["1", "2"]),428 """429 <input type="text" name="price" value="1,2" />""",430 )431 self.assertHTMLEqual(432 w.render("price", [1, 2]),433 """434 <input type="text" name="price" value="1,2" />""",435 )436class QueryArrayWidgetTests(TestCase):437 def test_widget_value_from_datadict(self):438 w = QueryArrayWidget()439 # Values can be provided as csv string: ?foo=bar,baz440 data = {"price": None}441 result = w.value_from_datadict(data, {}, "price")442 self.assertEqual(result, [])443 data = {"price": "1"}444 result = w.value_from_datadict(data, {}, "price")445 self.assertEqual(result, ["1"])446 data = {"price": "1,2"}447 result = w.value_from_datadict(data, {}, "price")448 self.assertEqual(sorted(result), ["1", "2"])449 data = {"price": "1,,2"}450 result = w.value_from_datadict(data, {}, "price")451 self.assertEqual(sorted(result), ["1", "2"])452 data = {"price": "1,"}453 result = w.value_from_datadict(data, {}, "price")454 self.assertEqual(result, ["1"])455 data = {"price": ","}456 result = w.value_from_datadict(data, {}, "price")457 self.assertEqual(result, [])458 data = {"price": ""}459 result = w.value_from_datadict(data, {}, "price")460 self.assertEqual(result, [])461 result = w.value_from_datadict({}, {}, "price")462 self.assertEqual(result, [])463 # Values can be provided as query array: ?foo[]=bar&foo[]=baz464 data = {"price[]": None}465 result = w.value_from_datadict(data, {}, "price")466 self.assertEqual(result, [])467 data = {"price[]": ["1"]}468 result = w.value_from_datadict(data, {}, "price")469 self.assertEqual(result, ["1"])470 data = {"price[]": ["1", "2"]}471 result = w.value_from_datadict(data, {}, "price")472 self.assertEqual(sorted(result), ["1", "2"])473 data = {"price[]": ["1", "", "2"]}474 result = w.value_from_datadict(data, {}, "price")475 self.assertEqual(sorted(result), ["1", "2"])476 data = {"price[]": ["1", ""]}477 result = w.value_from_datadict(data, {}, "price")478 self.assertEqual(result, ["1"])479 data = {"price[]": ["", ""]}480 result = w.value_from_datadict(data, {}, "price")481 self.assertEqual(result, [])482 data = {"price[]": []}483 result = w.value_from_datadict(data, {}, "price")484 self.assertEqual(result, [])485 result = w.value_from_datadict({}, {}, "price")...
widgets.pyi
Source:widgets.pyi
...6class LinkWidget(forms.Widget):7 choices: Any = ...8 def __init__(self, attrs: Optional[Any] = ..., choices: Any = ...) -> None: ...9 data: Any = ...10 def value_from_datadict(self, data: Any, files: Any, name: Any): ...11 def render(12 self,13 name: str,14 value: Any,15 attrs: Optional[_OptAttrs] = ...,16 choices: Tuple = ...,17 renderer: Optional[EngineMixin] = ...,18 ) -> SafeText: ...19 def render_options(self, choices: Any, selected_choices: Any, name: Any): ...20 def render_option(21 self, name: Any, selected_choices: Any, option_value: Any, option_label: Any22 ): ...23 def option_string(self): ...24class SuffixedMultiWidget(forms.MultiWidget):25 suffixes: Any = ...26 def __init__(self, *args: Any, **kwargs: Any) -> None: ...27 def suffixed(self, name: Any, suffix: Any): ...28 def get_context(self, name: Any, value: Any, attrs: Any): ...29 def value_from_datadict(self, data: Any, files: Any, name: Any): ...30 def value_omitted_from_data(self, data: Any, files: Any, name: Any): ...31 def replace_name(self, output: Any, index: Any): ...32 def decompress(self, value: Any): ...33class RangeWidget(SuffixedMultiWidget):34 template_name: str = ...35 suffixes: Any = ...36 def __init__(self, attrs: Optional[Any] = ...) -> None: ...37 def decompress(self, value: Any): ...38class DateRangeWidget(RangeWidget):39 suffixes: Any = ...40class LookupChoiceWidget(SuffixedMultiWidget):41 suffixes: Any = ...42 def decompress(self, value: Any): ...43class BooleanWidget(forms.Select):44 def __init__(self, attrs: Optional[Any] = ...) -> None: ...45 def render(46 self,47 name: Any,48 value: Any,49 attrs: Optional[Any] = ...,50 renderer: Optional[Any] = ...,51 ): ...52 def value_from_datadict(self, data: Any, files: Any, name: Any): ...53class BaseCSVWidget(forms.Widget):54 def value_from_datadict(self, data: Any, files: Any, name: Any): ...55 def render(56 self,57 name: Any,58 value: Any,59 attrs: Optional[Any] = ...,60 renderer: Optional[Any] = ...,61 ): ...62class CSVWidget(BaseCSVWidget, forms.TextInput): ...63class QueryArrayWidget(BaseCSVWidget, forms.TextInput):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!