Completed
Push — master ( 415925...6a3766 )
by Antoine
01:00
created

TestUser.test_create()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
1
import unittest
2
import json
3
4
from server import server
5
from models.abc import db
6
from models import User
7
from repositories import UserRepository
8
9
10
class TestUser(unittest.TestCase):
11
12
    @classmethod
13
    def setUpClass(cls):
14
        cls.client = server.test_client()
15
16
    def setUp(self):
17
        db.create_all()
18
19
    def tearDown(self):
20
        db.session.remove()
21
        db.drop_all()
22
23
    def test_get(self):
24
        """ The GET on `/user` should return an user """
25
        UserRepository.create(first_name='John', last_name='Doe', age=25)
26
        response = self.client.get('/application/user/Doe/John')
27
28
        self.assertEqual(response.status_code, 200)
29
        response_json = json.loads(response.data.decode('utf-8'))
30
        self.assertEqual(response_json, {'user': {'age': 25, 'first_name': 'John', 'last_name': 'Doe'}})
31
32
    def test_create(self):
33
        """ The POST on `/user` should create an user """
34
        response = self.client.post(
35
            '/application/user/Doe/John',
36
            content_type='application/json',
37
            data=json.dumps({
38
                'age': 30
39
            })
40
        )
41
42
        self.assertEqual(response.status_code, 200)
43
        response_json = json.loads(response.data.decode('utf-8'))
44
        self.assertEqual(response_json, {'user': {'age': 30, 'first_name': 'John', 'last_name': 'Doe'}})
45
        self.assertEqual(User.query.count(), 1)
46
47
    def test_update(self):
48
        """ The PUT on `/user` should update an user's age """
49
        UserRepository.create(first_name='John', last_name='Doe', age=25)
50
        response = self.client.put(
51
            '/application/user/Doe/John',
52
            content_type='application/json',
53
            data=json.dumps({
54
                'age': 30
55
            })
56
        )
57
58
        self.assertEqual(response.status_code, 200)
59
        response_json = json.loads(response.data.decode('utf-8'))
60
        self.assertEqual(response_json, {'user': {'age': 30, 'first_name': 'John', 'last_name': 'Doe'}})
61
        user = UserRepository.get(first_name='John', last_name='Doe')
62
        self.assertEqual(user.age, 30)
63