File size: 3,365 Bytes
d21cb06 | 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 | import numpy as np
import os.path as osp
from unittest import TestCase
from datumaro.components.project import Project
from datumaro.util.command_targets import ProjectTarget, \
ImageTarget, SourceTarget
from datumaro.util.image import save_image
from datumaro.util.test_utils import TestDir
class CommandTargetsTest(TestCase):
def test_image_false_when_no_file(self):
target = ImageTarget()
status = target.test('somepath.jpg')
self.assertFalse(status)
def test_image_false_when_false(self):
with TestDir() as test_dir:
path = osp.join(test_dir, 'test.jpg')
with open(path, 'w+') as f:
f.write('qwerty123')
target = ImageTarget()
status = target.test(path)
self.assertFalse(status)
def test_image_true_when_true(self):
with TestDir() as test_dir:
path = osp.join(test_dir, 'test.jpg')
save_image(path, np.ones([10, 7, 3]))
target = ImageTarget()
status = target.test(path)
self.assertTrue(status)
def test_project_false_when_no_file(self):
target = ProjectTarget()
status = target.test('somepath.jpg')
self.assertFalse(status)
def test_project_false_when_no_name(self):
target = ProjectTarget(project=Project())
status = target.test('')
self.assertFalse(status)
def test_project_true_when_project_file(self):
with TestDir() as test_dir:
path = osp.join(test_dir, 'test.jpg')
Project().save(path)
target = ProjectTarget()
status = target.test(path)
self.assertTrue(status)
def test_project_true_when_project_name(self):
project_name = 'qwerty'
project = Project({
'project_name': project_name
})
target = ProjectTarget(project=project)
status = target.test(project_name)
self.assertTrue(status)
def test_project_false_when_not_project_name(self):
project_name = 'qwerty'
project = Project({
'project_name': project_name
})
target = ProjectTarget(project=project)
status = target.test(project_name + '123')
self.assertFalse(status)
def test_project_false_when_not_project_file(self):
with TestDir() as test_dir:
path = osp.join(test_dir, 'test.jpg')
with open(path, 'w+') as f:
f.write('wqererw')
target = ProjectTarget()
status = target.test(path)
self.assertFalse(status)
def test_source_false_when_no_project(self):
target = SourceTarget()
status = target.test('qwerty123')
self.assertFalse(status)
def test_source_true_when_source_exists(self):
source_name = 'qwerty'
project = Project()
project.add_source(source_name)
target = SourceTarget(project=project)
status = target.test(source_name)
self.assertTrue(status)
def test_source_false_when_source_doesnt_exist(self):
source_name = 'qwerty'
project = Project()
project.add_source(source_name)
target = SourceTarget(project=project)
status = target.test(source_name + '123')
self.assertFalse(status) |