def test_image_cache(self): self.assertEqual(RawFile.objects.count(), 0) self.assertEqual(Image.objects.count(), 0) rf = RawFile() rf.file = self.uploadFile('test.JPEG') self.assertEqual(rf.ext, 'jpg') rf.save() self.assertEqual(rf.ext, 'jpg') self.assertEqual(RawFile.objects.count(), 1) self.assertEqual(Image.objects.count(), 0) img, is_created = Image.get_or_create_smart(rf.url) self.assertValidLocalFile(img.path_accessed) self.assertValidInternetUrl(img.url_accessed) f = Path(img.path_accessed) self.assertEqual(f.is_symlink(), True) self.assertEqual(Image.objects.count(), 1) img.save() self.assertEqual(Image.objects.count(), 1) self.assertValidInternetUrl(img.url_accessed) self.assertValidInternetUrl(img.url_summarized) self.assertEqual(img, Image.objects.first()) self.assertEqual(rf.img, img) self.assertEqual(img.rf, rf) img = Image.objects.first() rf = RawFile.objects.first() self.assertEqual(rf.img, img) self.assertEqual(img.rf, rf)
def test_content_property(self): test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' img, is_created = Image.get_or_create_smart(test_data) saved = Image.objects.first() url = test_data self.assertEqual(img.content, url) self.assertEqual(saved, img) self.assertEqual(saved.content, img.content) img2 = Image() img2.content = 'http://static.naver.net/www/mobile/edit/2016/0407/mobile_17004159045.png' img2.save() img2.summarize() self.assertNotEqual(img2, img) img3 = Image() img3.content = 'http://static.naver.net/www/mobile/edit/2016/0407/mobile_17004159045.png' img3.save() img3.summarize() self.assertEqual(img3, img2) rf4 = RawFile() rf4.file = self.uploadFile('test.jpg') rf4.save() img4, is_created = Image.get_or_create_smart(rf4.url) self.assertNotEqual(img4.content, rf4.url) self.assertEqual(img4.url_for_access, rf4.url) self.assertEqual(img4.url_for_access.endswith(img4.content), True)
def test_save_and_retreive(self): rf = RawFile() rf.file = self.uploadFile('test.png') rf.save() saved = RawFile.objects.first() self.assertEqual(saved, rf) self.assertEqual(saved.file, rf.file)
def test_transpose(self): rf = RawFile() rf.file = self.uploadFile('test_transpose.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) timestamp = img.timestamp lonLat = img.lonLat self.assertNotEqual(lonLat, None) self.assertNotEqual(timestamp, None)
def test_url_ext_property(self): rf = RawFile() rf.file = self.uploadFile('test.png') rf.save() saved = RawFile.objects.first() file_url = rf.file.url self.assertEqual(rf.url, '%s%s' % (SERVER_HOST, file_url)) self.assertEqual(rf.url, saved.url) self.assertEqual(rf.ext, 'png') self.assertEqual(rf.ext, saved.ext)
def test_file_property(self): rf = RawFile() rf.file = self.uploadFile('test.png') rf.save() saved = RawFile.objects.first() url = rf.url regex = re_compile(r'^.*rfs/\d{4}/\d{1,2}/\d{1,2}/.+$') self.assertNotEqual(regex.match(url), None) self.assertEqual(saved, rf) self.assertEqual(saved.uuid, rf.uuid) self.assertEqual(saved.file, rf.file)
def setUp(self): super(RawFileViewsetTest, self).setUp() response = self.client.post('/users/register/') self.auth_user_token = json_loads(response.content)['auth_user_token'] self.client.post('/users/login/', {'auth_user_token': self.auth_user_token}) response = self.client.post('/vds/register/', dict(email='*****@*****.**')) self.auth_vd_token = json_loads(response.content)['auth_vd_token'] self.client.post('/vds/login/', {'auth_vd_token': self.auth_vd_token}) self.vd = VD.objects.first() self.rf = RawFile() self.rf.file = self.uploadFile('test.png') self.rf.vd = self.vd self.rf.save()
def test_exif_timestamp(self): exif = exif_lib.get_exif_data(PIL_Image.open('image/samples/gps_test.jpg')) timestamp = exif_lib.get_timestamp(exif) self.assertEqual(timestamp, 1459149534000) rf = RawFile() rf.file = self.uploadFile('gps_test.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) saved = Image.objects.first() self.assertEqual(img.timestamp, timestamp) self.assertEqual(saved.timestamp, timestamp)
def test_dhash(self): rf = RawFile() rf.file = self.uploadFile('test.jpg') rf.save() rf2 = RawFile() rf2.file = self.uploadFile('test_480.jpg') rf2.save() img, is_created = Image.get_or_create_smart(rf.url) img2, is_created = Image.get_or_create_smart(rf2.url) self.assertNotEqual(img.dhash, None) self.assertNotEqual(img2.dhash, None) self.assertEqual(img.dhash, img2.dhash)
def test_exif_gps(self): exif = exif_lib.get_exif_data(PIL_Image.open('image/samples/gps_test.jpg')) lonLat = exif_lib.get_lon_lat(exif) point = GEOSGeometry('POINT(%f %f)' % lonLat, srid=4326) self.assertEqual(point.x, 127.103744) # lon(경도) self.assertEqual(point.y, 37.399731) # lat(위도) rf = RawFile() rf.file = self.uploadFile('gps_test.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) saved = Image.objects.first() self.assertEqual(img.lonLat, point) self.assertEqual(saved.lonLat, point)
def test_rf_task(self): rf = RawFile() rf.file = self.uploadFile('test.png') rf.save() rf.task() rf = RawFile.objects.get(id=rf.id) self.assertEqual(rf.mhash, UUID('5abd147d-ceb8-218a-a160-1c7821db6654'))
def test_no_exif(self): exif = exif_lib.get_exif_data(PIL_Image.open('image/samples/no_exif_test.jpg')) lonLat = exif_lib.get_lon_lat(exif) timestamp = exif_lib.get_timestamp(exif) self.assertIsNone(lonLat[0]) self.assertIsNone(lonLat[1]) self.assertIsNone(timestamp) rf = RawFile() rf.file = self.uploadFile('no_exif_test.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) saved = Image.objects.first() self.assertEqual(img.lonLat, None) self.assertEqual(img.timestamp, None) self.assertEqual(saved, img) self.assertEqual(saved.lonLat, None) self.assertEqual(saved.timestamp, None)
def test_vd_property(self): vd = VD(); vd.save() rf = RawFile() rf.file = self.uploadFile('test.png') rf.vd = vd rf.save() saved = RawFile.objects.first() self.assertNotEqual(rf.vd, None) self.assertEqual(saved, rf) self.assertEqual(saved.vd, rf.vd)
class RawFileViewsetTest(APITestBase): def setUp(self): super(RawFileViewsetTest, self).setUp() response = self.client.post('/users/register/') self.auth_user_token = json_loads(response.content)['auth_user_token'] self.client.post('/users/login/', {'auth_user_token': self.auth_user_token}) response = self.client.post('/vds/register/', dict(email='*****@*****.**')) self.auth_vd_token = json_loads(response.content)['auth_vd_token'] self.client.post('/vds/login/', {'auth_vd_token': self.auth_vd_token}) self.vd = VD.objects.first() self.rf = RawFile() self.rf.file = self.uploadFile('test.png') self.rf.vd = self.vd self.rf.save() def test_list(self): response = self.client.get('/rfs/') self.assertEqual(response.status_code, status.HTTP_200_OK) results = json_loads(response.content)['results'] self.assertEqual(type(results), list) self.assertEqual(len(results), 1) def test_detail(self): response = self.client.get('/rfs/%s/' % self.rf.id) self.assertEqual(response.status_code, status.HTTP_200_OK) result = json_loads(response.content) self.assertEqual(type(result), dict) self.assertIn('uuid', result) self.assertIn('vd', result) #self.assertNotIn('file', result) self.assertNotIn('id', result) self.assertNotIn('phash', result) self.assertNotIn('mhash', result) self.assertEqual(result['uuid'], self.rf.uuid) self.assertEqual(result['vd'], self.vd.id) self.assertIn('url', result) url = result['url'] self.assertEqual(url.startswith(SERVER_HOST), True) r = requests_get(url) self.assertEqual(r.status_code, status.HTTP_200_OK) def test_create(self): self.assertEqual(RawFile.objects.count(), 1) self.assertVdLogin() self.assertNotEqual(self.vd_id, None) with open('image/samples/test.png') as f: response = self.client.post('/rfs/', dict(file=f)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(RawFile.objects.count(), 2) result = json_loads(response.content) self.assertEqual(result['vd'], self.vd.id) def test_create_with_token_authentication(self): self.logout() self.assertEqual(RawFile.objects.count(), 1) self.assertNotLogin() self.assertVdNotLogin() self.assertEqual(self.vd_id, None) with open('image/samples/test.png') as f: response = self.client.post('/rfs/', dict(file=f, auth_user_token=self.auth_user_token, auth_vd_token=self.auth_vd_token)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(RawFile.objects.count(), 2) self.assertNotLogin() self.assertVdNotLogin() self.assertEqual(self.vd_id, None) def _skip_test_create_with_kor_file_name(self): self.assertEqual(RawFile.objects.count(), 1) self.assertVdLogin() self.assertNotEqual(self.vd_id, None) with open('image/samples/한글.png') as f: response = self.client.post('/rfs/', dict(file=f)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(RawFile.objects.count(), 2) result = json_loads(response.content) self.assertEqual(result['vd'], self.vd.id)
def test_task(self): rf = RawFile() rf.file = self.uploadFile('test.png') rf.save() rf.task() rf = RawFile.objects.get(id=rf.id) saved = RawFile.objects.first() self.assertEqual(rf, saved) self.assertNotEqual(rf.mhash, None) self.assertEqual(rf.same, None) self.assertEqual(saved.mhash, rf.mhash) self.assertEqual(saved.same, rf.same) rf2 = RawFile() rf2.file = self.uploadFile('test.jpg') rf2.save() rf2.task() rf2 = RawFile.objects.get(id=rf2.id) self.assertEqual(rf.same, None) self.assertEqual(rf2.same, None) rf3 = RawFile() rf3.file = self.uploadFile('test.png') rf3.save() rf3_old_file_path = rf3.file.path rf3.task() rf3 = RawFile.objects.get(id=rf3.id) self.assertEqual(rf3.file.path, rf3_old_file_path) self.assertEqual(rf.same, None) self.assertEqual(rf2.same, None) self.assertEqual(rf3.same, rf) self.assertEqual(rf.sames.count(), 1) self.assertEqual(rf.sames.first(), rf3) f = Path(rf.file.path) f2 = Path(rf2.file.path) f3 = Path(rf3.file.path) self.assertEqual(f.is_symlink(), False) self.assertEqual(f2.is_symlink(), False) self.assertEqual(f3.is_symlink(), True) rf.task() rf2.task() f = Path(rf.file.path) f2 = Path(rf2.file.path) f3 = Path(rf3.file.path) self.assertEqual(f.is_symlink(), False) self.assertEqual(f2.is_symlink(), False) self.assertEqual(f3.is_symlink(), True) rf4 = RawFile() rf4.file = self.uploadFile('test_org.jpg') rf4.save() self.assertEqual(rf4.mhash, None) pil4_1 = PIL_Image.open(rf4.file.path) self.assertEqual(pil4_1.size, (4160, 2340)) rf4.task() self.assertEqual(rf4.mhash, UUID('f34bb180-a108-e20f-4167-2b734c497f0f')) pil4_2 = PIL_Image.open(rf4.file.path) self.assertEqual(pil4_2.size, (1280, 720)) rf4.task() self.assertEqual(rf4.mhash, UUID('f34bb180-a108-e20f-4167-2b734c497f0f')) pil4_3 = PIL_Image.open(rf4.file.path) self.assertEqual(pil4_3.size, (1280, 720))
def test_task(self): _rf = RawFile() _rf.file = self.uploadFile('no_exif_test.jpg') _rf.save() img, is_created = Image.get_or_create_smart(_rf.url) self.assertEqual(Image.objects.count(), 1) saved = Image.objects.first() img.task() self.assertEqual(img.similar, None) saved.task() self.assertEqual(saved.phash, img.phash) self.assertEqual(saved.dhash, img.dhash) self.assertEqual(saved.similar, img.similar) _rf2 = RawFile() _rf2.file = self.uploadFile('test.jpg') _rf2.save() img2, is_created = Image.get_or_create_smart(_rf2.url) img2.task() self.assertEqual(img.similar, None) self.assertEqual(img2.similar, None) _rf3 = RawFile() _rf3.file = self.uploadFile('test_480.jpg') _rf3.save() img3, is_created = Image.get_or_create_smart(_rf3.url) img3.lonLat = None img3.timestamp = None #img3.save() self.assertEqual(img3.lonLat, None) self.assertEqual(img3.timestamp, None) img3.task() self.assertEqual(img.similar, None) self.assertEqual(img2.similar, None) self.assertEqual(img3.similar, img2) self.assertEqual(img2.similars.count(), 1) self.assertEqual(img2.similars.first(), img3) self.assertEqual(img3.lonLat, img2.lonLat) self.assertEqual(img3.timestamp, img2.timestamp)
def test_string_representation(self): rf_id = uuid1() rf = RawFile(id=rf_id) self.assertEqual(unicode(rf), '%s.rf' % b16encode(rf_id.bytes)) self.assertEqual(rf.uuid, unicode(rf))