forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.py
165 lines (139 loc) · 4.8 KB
/
server_test.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf8 -*-
import datetime
import functools
import unittest
import assets_helper
import db
import server
import utils
# fixtures chronology
#
# A B
# +===========+ -- asset X
# | |
# <----+--[--+--[--+--]--+--]--+---> (time)
# | | | | | | |
# | | +==+=====+==+ | -- asset Y
# | | C | | D |
# | | | | |
# E F G H I -- test points
# - X XY Y - -- expected test result
date_e = datetime.datetime(2013, 1, 15, 00, 00)
date_a = datetime.datetime(2013, 1, 16, 00, 00)
date_f = datetime.datetime(2013, 1, 16, 12, 00)
date_c = datetime.datetime(2013, 1, 16, 23, 00)
date_g = datetime.datetime(2013, 1, 17, 10, 00)
date_b = datetime.datetime(2013, 1, 19, 23, 59)
date_h = datetime.datetime(2013, 1, 20, 10, 59)
date_d = datetime.datetime(2013, 1, 21, 00, 00)
asset_x = {
'mimetype': u'web',
'asset_id': u'4c8dbce552edb5812d3a866cfe5f159d',
'name': u'WireLoad',
'uri': u'http://www.wireload.net',
'start_date': date_a,
'end_date': date_b,
'duration': u'5',
'is_enabled': 1,
'nocache': 0,
'play_order': 1,
}
asset_x_diff = {
'duration': u'10'
}
asset_y = {
'mimetype': u'image',
'asset_id': u'7e978f8c1204a6f70770a1eb54a76e9b',
'name': u'Google',
'uri': u'https://www.google.com/images/srpr/logo3w.png',
'start_date': date_c,
'end_date': date_d,
'duration': u'6',
'is_enabled': 1,
'nocache': 0,
'play_order': 0,
}
asset_y_diff = {
'duration': u'324'
}
asset_z = {
'mimetype': u'image',
'asset_id': u'9722cd9c45e44dc9b23521be8132b38f',
'name': u'url test',
'start_date': date_c.isoformat(),
'end_date': date_d.isoformat(),
'duration': u'1',
'is_enabled': 1,
'nocache': 0,
}
url_fail = 'http://doesnotwork.example.com'
url_redir = 'http://example.com'
uri_ = '/home/user/file'
#url_timeout = 'http://...'
class Req():
def __init__(self, asset):
self.POST = asset
class URLHelperTest(unittest.TestCase):
def test_url_1(self):
self.assertTrue(server.url_fails(url_fail))
def test_url_2(self):
self.assertFalse(server.url_fails(url_redir))
def test_url_3(self):
self.assertFalse(server.url_fails(uri_))
class DBHelperTest(unittest.TestCase):
def setUp(self):
self.assertEmpty = functools.partial(self.assertEqual, [])
self.conn = db.conn(':memory:')
with db.commit(self.conn) as cursor:
cursor.execute(assets_helper.create_assets_table)
def tearDown(self):
self.conn.close()
# ✂--------
def test_create_read_asset(self):
assets_helper.create(self.conn, asset_x)
assets_helper.create(self.conn, asset_y)
should_be_y_x = assets_helper.read(self.conn)
self.assertEqual([asset_y, asset_x], should_be_y_x)
# ✂--------
def test_create_update_read_asset(self):
assets_helper.create(self.conn, asset_x)
asset_x_ = asset_x.copy()
asset_x_.update(**asset_x_diff)
assets_helper.update(self.conn, asset_x['asset_id'], asset_x_)
assets_helper.create(self.conn, asset_y)
asset_y_ = asset_y.copy()
asset_y_.update(**asset_y_diff)
assets_helper.update(self.conn, asset_y['asset_id'], asset_y_)
should_be_y__x_ = assets_helper.read(self.conn)
self.assertEqual([asset_y_, asset_x_], should_be_y__x_)
# ✂--------
def test_create_delete_asset(self):
assets_helper.create(self.conn, asset_x)
assets_helper.delete(self.conn, asset_x['asset_id'])
assets_helper.create(self.conn, asset_y)
assets_helper.delete(self.conn, asset_y['asset_id'])
should_be_empty = assets_helper.read(self.conn)
self.assertEmpty(should_be_empty)
# ✂--------
def set_now(self, d):
assets_helper.get_time = lambda: d
def test_get_playlist(self):
assets_helper.create(self.conn, asset_x)
assets_helper.create(self.conn, asset_y)
self.set_now(date_e)
should_be_empty = assets_helper.get_playlist(self.conn)
self.assertEmpty(should_be_empty)
self.set_now(date_f)
[should_be_x] = assets_helper.get_playlist(self.conn)
self.assertEqual(asset_x['asset_id'], should_be_x['asset_id'])
self.set_now(date_g)
should_be_y_x = assets_helper.get_playlist(self.conn)
self.assertEqual([should_be_y_x[0]['asset_id'],
should_be_y_x[1]['asset_id']],
[asset_y['asset_id'],
asset_x['asset_id']])
self.set_now(date_h)
[should_be_y] = assets_helper.get_playlist(self.conn)
self.assertEqual(asset_y['asset_id'], should_be_y['asset_id'])
# ✂--------