-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_render_1.py
169 lines (137 loc) · 5.62 KB
/
test_render_1.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
166
167
168
169
import sys
import pytest
import render_1
import models
from sqlalchemy import create_engine
from datetime import datetime
import mock
import unittest
import os
import factory
import factory.alchemy
from models import RenderCache_Model, UserJob_Model
@pytest.fixture(scope='session', autouse=True)
def connection(request):
engine = create_engine('postgresql://postgres@/test_bar')
models.Base.metadata.create_all(engine)
connection = engine.connect()
models.DBSession.registry.clear()
models.DBSession.configure(bind=connection)
models.Base.metadata.bind = engine
request.addfinalizer(models.Base.metadata.drop_all)
return connection
@pytest.fixture(autouse=True)
def db_session(request, connection):
from transaction import abort
trans = connection.begin()
request.addfinalizer(trans.rollback)
request.addfinalizer(abort)
from models import DBSession
return DBSession
@pytest.mark.usefixtures("connection", "db_session")
class JobFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta():
model = models.UserJob_Model
sqlalchemy_session = db_session
jobstatus = 0
starttime = datetime.utcnow()
lastmodified = datetime.utcnow()
band1 = u'4'
band2 = u'3'
band3 = u'2'
entityid = u'LC80470272015005LGN00'
email = u'test@test.com'
jobid = factory.Sequence(lambda n: n)
@pytest.fixture(scope='class')
def fake_job1(db_session):
model_instance = models.UserJob_Model(
jobstatus=0,
starttime=datetime.utcnow(),
lastmodified=datetime.utcnow()
)
db_session.add(model_instance)
db_session.flush()
# --- test db functionality tests
def test_db_lookup(db_session):
model_instance = models.UserJob_Model(jobstatus=0,
starttime=datetime.utcnow(),
lastmodified=datetime.utcnow())
db_session.add(model_instance)
db_session.flush()
assert 1 == db_session.query(models.UserJob_Model).count()
def test_db_is_rolled_back(db_session):
assert 0 == db_session.query(models.UserJob_Model).count()
# --- process tests
@pytest.mark.usefixtures("connection", "db_session", "fake_job1")
class TestProcess(unittest.TestCase):
fake_job_message = {u'job_id': u'1',
u'band_2': u'3',
u'band_3': u'2',
u'band_1': u'4',
u'scene_id': u'LC80470272015005LGN00',
u'email': u'test@test.com'}
test_input_path = os.getcwd() + '/download/LC80470272015005LGN00'
test_bands = [u'4', u'3', u'2']
test_scene_id = 'LC80470272015005LGN00'
test_band_output = '432'
test_file_location = (os.getcwd() +
'/download/LC80470272015005LGN00/LC80470272015005LGN00_bands_432.TIF')
test_file_name_zip = 'LC80470272015005LGN00_bands_432.zip'
@mock.patch('worker.render_1.Downloader')
def test_download_returns_correct_values(self, Downloader):
input_path, bands, scene_id = (render_1.download_and_set(
self.fake_job_message, render_1.PATH_DOWNLOAD))
self.assertEqual(input_path,
os.getcwd() + '/download/LC80470272015005LGN00')
self.assertEqual(bands, [u'4', u'3', u'2'])
self.assertEqual(scene_id, 'LC80470272015005LGN00')
@mock.patch('worker.render_1.Process')
def test_merge_images(self, Process):
band_output, file_location = (render_1.merge_images(
self.fake_job_message,
self.test_input_path,
self.test_bands,
render_1.PATH_DOWNLOAD,
self.test_scene_id)
)
self.assertEqual(band_output, '432')
self.assertEqual(
file_location, os.getcwd() +
'/download/LC80470272015005LGN00/LC80470272015005LGN00_bands_432.TIF')
@mock.patch('worker.render_1.zipfile')
def test_zip_file(self, zipfile):
file_name_zip = render_1.zip_file(self.fake_job_message,
self.test_band_output,
self.test_scene_id,
self.test_input_path,
self.test_file_location)
self.assertEqual(file_name_zip, "LC80470272015005LGN00_bands_432.zip")
@mock.patch('worker.render_1.Key')
@mock.patch('worker.render_1.boto')
def test_upload_to_s3(self, boto, Key):
file_location = render_1.upload_to_s3(self.test_file_location,
self.test_file_name_zip,
self.test_input_path,
self.fake_job_message
)
self.assertEqual(
file_location, os.getcwd() +
'/download/LC80470272015005LGN00/LC80470272015005LGN00_bands_432.zip')
@mock.patch('worker.render_1.Key')
@mock.patch('worker.render_1.boto')
def test_upload_to_s3_fails_with_exception(self, boto, Key):
# missing job argument to cause exception
with self.assertRaises(Exception):
render_1.upload_to_s3(self.test_file_location,
self.test_file_name_zip,
self.test_input_path,
None
)
def test_cleanup_downloads():
test_dir = os.getcwd() + '/testdir'
if not os.path.exists(test_dir):
os.makedirs(test_dir)
f = open(test_dir + '/test.txt', 'a')
f.write('this is a test')
f.close()
assert render_1.cleanup_downloads(test_dir) == True