TestUser.test_get()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
c 4
b 0
f 0
dl 0
loc 10
rs 9.4285
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(
31
            response_json,
32
            {'user': {'age': 25, 'first_name': 'John', 'last_name': 'Doe'}}
33
        )
34
35
    def test_create(self):
36
        """ The POST on `/user` should create an user """
37
        response = self.client.post(
38
            '/application/user/Doe/John',
39
            content_type='application/json',
40
            data=json.dumps({
41
                'age': 30
42
            })
43
        )
44
45
        self.assertEqual(response.status_code, 200)
46
        response_json = json.loads(response.data.decode('utf-8'))
47
        self.assertEqual(
48
            response_json,
49
            {'user': {'age': 30, 'first_name': 'John', 'last_name': 'Doe'}}
50
        )
51
        self.assertEqual(User.query.count(), 1)
52
53
    def test_update(self):
54
        """ The PUT on `/user` should update an user's age """
55
        UserRepository.create(first_name='John', last_name='Doe', age=25)
56
        response = self.client.put(
57
            '/application/user/Doe/John',
58
            content_type='application/json',
59
            data=json.dumps({
60
                'age': 30
61
            })
62
        )
63
64
        self.assertEqual(response.status_code, 200)
65
        response_json = json.loads(response.data.decode('utf-8'))
66
        self.assertEqual(
67
            response_json,
68
            {'user': {'age': 30, 'first_name': 'John', 'last_name': 'Doe'}}
69
        )
70
        user = UserRepository.get(first_name='John', last_name='Doe')
71
        self.assertEqual(user.age, 30)
72