How to use test_get_context method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_blocks.py

Source:test_blocks.py Github

copy

Full Screen

...7 super(AttrDict, self).__init__(*args, **kwargs)8 self.__dict__ = self9class LinkBlockTestCase(TestCase):10 fixtures = ["countries_and_regions"]11 def test_get_context(self):12 block = LinkBlock()13 request = RequestFactory()14 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})15 context = block.get_context(16 {"url": "url", "text": "text", "cls": "class"},17 parent_context={"request": request},18 )19 self.assertEqual("url?country=104", context.get("href"))20 self.assertEqual("text", context.get("text"))21 self.assertEqual("class", context.get("class"))22class AnchorBlockTestCase(TestCase):23 def test_get_context(self):24 block = AnchorBlock()25 context = block.get_context({"slug": "slug"})26 self.assertEqual("slug", context.get("slug"))27class FAQsBlockTestCase(TestCase):28 def test_get_context(self):29 block = FAQsBlock()30 context = block.get_context(31 {32 "title": "title",33 "faqs": [34 {"slug": "slug1", "question": "question1", "answer": "answer1"},35 {"slug": "slug2", "question": "question2", "answer": "answer2"},36 ],37 }38 )39 self.assertEqual("title", context.get("title"))40 self.assertEqual(2, len(context.get("list")))41 self.assertEqual("slug1", context.get("list")[0]["slug"])42 self.assertEqual("question1", context.get("list")[0]["term"])43 self.assertEqual("answer1", context.get("list")[0]["definition"])44class TwitterBlockTestCase(TestCase):45 def test_get_context(self):46 block = TwitterBlock()47 context = block.get_context({"count": 3, "username": "Land_Matrix"})48 self.assertEqual("", context.get("timeline"))49 self.assertEqual("Land_Matrix", context.get("username"))50class NoWrapsStreamBlockTestCase(TestCase):51 def setUp(self):52 self.block = NoWrapsStreamBlock()53 class ChildBlock:54 block_type = ""55 def __init__(self, value, block_type=""):56 self.value = value57 self.block_type = block_type58 def render(self, context=None):59 return self.value60 self.children = [61 ChildBlock("1", "full_width_container"),62 ChildBlock("2", "test"),63 ]64 def test_render_basic(self):65 output = self.block.render_basic(self.children)66 self.assertEqual(67 '<div class="">1</div>\n<div class="block-test block">2</div>', output68 )69# TODO dont run this test. probably dont need it anymore anyways70# class NoWrapsStreamFieldTestCase(TestCase):71# def test_init(self):72# block = NoWrapsStreamField(Block())73# self.assertIsInstance(block.stream_block, Block)74# block = NoWrapsStreamField(Block)75# self.assertIsInstance(block.stream_block, Block)76# block = NoWrapsStreamField([])77# self.assertIsInstance(block.stream_block, NoWrapsStreamBlock)78class ImageBlockTestCase(TestCase):79 def test_get_context(self):80 block = ImageBlock()81 image = Image.objects.create(id=1, title="Test", file=get_test_image_file())82 context = block.get_context(image)83 self.assertGreater(len(context.get("url")), 0)84 self.assertEqual("Test", context.get("name"))85class LinkedImageBlockTestCase(TestCase):86 fixtures = ["countries_and_regions"]87 def test_get_context(self):88 block = LinkedImageBlock()89 request = RequestFactory()90 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})91 image = Image.objects.create(id=1, title="Test", file=get_test_image_file())92 context = block.get_context(93 {"url": "url", "image": image}, {"request": request}94 )95 self.assertEqual("url?country=104", context.get("href"))96 self.assertGreater(len(context.get("url")), 0)97class SliderBlockTestCase(TestCase):98 def test_get_context(self):99 block = SliderBlock()100 request = RequestFactory()101 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})102 image = Image.objects.create(id=1, title="Test", file=get_test_image_file())103 context = block.get_context(104 {105 "title": "title",106 "images": [{"image": image, "url": "url", "caption": "caption"}],107 },108 {"request": request},109 )110 self.assertEqual("title", context.get("title"))111 self.assertEqual(1, len(context.get("images")))112 self.assertGreater(len(context.get("images")[0]["url"]), 0)113 self.assertEqual("Test", context.get("images")[0]["name"])114 self.assertEqual("url", context.get("images")[0]["href"])115 self.assertEqual("caption", context.get("images")[0]["caption"])116class GalleryBlockTestCase(TestCase):117 def test_get_context(self):118 block = GalleryBlock()119 request = RequestFactory()120 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})121 image = Image.objects.create(id=1, title="Test", file=get_test_image_file())122 context = block.get_context(123 {124 "title": "title",125 "columns": "2",126 "images": [{"image": image, "url": "url"}],127 },128 {"request": request},129 )130 self.assertEqual("title", context.get("title"))131 self.assertEqual(2, context.get("columns"))132 self.assertEqual(1, len(context.get("images")))133 self.assertGreater(len(context.get("images")[0]["url"]), 0)134 self.assertEqual("Test", context.get("images")[0]["name"])135 self.assertEqual("url", context.get("images")[0]["href"])136class TitleWithIconBlockTestCase(TestCase):137 fixtures = ["countries_and_regions"]138 def test_get_context(self):139 block = TitleWithIconBlock()140 request = RequestFactory()141 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})142 context = block.get_context(143 {"value": "value", "fa_icon": "fa_icon", "url": "url"}, {"request": request}144 )145 self.assertEqual("value", context.get("value"))146 self.assertEqual("fa_icon", context.get("fa_icon"))147 self.assertEqual("url?country=104", context.get("url"))148class LatestNewsBlockTestCase(TestCase):149 fixtures = ["countries_and_regions"]150 def setUp(self):151 self.block = LatestNewsBlock()152 self.request = RequestFactory()153 self.page = BlogPage.objects.create(154 title="Blog Page", path="/", depth=0, live=True155 )156 def test_get_context_with_country(self):157 self.page.tags.add("myanmar")158 self.page.save()159 self.request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})160 context = self.block.get_context({"limit": "3"}, {"request": self.request})161 self.assertEqual(104, context.get("country").id)162 self.assertEqual("Myanmar", context.get("name"))163 self.assertGreater(len(context.get("news")), 0)164 def test_get_context_with_region(self):165 self.page.tags.add("asia")166 self.page.save()167 self.request.resolver_match = AttrDict(kwargs={"region_slug": "asia"})168 context = self.block.get_context({"limit": "3"}, {"request": self.request})169 self.assertEqual(142, context.get("region").id)170 self.assertEqual("Asia", context.get("name"))171 self.assertGreater(len(context.get("news")), 0)172class StatisticsBlockTestCase(TestCase):173 fixtures = ["countries_and_regions"]174 def test_get_context(self):175 block = StatisticsBlock()176 request = RequestFactory()177 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})178 context = block.get_context({}, {"request": request})179 self.assertEqual(104, context.get("country").id)180class MapDataChartsBlockTestCase(TestCase):181 fixtures = ["countries_and_regions"]182 def test_get_context(self):183 block = MapDataChartsBlock()184 request = RequestFactory()185 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})186 context = block.get_context({}, {"request": request})187 self.assertEqual(104, context.get("country").id)188class LinkMapBlockTestCase(TestCase):189 fixtures = ["countries_and_regions"]190 def test_get_context(self):191 block = LinkMapBlock()192 request = RequestFactory()193 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})194 context = block.get_context({}, {"request": request})195 self.assertEqual(196 {"implementation", "intention", "level_of_accuracy"},197 set(context.get("legend").keys()),198 )199 self.assertGreater(len(context.get("legend_json")), 0)200 self.assertEqual(104, context.get("map_object").id)201 self.assertEqual(True, context.get("is_country"))202class LatestDatabaseModificationsBlockTestCase(TestCase):203 fixtures = ["countries_and_regions"]204 def test_get_context(self):205 block = LatestDatabaseModificationsBlock()206 request = RequestFactory()207 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})208 context = block.get_context({"limit": "limit"}, {"request": request})209 self.assertEqual(104, context.get("country").id)210 self.assertEqual("limit", context.get("limit"))211class RegionBlockTestCase(TestCase):212 fixtures = ["countries_and_regions"]213 def test_get_context(self):214 block = RegionBlock()215 request = RequestFactory()216 request.resolver_match = AttrDict(kwargs={"country_slug": "myanmar"})217 context = block.get_context({}, {"request": request})218 self.assertEqual(104, context.get("country").id)219 self.assertEqual(142, context.get("region").id)220class CountriesBlockTestCase(TestCase):221 fixtures = ["countries_and_regions"]222 def test_get_context(self):223 block = CountriesBlock()224 request = RequestFactory()225 request.resolver_match = AttrDict(kwargs={"region_slug": "asia"})226 context = block.get_context({}, {"request": request})227 self.assertEqual(142, context.get("region").id)228 self.assertGreater(len(context.get("countries")), 0)229class Columns1To1BlockTestCase(TestCase):230 def test_get_context(self):231 block = Columns1To1Block()232 context = block.get_context(233 {"left_column": "left_column", "right_column": "right_column"}234 )235 self.assertEqual("left_column", context.get("left_column"))236 self.assertEqual("right_column", context.get("right_column"))237class ThreeColumnsBlockTestCase(TestCase):238 def test_get_context(self):239 block = ThreeColumnsBlock()240 context = block.get_context(241 {242 "left_column": "left_column",243 "middle_column": "middle_column",244 "right_column": "right_column",245 }246 )247 self.assertEqual("left_column", context.get("left_column"))248 self.assertEqual("middle_column", context.get("middle_column"))249 self.assertEqual("right_column", context.get("right_column"))250class TabsBlockTestCase(TestCase):251 def test_get_context(self):252 block = TabsBlock()253 context = block.get_context(254 {"tabs": [{"title": "title", "fa_icon": "fa_icon", "content": "content"}]}255 )256 self.assertEqual(1, len(context.get("list")))257 self.assertEqual("title", context.get("list")[0]["title"])258 self.assertEqual("fa_icon", context.get("list")[0]["fa_icon"])259 self.assertEqual("content", context.get("list")[0]["content"])260class FullWidthContainerBlockTestCase(TestCase):261 def test_get_context(self):262 block = FullWidthContainerBlock()263 context = block.get_context({"content": "content", "color": "color"})264 self.assertEqual("content", context.get("content"))...

