|
| 1 | +import twitter |
| 2 | +import json |
| 3 | +import sys |
| 4 | +import unittest |
| 5 | + |
| 6 | + |
| 7 | +class PlaceTest(unittest.TestCase): |
| 8 | + SAMPLE_JSON = '''{"id": "df51dec6f4ee2b2c", "url": "https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json", "place_type": "neighborhood", "name": "Presidio", "full_name": "Presidio, San Francisco", "country_code": "US", "country": "United States", "contained_within": [{"id": "5a110d312052166f", "url": "https://api.twitter.com/1.1/geo/id/5a110d312052166f.json", "place_type": "city", "name": "San Francisco", "full_name": "San Francisco, CA", "country_code": "US", "country": "United States", "centroid": [-122.4461400159226, 37.759828999999996], "bounding_box": {"type": "Polygon", "coordinates": [[[-122.514926, 37.708075], [-122.514926, 37.833238], [-122.357031, 37.833238], [-122.357031, 37.708075], [-122.514926, 37.708075]]]}, "attributes": {}}], "geometry": null, "polylines": [], "centroid": [-122.46598425785236, 37.79989625], "bounding_box": {"type": "Polygon", "coordinates": [[[-122.4891333, 37.786925], [-122.4891333, 37.8128675], [-122.446306, 37.8128675], [-122.446306, 37.786925], [-122.4891333, 37.786925]]]}, "attributes": {"geotagCount": "6", "162834:id": "2202"}}''' |
| 9 | + |
| 10 | + def _GetSampleContainedPlace(self): |
| 11 | + return twitter.Place(id='5a110d312052166f', |
| 12 | + url='https://api.twitter.com/1.1/geo/id/5a110d312052166f.json', |
| 13 | + place_type='city', |
| 14 | + name='San Franciso', |
| 15 | + full_name='San Francisco, CA', |
| 16 | + country_code='US', |
| 17 | + country='United States', |
| 18 | + centroid=[-122.4461400159226, |
| 19 | + 37.759828999999996], |
| 20 | + bounding_box=dict( |
| 21 | + type='Polygon', |
| 22 | + coordinates=[ |
| 23 | + [ |
| 24 | + [-122.514926, 37.708075], |
| 25 | + [-122.514926, 37.833238], |
| 26 | + [-122.357031, 37.833238], |
| 27 | + [-122.357031, 37.708075], |
| 28 | + [-122.514926, 37.708075] |
| 29 | + ] |
| 30 | + ], |
| 31 | + attributes=dict() |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + def _GetSamplePlace(self): |
| 36 | + return twitter.Place(id='df51dec6f4ee2b2c', |
| 37 | + url='https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json', |
| 38 | + place_type='neighborhood', |
| 39 | + name='Presidio', |
| 40 | + full_name='Presidio, San Francisco', |
| 41 | + country_code='US', |
| 42 | + country='United States', |
| 43 | + contained_within=[ |
| 44 | + self._GetSampleContainedPlace() |
| 45 | + ], |
| 46 | + geometry='null', |
| 47 | + polylines=[], |
| 48 | + centroid=[-122.46598425785236, 37.79989625], |
| 49 | + bounding_box=dict( |
| 50 | + type='Polygon', |
| 51 | + coordinates=[ |
| 52 | + [ |
| 53 | + [-122.4891333, 37.786925], |
| 54 | + [-122.4891333, 37.8128675], |
| 55 | + [-122.446306, 37.8128675], |
| 56 | + [-122.446306, 37.786925], |
| 57 | + [-122.4891333, 37.786925] |
| 58 | + ] |
| 59 | + ] |
| 60 | + ), |
| 61 | + attributes={ |
| 62 | + 'geotagCount': '6', |
| 63 | + '162834:id': '2202' |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + def testProperties(self): |
| 68 | + '''Test all of the twitter.Place properties''' |
| 69 | + place = twitter.Place() |
| 70 | + place.id = 'df51dec6f4ee2b2c' |
| 71 | + self.assertEqual('df51dec6f4ee2b2c', place.id) |
| 72 | + place.name = 'Presidio' |
| 73 | + self.assertEqual('Presidio', place.name) |
| 74 | + place.full_name = 'Presidio, San Francisco' |
| 75 | + self.assertEqual('Presidio, San Francisco', place.full_name) |
| 76 | + place.country_code = 'US' |
| 77 | + self.assertEqual('US', place.country_code) |
| 78 | + place.country = 'United States' |
| 79 | + self.assertEqual('United States', place.country) |
| 80 | + place.url = 'https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json' |
| 81 | + self.assertEqual('https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json', place.url) |
| 82 | + place.contained_within = [self._GetSampleContainedPlace()] |
| 83 | + self.assertEqual('5a110d312052166f', place.contained_within[0].id) |
| 84 | + |
| 85 | + @unittest.skipIf(sys.version_info.major >= 3, "skipped until fix found for v3 python") |
| 86 | + def testAsJsonString(self): |
| 87 | + '''Test the twitter.Place AsJsonString method''' |
| 88 | + self.assertEqual(PlaceTest.SAMPLE_JSON, |
| 89 | + self._GetSamplePlace().AsJsonString()) |
| 90 | + |
| 91 | + def testAsDict(self): |
| 92 | + '''Test the twitter.Place AsDict method''' |
| 93 | + place = self._GetSamplePlace() |
| 94 | + data = place.AsDict() |
| 95 | + self.assertEqual('df51dec6f4ee2b2c', data['id']) |
| 96 | + self.assertEqual('Presidio', data['name']) |
| 97 | + self.assertEqual('Presidio, San Francisco', data['full_name']) |
| 98 | + self.assertEqual('US', data['country_code']) |
| 99 | + self.assertEqual('United States', data['country']) |
| 100 | + self.assertEqual('https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json', data['url']) |
| 101 | + |
| 102 | + def testEq(self): |
| 103 | + '''Test the twitter.Place __eq__ method''' |
| 104 | + place = twitter.Place() |
| 105 | + place.id = 'df51dec6f4ee2b2c' |
| 106 | + place.name = 'Presidio' |
| 107 | + place.full_name = 'Presidio, San Francisco' |
| 108 | + place.country_code = 'US' |
| 109 | + place.country = 'United States' |
| 110 | + place.url = 'https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json' |
| 111 | + place.place_type = 'neighborhood' |
| 112 | + place.centroid = [-122.4461400159226, 37.759828999999996] |
| 113 | + place.geometry = 'null' |
| 114 | + place.polylines = [] |
| 115 | + place.bounding_box = dict(type='Polygon', |
| 116 | + coordinates=[ |
| 117 | + [ |
| 118 | + [-122.4891333, 37.786925], |
| 119 | + [-122.4891333, 37.8128675], |
| 120 | + [-122.446306, 37.8128675], |
| 121 | + [-122.446306, 37.786925], |
| 122 | + [-122.4891333, 37.786925] |
| 123 | + ] |
| 124 | + ] |
| 125 | + ) |
| 126 | + place.attributes = { |
| 127 | + 'geotagCount': '6', |
| 128 | + '162834:id': '2202' |
| 129 | + } |
| 130 | + self.assertEqual(place, self._GetSamplePlace()) |
| 131 | + |
| 132 | + def testHash(self): |
| 133 | + '''Test the twitter.Place __hash__ method''' |
| 134 | + place = self._GetSamplePlace() |
| 135 | + self.assertEqual(hash(place), hash(place.id)) |
| 136 | + |
| 137 | + def testNewFromJsonDict(self): |
| 138 | + '''Test the twitter.Status NewFromJsonDict method''' |
| 139 | + data = json.loads(PlaceTest.SAMPLE_JSON) |
| 140 | + place = twitter.Place.NewFromJsonDict(data) |
| 141 | + self.assertEqual(self._GetSamplePlace(), place) |
0 commit comments