forked from notedit/motiky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_post.py
135 lines (100 loc) · 4.5 KB
/
test_post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
import os
import sys
import types
import json
import time
from datetime import datetime
from datetime import timedelta
from StringIO import StringIO
from tests import TestCase
from motiky.logic.models import User,Post,UserLikeAsso,Report,Install,\
UserFollowAsso,Comment,Activity,Action
from motiky.logic import backend
from motiky.configs import db,redis
class TestPost(TestCase):
def test_post_view(self):
# post
user1 = backend.add_user('username01','photo_url01','weibo_id01')
headers = self.generate_header('weibo_id01')
data = {
'title':'title01',
'author_id':user1['id'],
'video_file':(StringIO('AAAA' * 10000),'hello.mp4'),
'pic_file':(StringIO('AAAA' * 1000),'hello.png')
}
resp = self.client.post('/post',data=data,headers=headers)
_data = json.loads(resp.data)
assert resp.status_code == 200
assert _data['title'] == 'title01'
# get
resp = self.client.get('/post/%d' % _data['id'],headers=headers)
data_get = json.loads(resp.data)
print resp.data
assert data_get['title'] == 'title01'
# put
put_in = {'pic_small':'pic_small'}
resp = self.client.put('/post/%d'%_data['id'],data=json.dumps(put_in),
headers=headers,content_type='application/json')
print resp.data
assert resp.status_code == 204
def test_posts_view(self):
user1 = backend.add_user('username01','photo_url01','weibo_id01')
post1 = backend.add_post('post01',user1['id'],
'video_url','pic_small1')
post2 = backend.add_post('post02',user1['id'],
'video_url','pic_small2')
post3 = backend.add_post('post03',user1['id'],
'video_url','pic_small3')
headers = self.generate_header('weibo_id01')
resp = self.client.get('/posts',headers=headers)
data_get = json.loads(resp.data)
assert len(data_get['posts']) == 3
def test_user_posts(self):
user1 = backend.add_user('username01','photo_url01','weibo_id01')
post1 = backend.add_post('post01',user1['id'],
'video_url','pic_small1')
post2 = backend.add_post('post02',user1['id'],
'video_url','pic_small2')
post3 = backend.add_post('post03',user1['id'],
'video_url','pic_small3')
headers = self.generate_header('weibo_id01')
resp = self.client.get('/posts/user/%d'%user1['id'],headers=headers)
data_get = json.loads(resp.data)
assert len(data_get['posts']) == 3
def test_user_liked_posts(self):
user1 = backend.add_user('username01','photo_url01','weibo_id01')
post1 = backend.add_post('post01',user1['id'],
'video_url','pic_small1')
post2 = backend.add_post('post02',user1['id'],
'video_url','pic_small2')
post3 = backend.add_post('post03',user1['id'],
'video_url','pic_small3')
headers = self.generate_header('weibo_id01')
ula1 = UserLikeAsso(user_id=user1['id'],post_id=post1['id'])
ula2 = UserLikeAsso(user_id=user1['id'],post_id=post2['id'])
ula3 = UserLikeAsso(user_id=user1['id'],post_id=post3['id'])
db.session.add(ula1)
db.session.add(ula2)
db.session.add(ula3)
db.session.commit()
resp = self.client.get('/posts/user/%d/liked'%user1['id'],headers=headers)
data_get = json.loads(resp.data)
assert len(data_get['posts']) == 3
def test_post_like(self):
user1 = backend.add_user('username01','photo_url01','weibo_id01')
post1 = backend.add_post('post01',user1['id'],
'video_url','pic_small1')
headers = self.generate_header('weibo_id01')
_data = {
'user_id':user1['id'],
'post_id':post1['id']
}
resp = self.client.post('/post/like',data=json.dumps(_data),
headers=headers,content_type='application/json')
data_get = json.loads(resp.data)
assert data_get['like_count'] == 1
resp = self.client.post('/post/unlike',data=json.dumps(_data),
headers=headers,content_type='application/json')
data_get = json.loads(resp.data)
assert data_get['like_count'] == 0