How to use _make_items method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

tests.py

Source:tests.py Github

copy

Full Screen

...250 def test_single_item_json(self):251 schema1 = Schema.objects.get(slug='type1')252 zone = 'US/Pacific'253 with self.settings(TIME_ZONE=zone):254 item = _make_items(1, schema1)[0]255 item.save()256 id_ = item.id257 response = self.client.get(reverse('single_item_json', kwargs={'id_': id_}))258 self.assertEqual(response.status_code, 200)259 out = simplejson.loads(response.content)260 self.assertEqual(out['type'], 'Feature')261 self.assert_('geometry' in out.keys())262 self.assertEqual(out['geometry']['type'], 'Point')263 self.assert_('properties' in out.keys())264 self.assertEqual(out['properties']['title'], item.title)265 self.assertEqual(out['properties']['description'], item.description)266 self.assertEqual(out['properties']['type'], schema1.slug)267 def test_items_nofilter(self):268 # create a few items269 schema1 = Schema.objects.get(slug='type1')270 schema2 = Schema.objects.get(slug='type2')271 items = []272 zone = 'US/Eastern'273 with self.settings(TIME_ZONE=zone):274 items += _make_items(5, schema1)275 items += _make_items(5, schema2)276 for item in items:277 item.save()278 response = self.client.get(reverse('items_json'))279 self.assertEqual(response.status_code, 200)280 ritems = simplejson.loads(response.content)281 assert len(ritems['features']) == len(items)282 def test_items_atom_nofilter(self):283 zone = 'America/Chicago'284 with self.settings(TIME_ZONE=zone):285 schema1 = Schema.objects.get(slug='type1')286 schema2 = Schema.objects.get(slug='type2')287 items = _make_items(5, schema1) + _make_items(5, schema2)288 for item in items:289 item.save()290 response = self.client.get(reverse('items_atom'))291 self.assertEqual(response.status_code, 200)292 self.assertEqual(response['content-type'], 'application/atom+xml')293 feed = feedparser.parse(response.content)294 self.assertEqual(feed['feed']['title'], u'openblock news item atom feed')295 self.assertEqual(len(feed['entries']), len(items))296 assert self._items_exist_in_xml_result(items, response.content)297 def test_items_filter_schema(self):298 zone = 'Asia/Dubai'299 with self.settings(TIME_ZONE=zone):300 # create a few items of each of two schema types301 schema1 = Schema.objects.get(slug='type1')302 schema2 = Schema.objects.get(slug='type2')303 items1 = _make_items(5, schema1)304 items2 = _make_items(5, schema2)305 for item in items1 + items2:306 item.save()307 # query for only the second schema308 response = self.client.get(reverse('items_json') + "?type=type2")309 self.assertEqual(response.status_code, 200)310 ritems = simplejson.loads(response.content)311 self.assertEqual(len(ritems['features']), len(items2))312 for item in ritems['features']:313 assert item['properties']['type'] == 'type2'314 assert self._items_exist_in_result(items2, ritems)315 # query for both schemas.316 response = self.client.get(reverse('items_json') + "?type=type2&type=type1")317 self.assertEqual(response.status_code, 200)318 ritems = simplejson.loads(response.content)319 self.assertEqual(len(ritems['features']), len(items2 + items1))320 def test_items_atom_filter_schema(self):321 zone = 'Australia/North'322 with self.settings(TIME_ZONE=zone):323 # create a few items of each of two schema types324 schema1 = Schema.objects.get(slug='type1')325 schema2 = Schema.objects.get(slug='type2')326 items1 = _make_items(5, schema1)327 items2 = _make_items(5, schema2)328 for item in items1 + items2:329 item.save()330 # query for only the second schema331 response = self.client.get(reverse('items_atom') + "?type=type2")332 self.assertEqual(response.status_code, 200)333 feed = feedparser.parse(response.content)334 assert len(feed['entries']) == len(items2)335 for item in feed['entries']:336 # Yay feedparser, we don't know how it will spell the337 # openblock:type element, varies depending on... something.338 assert item.get('openblock_type', item.get('type')) == u'type2'339 def test_extension_fields_json(self):340 zone = 'Europe/Malta'341 with self.settings(TIME_ZONE=zone):342 schema = Schema.objects.get(slug='test-schema')343 ext_vals = {344 'varchar': ('This is a varchar', 'This is a varchar'), 345 'date': (datetime.date(2001, 01, 02), '2001-01-02'),346 'time': (datetime.time(hour=10, minute=11, second=12), 347 '10:11:12-08:00'),348 'datetime': (datetime.datetime(2001, 01, 02, hour=10, minute=11, second=12),349 '2001-01-02T10:11:12-08:00'),350 'bool': (True, True),351 'int': (7, 7),352 'lookup': ('7701,7700', ['Lookup 7701 Name', 'Lookup 7700 Name']),353 }354 items = _make_items(5, schema)355 for item in items:356 item.save()357 for k,v in ext_vals.items():358 item.attributes[k] = v[0]359 response = self.client.get(reverse('items_json'))360 self.assertEqual(response.status_code, 200)361 ritems = simplejson.loads(response.content)362 self.assertEqual(len(ritems['features']), len(items))363 for item in ritems['features']:364 for k, v in ext_vals.items():365 self.assertEqual(item['properties'][k], v[1])366 assert self._items_exist_in_result(items, ritems)367 def test_extension_fields_atom(self):368 zone = 'Pacific/Fiji'369 with self.settings(TIME_ZONE=zone):370 schema = Schema.objects.get(slug='test-schema')371 ext_vals = {372 'varchar': ('This is a varchar', 'This is a varchar'), 373 'date': (datetime.date(2001, 01, 02), '2001-01-02'),374 'time': (datetime.time(hour=10, minute=11, second=12), 375 '10:11:12-08:00'),376 'datetime': (datetime.datetime(2001, 01, 02, hour=10, minute=11, second=12),377 '2001-01-02T10:11:12-08:00'),378 'bool': (True, 'True'),379 'int': (7, '7'),380 'lookup': ('7700,7701', 'Lookup 7700 Name'), # only check 1381 }382 items = _make_items(5, schema)383 for item in items:384 item.save()385 for k,v in ext_vals.items():386 item.attributes[k] = v[0]387 response = self.client.get(reverse('items_atom'))388 self.assertEqual(response.status_code, 200)389 # Darn feedparser throws away nested extension elements. Gahhh.390 # Okay, let's parse the old-fashioned way.391 from lxml import etree392 root = etree.fromstring(response.content)393 ns = {'atom': 'http://www.w3.org/2005/Atom',394 'openblock': 'http://openblock.org/ns/0'}395 entries = root.xpath('//atom:entry', namespaces=ns)396 assert len(entries) == len(items)397 for entry in entries:398 for key, value in sorted(ext_vals.items()):399 attrs = entry.xpath(400 'openblock:attributes/openblock:attribute[@name="%s"]' % key,401 namespaces=ns)402 if key == 'lookup':403 self.assertEqual(len(attrs), 2)404 else:405 self.assertEqual(len(attrs), 1)406 self.assertEqual(attrs[0].text, value[1])407 assert self._items_exist_in_xml_result(items, response.content)408 def test_items_filter_daterange_rfc3339(self):409 import pyrfc3339410 import pytz411 zone='US/Pacific'412 local_tz = pytz.timezone(zone)413 with self.settings(TIME_ZONE=zone):414 # create some items, they will have415 # dates spaced apart by one day, newest first416 schema1 = Schema.objects.get(slug='type1')417 items = _make_items(4, schema1)418 for item in items:419 item.save()420 # filter out the first and last item by constraining421 # the date range to the inner two items.422 # (Use local timezone for consistency with _make_items())423 startdate = pyrfc3339.generate(items[2].pub_date.replace(tzinfo=local_tz))424 enddate = pyrfc3339.generate(items[1].pub_date.replace(tzinfo=local_tz))425 # filter both ends426 qs = "?startdate=%s&enddate=%s" % (startdate, enddate)427 response = self.client.get(reverse('items_json') + qs)428 self.assertEqual(response.status_code, 200)429 ritems = simplejson.loads(response.content)430 self.assertEqual(len(ritems['features']), 2)431 assert self._items_exist_in_result(items[1:3], ritems)432 # startdate only433 qs = "?startdate=%s" % startdate434 response = self.client.get(reverse('items_json') + qs)435 self.assertEqual(response.status_code, 200)436 ritems = simplejson.loads(response.content)437 assert len(ritems['features']) == 3438 assert self._items_exist_in_result(items[:-1], ritems)439 # enddate only440 qs = "?enddate=%s" % enddate441 response = self.client.get(reverse('items_json') + qs)442 self.assertEqual(response.status_code, 200)443 ritems = simplejson.loads(response.content)444 assert len(ritems['features']) == 3445 assert self._items_exist_in_result(items[1:], ritems)446 def test_items_filter_daterange(self):447 zone = 'UTC'448 with self.settings(TIME_ZONE=zone):449 # create some items, they will have450 # dates spaced apart by one day, newest first451 schema1 = Schema.objects.get(slug='type1')452 items = _make_items(4, schema1)453 for item in items:454 cd = item.item_date455 item.pub_date = datetime.datetime(year=cd.year, month=cd.month, day=cd.day)456 item.save()457 # filter out the first and last item by constraining458 # the date range to the inner two items459 startdate = items[2].pub_date.strftime('%Y-%m-%d')460 enddate = items[1].pub_date.strftime('%Y-%m-%d')461 # filter both ends462 qs = "?startdate=%s&enddate=%s" % (startdate, enddate)463 response = self.client.get(reverse('items_json') + qs)464 self.assertEqual(response.status_code, 200)465 ritems = simplejson.loads(response.content)466 assert len(ritems['features']) == 2467 assert self._items_exist_in_result(items[1:3], ritems)468 # startdate only469 qs = "?startdate=%s" % startdate470 response = self.client.get(reverse('items_json') + qs)471 self.assertEqual(response.status_code, 200)472 ritems = simplejson.loads(response.content)473 assert len(ritems['features']) == 3474 assert self._items_exist_in_result(items[:-1], ritems)475 # enddate only476 qs = "?enddate=%s" % enddate477 response = self.client.get(reverse('items_json') + qs)478 self.assertEqual(response.status_code, 200)479 ritems = simplejson.loads(response.content)480 assert len(ritems['features']) == 3481 assert self._items_exist_in_result(items[1:], ritems)482 def test_items_limit_offset(self):483 zone = 'Europe/Vienna'484 with self.settings(TIME_ZONE=zone):485 # create a bunch of items486 schema1 = Schema.objects.get(slug='type1')487 items = _make_items(10, schema1)488 for item in items:489 item.save()490 # with no query, we should get all the items491 response = self.client.get(reverse('items_json'))492 self.assertEqual(response.status_code, 200)493 ritems = simplejson.loads(response.content)494 assert len(ritems['features']) == len(items)495 assert self._items_exist_in_result(items, ritems)496 # limited to 5, we should get the first 5497 qs = "?limit=5"498 response = self.client.get(reverse('items_json') + qs)499 self.assertEqual(response.status_code, 200)500 ritems = simplejson.loads(response.content)501 assert len(ritems['features']) == 5502 assert self._items_exist_in_result(items[:5], ritems)503 # offset by 2, we should get the last 8504 qs = "?offset=2"505 response = self.client.get(reverse('items_json') + qs)506 self.assertEqual(response.status_code, 200)507 ritems = simplejson.loads(response.content)508 assert len(ritems['features']) == 8509 assert self._items_exist_in_result(items[2:], ritems)510 # offset by 2, limit to 5511 qs = "?offset=2&limit=5"512 response = self.client.get(reverse('items_json') + qs)513 self.assertEqual(response.status_code, 200)514 ritems = simplejson.loads(response.content)515 assert len(ritems['features']) == 5516 assert self._items_exist_in_result(items[2:7], ritems)517 def test_items_predefined_location(self):518 zone = 'Europe/Zurich'519 with self.settings(TIME_ZONE=zone):520 # create a bunch of items that are nowhere in particular521 schema1 = Schema.objects.get(slug='type1')522 items_nowhere = _make_items(5, schema1, 'nowhere ')523 for item in items_nowhere:524 item.save()525 # make some items that are centered on a location526 loc = Location.objects.get(slug='hood-1')527 pt = loc.location.centroid528 items_hood1 = _make_items(5, schema1, 'hood1 ')529 for item in items_hood1:530 item.location = pt531 item.save()532 qs = "?locationid=%s" % cgi.escape("neighborhoods/hood-1")533 response = self.client.get(reverse('items_json') + qs)534 self.assertEqual(response.status_code, 200)535 ritems = simplejson.loads(response.content)536 assert len(ritems['features']) == 5537 assert self._items_exist_in_result(items_hood1, ritems)538 # TODO what we really want is to assert *none* of these are found539 self.failIf(self._items_exist_in_result(items_nowhere, ritems))540 # make some items that are centered on another location541 loc2 = Location.objects.get(slug='hood-2')542 pt2 = loc2.location.centroid543 items_hood2 = _make_items(3, schema1, 'hood2 ')544 for item in items_hood2:545 item.location = pt2546 item.save()547 qs2 = qs + "&locationid=%s" % cgi.escape("neighborhoods/hood-2")548 response = self.client.get(reverse('items_json') + qs2)549 self.assertEqual(response.status_code, 200)550 ritems = simplejson.loads(response.content)551 self.assertEqual(len(ritems['features']), 8)552 self.assert_(self._items_exist_in_result(items_hood1, ritems))553 self.assert_(self._items_exist_in_result(items_hood2, ritems))554 # TODO what we really want is to assert *none* of these are found555 self.failIf(self._items_exist_in_result(items_nowhere, ritems))556 def test_items_radius(self):557 zone = 'Asia/Saigon'558 with self.settings(TIME_ZONE=zone):559 # create a bunch of items nowhere in particular560 schema1 = Schema.objects.get(slug='type1')561 items_nowhere = _make_items(5, schema1, 'nowhere ')562 for item in items_nowhere:563 item.save()564 # make some items that are centered on a location565 loc = Location.objects.get(slug='hood-1')566 pt = loc.location.centroid567 items_hood1 = _make_items(5, schema1, 'hood1 ')568 for item in items_hood1:569 item.location = pt570 item.save()571 qs = "?center=%f,%f&radius=10" % (pt.x, pt.y)572 response = self.client.get(reverse('items_json') + qs)573 self.assertEqual(response.status_code, 200)574 ritems = simplejson.loads(response.content)575 assert len(ritems['features']) == 5576 self.assert_(self._items_exist_in_result(items_hood1, ritems))577 self.failIf(self._items_exist_in_result(items_nowhere, ritems))578 def _items_exist_in_result(self, items, ritems):579 all_ids = set([i['properties']['id'] for i in ritems['features']])580 for item in items:581 if not item.id in all_ids: 582 return False583 return True584 def _items_exist_in_xml_result(self, items, xmlstring):585 from lxml import etree586 root = etree.fromstring(xmlstring)587 ns = {'atom': 'http://www.w3.org/2005/Atom',588 'openblock': 'http://openblock.org/ns/0'}589 title_nodes = root.xpath('//atom:entry/atom:title', namespaces=ns)590 all_titles = set([t.text for t in title_nodes])591 for item in items:592 if not item.title in all_titles:593 return False594 return True595def _make_items(number, schema, title_prefix=''):596 items = []597 from django.conf import settings598 local_tz = pytz.timezone(settings.TIME_ZONE)599 curdate = datetime.datetime.now().replace(microsecond=0, tzinfo=local_tz)600 inc = datetime.timedelta(days=-1)601 for i in range(number):602 desc = '%s item %d' % (schema.slug, i)603 items.append(NewsItem(schema=schema,604 title=title_prefix+desc,605 description=desc,606 item_date=curdate.date(),607 pub_date=curdate,608 location=geos.Point(0,0)))609 curdate += inc...

