1
+ from apistar import test
2
+ from app import app , users , ID_NOT_FOUND
3
+
4
+ client = test .TestClient (app )
5
+
6
+ def test_list_users ():
7
+ response = client .get ('/' )
8
+ assert response .status_code == 200
9
+ users = response .json ()
10
+ assert len (users ) == 1000
11
+ assert type (users ) == list
12
+ user = users [0 ]
13
+ expected = {"id" :1 ,"first_name" :"Sterne" ,"last_name" :"Gell" ,\
14
+ "bitcoin_address" :"15czwRiYGZ6PGM8EjkMS38XBNmkiohSExs" ,\
15
+ "social_security_number" :"283-62-6536" }
16
+ assert user == expected
17
+ last_id = users [- 1 ]["id" ]
18
+ assert last_id == 1000
19
+
20
+ def test_create_user ():
21
+ data = dict (first_name = "Kevin" ,
22
+ last_name = "Bisner" ,
23
+ bitcoin_address = "15czwRiYG78SGM8EjkMS38XBNmkiohSExs" ,
24
+ social_security_number = "110-51-1732" )
25
+ response = client .post ('/' , data = data )
26
+ assert response .status_code == 201
27
+ assert len (users ) == 1001
28
+
29
+ response = client .get ('/1001/' )
30
+ assert response .status_code == 200
31
+ expected = {"id" :1001 , "first_name" :"Kevin" , "last_name" :"Bisner" ,\
32
+ "bitcoin_address" :"15czwRiYG78SGM8EjkMS38XBNmkiohSExs" ,\
33
+ "social_security_number" :"110-51-1732" }
34
+ assert response .json () == expected
35
+
36
+ def test_create_user_missing_field ():
37
+ data = {'key' : 1 }
38
+ response = client .post ('/' , data = data )
39
+ assert response .status_code == 400
40
+
41
+ errors = response .json ()
42
+ assert errors ['social_security_number' ] == \
43
+ 'The "social_security_number" field is required.'
44
+
45
+ def test_create_user_field_validation ():
46
+ data = {'bitcoin_address' : 'x' * 35 ,
47
+ 'social_security_number' : 123456789 }
48
+ response = client .post ('/' , data = data )
49
+ assert response .status_code == 400
50
+
51
+ errors = response .json ()
52
+ assert errors ['bitcoin_address' ] == 'Must have no more than 34 characters.'
53
+ assert errors ['social_security_number' ] == 'Must have at least 11 characters.'
54
+
55
+ def test_get_user ():
56
+ response = client .get ('/813/' )
57
+ assert response .status_code == 200
58
+
59
+ expected = {"id" :813 ,"first_name" :"Keven" ,"last_name" :"Jirka" ,
60
+ "bitcoin_address" :"1HdhruUcmU1z3fDhz9b6FzajLuE3LrzoB8" ,
61
+ "social_security_number" :"898-62-9881" }
62
+ assert response .json () == expected
63
+
64
+ def test_get_user_notfound ():
65
+ response = client .get ('/11111/' )
66
+ assert response .status_code == 404
67
+ assert response .json () == {'error' : ID_NOT_FOUND }
68
+
69
+ def test_update_user ():
70
+ data = {'first_name' : 'Kevin' ,
71
+ 'last_name' : 'Bisner' ,
72
+ 'bitcoin_address' : 'HG7thak2968HJkLllaiw82FGlQ32K103hZ' ,
73
+ 'social_security_number' : '100-33-1129' }
74
+ response = client .put ('/777/' , data = data )
75
+ assert response .status_code == 200
76
+
77
+ #test put response
78
+ expected = {'id' : 777 , 'first_name' : 'Kevin' ,
79
+ 'last_name' : 'Bisner' ,
80
+ 'bitcoin_address' : 'HG7thak2968HJkLllaiw82FGlQ32K103hZ' ,
81
+ 'social_security_number' : '100-33-1129' }
82
+ assert response .json () == expected
83
+
84
+ #check if data persisted == wiped our previous data car 777
85
+ response = client .get ('/777/' )
86
+ assert response .json () == expected
87
+
88
+ def test_update_user_notfound ():
89
+ data = {'first_name' : 'Kevin' ,
90
+ 'last_name' : 'Bisner' ,
91
+ 'bitcoin_address' : 'HG7thak2968HJkLllaiw82FGlQ32K103hZ' ,
92
+ 'social_security_number' : '100-33-1129' }
93
+ response = client .put ('/11111/' , data = data )
94
+
95
+ assert response .status_code == 404
96
+ assert response .json () == {'error' : ID_NOT_FOUND }
97
+
98
+ def test_update_user_validation ():
99
+ data = {'first_name' : 'Kevin' ,
100
+ 'last_name' : 'Bisner' ,
101
+ 'bitcoin_address' : 's' * 35 ,
102
+ 'social_security_number' : '123456789' }
103
+ response = client .put ('/777/' , data = data )
104
+ assert response .status_code == 400
105
+
106
+ errors = response .json ()
107
+ assert errors ['bitcoin_address' ] == 'Must have no more than 34 characters.'
108
+ assert errors ['social_security_number' ] == 'Must have at least 11 characters.'
109
+
110
+ def test_delete_user ():
111
+ user_count = len (users )
112
+
113
+ for i in (11 , 22 , 33 ):
114
+ response = client .delete (f'/{ i } /' )
115
+ assert response .status_code == 204
116
+
117
+ response = client .get (f'/{ i } /' )
118
+ assert response .status_code == 404 #user_gone
119
+
120
+ assert len (users ) == user_count - 3
0 commit comments