def test_year(self): from serialize import to_json class Date(object): '''A date for a person''' def __init__(self, year, month, day): self.year = year self.month = month self.day = day to_json(Date(2011, 2, 3)) with open('output.json') as example: data = json.load(example) self.assertEqual(data.get("year"), 2011)
def test_int(self): jsonDate = to_json(self.date) jsonDate = json.loads(jsonDate) self.assertEqual(jsonDate['day'], 2) self.assertEqual(jsonDate['month'], 10) self.assertEqual(jsonDate['year'], 2010)
def test_obj(self): jsonobj = to_json(self.p1) jsonobj = json.loads(jsonobj) self.assertEqual(jsonobj['birth_date']['day'], 2) self.assertEqual(jsonobj['franchise']['name'], 'Spiderman') self.assertEqual(jsonobj['franchise']['owner'], 'Marvel') self.assertEqual(jsonobj['franchise']['started']['day'], 2)
class UnitTest(unittest.TestCase): fd1 = Date(1900, 8, 1) f1 = Franchise('Chris', 'Palmer', fd1) b1 = Date(2001, 4, 3) p1 = Person('The "Real" Deal', 'M', b1, False, 0.00, 1111, None, None, f1) test = serialize.to_json(p1) print(test)
def test_backslash(self): from serialize import to_json class Person(object): '''A person''' def __init__(self, name, gender, birth_date, is_cool, net_worth, debut_year, father, mother, franchise): self.name = name self.gender = gender self.birth_date = birth_date self.is_cool = is_cool self.net_worth = net_worth self.debut_year = debut_year self.father = father self.mother = mother self.franchise = franchise p1 = Person('Peter "Spidey" Parker', 'M', None, False, 15000.00, 1967, None, None, None) to_json(p1) with open('output.json') as example: data = json.load(example) print(data) self.assertEqual(data.get("name"), "Peter \"Spidey\" Parker")
def geo_fence(latitude, longitude, radius, limit=8): """ Finds closest MUNI stop in database by a user's geolocation. """ equation = "".join([ "( 3959 * acos( cos( radians(", latitude, ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(", longitude, ") ) + sin( radians(", latitude, ") ) * sin( radians( lat ) ) ) )" ]) # equation based on Haversine formula search = "".join([ "SELECT * FROM muni WHERE ", equation, " < ", str(radius), " ORDER BY ", equation, " LIMIT 0 , ", str(limit) ]) # grab all stops and returns JSON string of the data stops = db_session.query(Stop).from_statement(search).all() json_string = serialize.to_json(stops, latitude, longitude, debug=True) return json_string
self.father = father self.mother = mother self.franchise = franchise ################################################ ### Main method if __name__ == '__main__': # person 1 fd1 = Date(1962, 8, 1) f1 = Franchise('Spiderman', 'Marvel', fd1) b1 = Date(2011, 2, 3) p1 = Person('Peter "Spidey" Parker', 'M', b1, False, 15000.00, 1967, None, None, f1) # person 2p fd2 = Date(1962, 8, 1) f2 = Franchise('Superman', 'DC\\Comics', fd2) b2 = Date(2014, 5, 6) p2 = Person('Lois Lane', 'F', b2, True, 40000.50, 1981, None, None, f2) # person 3 fd3 = Date(1963, 1, 1) f3 = Franchise('Doctor Who', 'BBC', fd3) b3 = Date(2017, 8, 9) p3 = Person('River Song/Melody Pond', 'F', b3, True, 91234.56, 2001, p1, p2, f3) # print print(to_json(p3))
def test_null(self): jsonNull = to_json(self.p1) jsonNull = json.loads(jsonNull) self.assertEqual(jsonNull['father'], None)
def test_float(self): jsonFloat = to_json(self.p1) jsonFloat = json.loads(jsonFloat) self.assertEqual(jsonFloat['net_worth'], 15000.0)
def test_bool(self): jsonBool = to_json(self.p1) jsonBool = json.loads(jsonBool) self.assertEqual(jsonBool['is_cool'], False)
def test_str(self): jsonf1 = to_json(self.f1) jsonf1 = json.loads(jsonf1) self.assertEqual(jsonf1['name'], 'Spiderman') self.assertEqual(jsonf1['owner'], 'Marvel')