Full Screen

Full Screen

lr1_test.py

Source:lr1_test.py Github

copy

Full Screen

...15import collections16import unittest17from compiler.front_end import lr118from compiler.util import parser_types19def _make_items(text):20 """Makes a list of lr1.Items from the lines in text."""21 return frozenset([lr1.Item.parse(line.strip()) for line in text.splitlines()])22Token = collections.namedtuple("Token", ["symbol", "source_location"])23def _tokenize(text):24 """"Tokenizes" text by making each character into a token."""25 result = []26 for i in range(len(text)):27 result.append(Token(text[i], parser_types.make_location(28 (1, i + 1), (1, i + 2))))29 return result30def _parse_productions(text):31 """Parses text into a grammar by calling Production.parse on each line."""32 return [parser_types.Production.parse(line) for line in text.splitlines()]33# Example grammar 4.54 from Aho, Sethi, Lam, Ullman (ASLU) p263.34_alsu_grammar = lr1.Grammar("S", _parse_productions("""S -> C C35 C -> c C36 C -> d"""))37# Item sets corresponding to the above grammar, ASLU pp263-264.38_alsu_items = [39 _make_items("""S' -> . S, $40 S -> . C C, $41 C -> . c C, c42 C -> . c C, d43 C -> . d, c44 C -> . d, d"""),45 _make_items("""S' -> S ., $"""),46 _make_items("""S -> C . C, $47 C -> . c C, $48 C -> . d, $"""),49 _make_items("""C -> c . C, c50 C -> c . C, d51 C -> . c C, c52 C -> . c C, d53 C -> . d, c54 C -> . d, d"""),55 _make_items("""C -> d ., c56 C -> d ., d"""),57 _make_items("""S -> C C ., $"""),58 _make_items("""C -> c . C, $59 C -> . c C, $60 C -> . d, $"""),61 _make_items("""C -> d ., $"""),62 _make_items("""C -> c C ., c63 C -> c C ., d"""),64 _make_items("""C -> c C ., $"""),65]66# ACTION table corresponding to the above grammar, ASLU p266.67_alsu_action = {68 (0, "c"): lr1.Shift(3, _alsu_items[3]),69 (0, "d"): lr1.Shift(4, _alsu_items[4]),70 (1, lr1.END_OF_INPUT): lr1.Accept(),71 (2, "c"): lr1.Shift(6, _alsu_items[6]),72 (2, "d"): lr1.Shift(7, _alsu_items[7]),73 (3, "c"): lr1.Shift(3, _alsu_items[3]),74 (3, "d"): lr1.Shift(4, _alsu_items[4]),75 (4, "c"): lr1.Reduce(parser_types.Production("C", ("d",))),76 (4, "d"): lr1.Reduce(parser_types.Production("C", ("d",))),77 (5, lr1.END_OF_INPUT): lr1.Reduce(parser_types.Production("S", ("C", "C"))),78 (6, "c"): lr1.Shift(6, _alsu_items[6]),...

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 Lemoncheesecake 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