示例#1
0
    def test_delete_team_valid(self):
        # Test API DELETE method api/team

        valid_team = None
        test_id = -1
        test_name = 'API TEST DELETE TEAM'

        # Insert test user into database to get using the API
        valid_team = Team()
        valid_team.id = test_id
        valid_team.name = test_name
        try:
            db.session.add(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()

        # Delete the db instance through an API call
        response = requests.delete(self.team_url + '/' + str(test_id),
                                   headers=self.headers)

        self.assertEqual(response.status_code, 204)

        # Make sure db instance is no longer in db
        try:
            query = Team.query.get(test_id)
            self.assertisNone(query)
            db.session.close()
        except:
            db.session.rollback()
示例#2
0
    def test_get_single_team_valid(self):
        # Test API GET method api/team/(int:id) with valid id

        valid_team = None
        test_id = -1
        test_name = 'API TEST GET TEAM'

        # Insert test user into database to get using the API
        valid_team = Team()
        valid_team.id = test_id
        valid_team.name = test_name
        try:
            db.session.add(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()

        # Make sure API call gets and matches the test user just entered into the db above
        response = requests.get(self.team_url + '/' + str(test_id),
                                headers=self.headers)
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.text)
        self.assertEqual(json_response.get('id'), test_id)
        self.assertEqual(json_response.get('name'), test_name)

        # Delte the test user that was inserted earlier in this
        try:
            db.session.delete(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()
示例#3
0
def lookup_teams():

    global users_dict
    global games_dict

    json = requests.get(teams_api_url, headers=headers).json()

    for t in json.get('teams'):
        team_api_url = teams_api_url + str(t.get('name'))
        team = requests.get(team_api_url, headers=headers).json()
        team_db = Team()
        team_db.name = team.get('display_name')
        team_db.id = team.get('_id')
        team_db.info = team.get('info')
        team_db.image_url = team.get('logo')
        if team_db.image_url == None:
            team_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png'  # This is Twitch's default logo
        team_db.created = team.get('created_at')
        team_db.updated = team.get('updated_at')
        user_ids = []
        game_ids = []

        for user in team.get('users'):
            user_id = user.get('_id')
            user_ids.append(user_id)
            game_id = lookup_user(team_db, user_id)

            # for k in users_dict:
            # 	users_d[k] = users_dict[k]

            if game_id != None:
                game_ids.append(game_id)

            # for key in games_dict:
            # 	games_d[key] = games_dict[key]

        team_db.user_ids = user_ids
        team_db.game_ids = game_ids

        try:
            db.session.add(team_db)
            db.session.commit()
            db.session.close()
        except Exception as e:
            print(str(e) + '\n')
            db.session.rollback()
示例#4
0
    def test_update_team_valid(self):
        # Test API PUT method api/team

        valid_team = None
        test_id = -1
        test_name = 'API TEST UPDATE TEAM'

        # Insert test user into database to get using the API
        valid_team = Team()
        valid_team.id = test_id
        valid_team.name = test_name
        try:
            db.session.add(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()

        new_test_name = 'NEW API TEST UPDATE TEAM'

        update = {'name': new_test_name}

        # Update the user through the API
        response = requests.put(self.team_url + '/' + str(test_id),
                                data=json.dumps(update),
                                headers=self.headers)

        # Make sure instance was updated
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.text)
        self.assertEqual(json_response.get('name'), new_test_name)

        # Delete the test instance for cleanup
        try:
            updated_query = Team.query.filter_by(id=test_id)
            self.assertEqual(
                updated_query.count(),
                1)  # Make sure update did not create duplicate entry
            updated_team = updated_query.first()
            db.session.delete(updated_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()
示例#5
0
    def test_get_team_search_match(self):
        # Test API GET method api/team?q=<searchjson> with a match

        valid_team = None
        test_id = -1
        test_name = 'API TEST GET TEAM THROUGH SEARCH'
        test_info = 'Made Up Team Info'

        # Insert test user into database to get using the API
        valid_team = Team()
        valid_team.id = test_id
        valid_team.name = test_name
        valid_team.info = test_info
        try:
            db.session.add(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()

        filters = [dict(name='info', op='ilike', val='made up team info')]
        params = dict(q=json.dumps(dict(filters=filters)))
        response = requests.get(self.team_url,
                                params=params,
                                headers=self.headers)
        json_response = json.loads(response.text)

        # Make sure API call searches and matches the test user just entered into the db above
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json_response.get('num_results'), 1)
        self.assertEqual(json_response.get('objects')[0].get('id'), test_id)
        self.assertEqual(
            json_response.get('objects')[0].get('name'), test_name)
        self.assertEqual(
            json_response.get('objects')[0].get('info'), test_info)

        # Delte the test user that was inserted earlier in this
        try:
            db.session.delete(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()