Full Screen

Full Screen

context.py

Source:context.py Github

copy

Full Screen

1#! /usr/bin/env python2# coding: utf-83import os, sys4import numpy as np5import mxnet as mx6__all__ = [7 'get_context', 'try_gpu',8 'TEST'9]10def get_context(gpus=[]):11 gpu_count = mx.util.get_gpu_count()12 aval_gpu_count = []13 if len(gpus) > 0:14 aval_gpu_count = [i for i in gpus if i in range(gpu_count)]15 if len(aval_gpu_count) > 0:16 gpu_memorys = [mx.util.get_gpu_memory(gpu_dev_id=gpu_id) for gpu_id in aval_gpu_count]17 contexts = [mx.gpu(device_id) for device_id in aval_gpu_count]18 else:19 gpu_memorys = [-1]20 contexts = [mx.cpu()]21 return contexts, gpu_memorys22def TEST_get_context():23 gpus = []24 ctxs, mems = get_context(gpus)25 print(f'[TEST_get_context] ctxs: {ctxs} mems: {mems}')26 gpus = [0, 1]27 ctxs, mems = get_context(gpus)28 print(f'[TEST_get_context] ctxs: {ctxs} mems: {mems}')29def try_gpu(gpu_id=0):30 try:31 ctx = mx.gpu(gpu_id)32 _ = mx.nd.zeros((1,), ctx=ctx)33 except mx.base.MXNetError:34 ctx = mx.cpu()35 return ctx36def TEST_try_gpu():37 ctx = try_gpu(0)38 print(f'[] ctx: {ctx}')39 ctx = try_gpu(3)40 print(f'[] ctx: {ctx}')41def TEST():42 TEST_get_context()43 TEST_try_gpu()44if __name__ == '__main__':...

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 Django Test Plus 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