def test_set_data(self): data = Data('name') expected = Item(data=[data]) item = Item() item.data = [data] self.assertIsInstance(item.data, Array) self.assertEqual(item, expected)
def test_set_links(self): link = Link('rel', 'href') expected = Item(links=[link]) item = Item() item.links = [link] self.assertIsInstance(item.links, Array) self.assertEqual(item, expected)
def test_to_dict_with_data(self): data = [Data('name')] links = [Link('href', 'rel')] item = Item('href', data, links) expected = { 'href': 'href', 'data': [ {'name': 'name'} ], 'links': [ {'href': 'href', 'rel': 'rel'} ] } self.assertEqual(item.to_dict(), expected)
def list_tables(): with DBcm.UseDatabase(app.config["DB_CONFIG"]) as cursor: cursor.execute("""SHOW TABLES """) result = cursor.fetchall() if len(result) is not 0: for table in result: table_name = table[0] i = Item(data=[Data(name='table', value=table_name)], links=[Link(href='/api/table/post?name=' + table_name, rel='GET')]) collection.items.append(i) i.href = '/api/table/list' return Response(json.dumps(collection.to_dict(), indent=4, sort_keys=True), status=200, mimetype='application/vnd.collection+json') else: # if no result found, send 400 "Bad Request return Response(status=404, mimetype='application/vnd.collection+json')
def get(self): #get users from database users = g.con.get_users() template = { "data": [{ 'name': 'Username', 'value': '', 'prompt': '' }, { 'name': 'Realname', 'value': '', 'prompt': '' }, { 'name': 'Email', 'value': '', 'prompt': '' }] } links = [ #{"href": "www.sangz.com","rel": "home"}, { "href": api.url_for(Songs), "rel": "Songs", 'prompt': 'See the list of all songs' }, { "href": api.url_for(Playlist), "rel": "Playlist", "prompt": "See the current playlist" }, { "href": api.url_for(Chat), "rel": "Chat", "prompt": "See the conversation" } ] #links = {} collection = Collection(api.url_for(Users), template=template, links=links) collection.version = "1.0" for user in users: item = Item(api.url_for(User, userid=user[0])) item.data.append(Data("Id", user[0])) item.data.append(Data("nickname", user[1])) collection.items.append(item) string_data = str(collection) return Response(string_data, 200, mimetype="application/vnd.collection+json" + ";" + APIARY_PROFILES_URL)
def test_set_links_invalid(self): item = Item() invalid_obj = object() with self.assertRaises(TypeError): item.links = invalid_obj
def test_set_data_invalid(self): item = Item() invalid_obj = object() with self.assertRaises(TypeError): item.data = invalid_obj
def test_to_dict_minimal(self): item = Item() expected = {} self.assertEqual(item.to_dict(), expected)
def get(self): #connect to the db args = request.args try: user_id = int(args['user_id']) except (KeyError, ValueError): user_id = None # songs_db = g.con.get_songs() old non filtering db call songs_db = g.con.get_songs_filtered(user_id=user_id) links = [ #{"href": "www.sangz.com", "rel": "home"}, { "href": api.url_for(Users), "rel": "Users", "prompt": "Get the list of all users" }, { "href": api.url_for(Playlist), "rel": "Playlist", "prompt": "See the current playlist" }, { "href": api.url_for(Chat), "rel": "Chat", "prompt": "See the conversation" } ] template = { "data": [{ "prompt": "", "name": "user_id", "value": "" }, { "prompt": "", "name": "song_name", "value": "" } #{"prompt": "", "name":"media_location", #"value":""}, #{"prompt": "", "name":"media_type", #"value":""}, #{"prompt": "", "name":"artist_id", #"value":""}, #{"prompt": "", "name":"album_id", #"value":""}, #{"prompt": "", "name":"user_id", #"value":""} ] } collection = Collection(api.url_for(Songs), template=template, links=links) collection.version = "1.0" #create items print songs_db for song in songs_db: print song item = Item(api.url_for(Song, songid=song[0])) item.data.append(Data("ID", song[0])) item.data.append(Data("songname", song[1])) item.data.append( Data("uploader", api.url_for(User, userid=song[2]))) #links to artist and album are unavailable because they are not implemented collection.items.append(item) string_data = str(collection) return Response(string_data, 200, mimetype="application/vnd.collection+json" + ";" + SANGZ_SONG_PROFILE)