<commit_before># -*- coding: utf-8 -*-

import unittest
import json

from flask import url_for

from app import create_app, db
from app.models import  User, Todo


class TodolistAPITestCase(unittest.TestCase):

    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    @staticmethod
    def setup_new_user(username):
        user_data = {
            'username': username,
            'email': username + '@example.com',
            'password': '<PASSWORD>'
        }
        return user_data

    @staticmethod
    def get_headers():
        return {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    def add_user(self, username):
        user_data = self.setup_new_user(username)
        user = User(**user_data)
        db.session.add(user)
        db.session.commit()
        return User.query.filter_by(username=username).first()

    def add_user_through_json_post(self, username):
        user_data = self.setup_new_user(username)
        return self.client.post(url_for('api.add_user'),
                                headers=self.get_headers(),
                                data=json.dumps(user_data))

    # test for errors
    def test_bad_request(self):
        post_response = self.client.post(url_for('api.add_user'),
                                         headers=self.get_headers(), data='')
        self.assertEqual(post_response.status_code, 400)

        json_response = json.loads(post_response.data.decode('utf-8'))
        self.assertEqual(json_response['error'], 'Bad Request')

        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'], [])

    def test_not_found(self):
        response = self.client.get('/api/not/found')
        self.assertEqual(response.status_code, 404)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['error'], 'Not found')

    # test api post calls
    def test_add_user(self):
        username = 'adam'
        post_response = self.add_user_through_json_post(username)
        self.assertEqual(post_response.headers['Content-Type'],
                         'application/json')
        self.assertEqual(post_response.status_code, 201)

        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'][0]['user']['username'],
                         username)

    def test_add_todolist(self):
        post_response = self.client.post(url_for('api.add_todolist'),
                                    headers=self.get_headers(),
                                    data=json.dumps({'title': 'todolist'}))
        self.assertEqual(post_response.status_code, 201)

        # the expected id of the todolist is 1, as it is the first to be added
        response = self.client.get(url_for('api.get_todolist', id=1))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['todolist']['title'], 'todolist')

    def test_add_user_todolist(self):
        pass

    def test_add_todolist_todo(self):
        pass

    def test_add_todo(self):
        pass

    # test api get calls
    def test_get_users(self):
        username = 'adam'
        new_user = self.add_user(username)
        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'][0]['user']['username'],
                         username)

    def test_get_user(self):
        username = 'adam'
        new_user = self.add_user(username)
        response = self.client.get(url_for('api.get_user', username=username))
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['user']['username'], username)

    def test_get_anon_todolists(self):
        pass

    def test_get_user_todolists(self):
        pass

    def test_get_anon_todolist_todos(self):
        pass

    def test_get_user_todolist_todos(self):
        pass<commit_msg>Added two post API tests.<commit_after># -*- coding: utf-8 -*-

import unittest
import json

from flask import url_for

from app import create_app, db
from app.models import  User, Todo, TodoList


class TodolistAPITestCase(unittest.TestCase):

    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    @staticmethod
    def setup_new_user(username):
        user_data = {
            'username': username,
            'email': username + '@example.com',
            'password': '<PASSWORD>'
        }
        return user_data

    @staticmethod
    def get_headers():
        return {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    def add_user(self, username):
        user_data = self.setup_new_user(username)
        User(**user_data).save()
        return User.query.filter_by(username=username).first()

    def add_todolist(self, username, title):
        todolist = TodoList(title=title, creator=username).save()
        return TodoList.query.filter_by(id=todolist.id).first()

    def add_user_through_json_post(self, username):
        user_data = self.setup_new_user(username)
        return self.client.post(url_for('api.add_user'),
                                headers=self.get_headers(),
                                data=json.dumps(user_data))

    # test for errors
    def test_bad_request(self):
        post_response = self.client.post(url_for('api.add_user'),
                                         headers=self.get_headers(), data='')
        self.assertEqual(post_response.status_code, 400)

        json_response = json.loads(post_response.data.decode('utf-8'))
        self.assertEqual(json_response['error'], 'Bad Request')

        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'], [])

    def test_not_found(self):
        response = self.client.get('/api/not/found')
        self.assertEqual(response.status_code, 404)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['error'], 'Not found')

    # test api post calls
    def test_add_user(self):
        username = 'adam'
        post_response = self.add_user_through_json_post(username)
        self.assertEqual(post_response.headers['Content-Type'],
                         'application/json')
        self.assertEqual(post_response.status_code, 201)

        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'][0]['user']['username'],
                         username)

    def test_add_todolist(self):
        post_response = self.client.post(
            url_for('api.add_todolist'),
            headers=self.get_headers(),
            data=json.dumps({'title': 'todolist'})
        )
        self.assertEqual(post_response.status_code, 201)

        # the expected id of the todolist is 1, as it is the first to be added
        response = self.client.get(url_for('api.get_todolist', todolist_id=1))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['todolist']['title'], 'todolist')

    def test_add_user_todolist(self):
        username = 'adam'
        new_user = self.add_user(username)
        post_response = self.client.post(
            url_for('api.add_user_todolist', username=username),
            headers=self.get_headers(),
            data=json.dumps({'title': 'todolist'})
        )
        self.assertEqual(post_response.status_code, 201)

        response = self.client.get(url_for('api.get_user_todolists',
                                           username=username))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))

        # check title, creator are set correctly and a total of one todolist
        self.assertEqual(json_response['todolists'][0]['title'], 'todolist')
        self.assertEqual(json_response['todolists'][0]['creator'], username)
        self.assertEqual(len(json_response['todolists']), 1)

    def test_add_user_todolist_todo(self):
        username = 'adam'
        todolist_title = 'new todolist'
        new_user = self.add_user(username)
        new_todolist = self.add_todolist(username, todolist_title)

        post_response = self.client.post(
            url_for('api.add_user_todolist_todo',
                    username=username, todolist_id=new_todolist.id),
            headers=self.get_headers(),
            data=json.dumps({
                'description': 'new todo',
                'creator': username,
                'todolist_id': new_todolist.id
            })
        )
        self.assertEqual(post_response.status_code, 201)

        response = self.client.get(url_for('api.get_user_todolist_todos',
                                           username=username,
                                           todolist_id=new_todolist.id))
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data.decode('utf-8'))

        # check title, creator are set correctly and a total of one todo
        self.assertEqual(json_response['todos'][0]['description'], 'new todo')
        self.assertEqual(json_response['todos'][0]['creator'], username)
        self.assertEqual(len(json_response['todos']), 1)

    def test_add_todolist_todo(self):
        pass

    # test api get calls
    def test_get_users(self):
        username = 'adam'
        new_user = self.add_user(username)
        response = self.client.get(url_for('api.get_users'))
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['users'][0]['user']['username'],
                         username)

    def test_get_user(self):
        username = 'adam'
        new_user = self.add_user(username)
        response = self.client.get(url_for('api.get_user', username=username))
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['user']['username'], username)

    def test_get_todolists(self):
        pass

    def test_get_user_todolists(self):
        pass

    def test_get_todolist_todos(self):
        pass

    def test_get_user_todolist_todos(self):
        pass
{'fraction_non_alphanumeric': 0.1196639807989028, 'fraction_numerical': 0.008400480027430138, 'mean_word_length': 2.4889354066985647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '186024', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3447, 'n_words': 546}