-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_models.py
131 lines (107 loc) · 5.46 KB
/
test_models.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
import os
from unittest import TestCase
from rsconnect.models import AppMode, AppModes, GlobMatcher
class TestModels(TestCase):
def test_app_mode_class(self):
mode = AppMode(1, "test", "Testing")
self.assertEqual(mode.ordinal(), 1)
self.assertEqual(mode.name(), "test")
self.assertEqual(mode.desc(), "Testing")
self.assertIsNone(mode.extension())
self.assertEqual(mode.name(), str(mode))
self.assertEqual(mode.desc(), repr(mode))
mode = AppMode(2, "test-2", "Testing (again)", ".ipynb")
self.assertEqual(mode.ordinal(), 2)
self.assertEqual(mode.name(), "test-2")
self.assertEqual(mode.desc(), "Testing (again)")
self.assertEqual(mode.extension(), ".ipynb")
def test_app_modes_constants(self):
defined = list(filter(lambda n: n.isupper(), AppModes.__dict__.keys()))
modes = list(AppModes._modes)
ordinals = []
names = []
descriptions = []
extensions = []
self.assertEqual(len(modes), len(defined))
# This makes sure all named mode constants appear in the modes list.
for name in defined:
named_mode = AppModes.__dict__.get(name)
self.assertIn(named_mode, modes)
# The stuff here makes sure that each mode defined in the modes list is unique.
for mode in modes:
self.assertNotIn(mode.ordinal(), ordinals)
ordinals.append(mode.ordinal())
self.assertNotIn(mode.name(), names)
ordinals.append(mode.name())
self.assertNotIn(mode.desc(), descriptions)
ordinals.append(mode.desc())
if mode.extension() is not None:
self.assertNotIn(mode.extension(), extensions)
ordinals.append(mode.extension())
def test_get_by_ordinal(self):
self.assertIs(AppModes.get_by_ordinal(1), AppModes.SHINY)
self.assertIs(AppModes.get_by_ordinal(-1, True), AppModes.UNKNOWN)
self.assertIs(AppModes.get_by_ordinal(None, True), AppModes.UNKNOWN)
with self.assertRaises(ValueError):
AppModes.get_by_ordinal(-1)
with self.assertRaises(ValueError):
AppModes.get_by_ordinal(None)
def test_get_by_name(self):
self.assertIs(AppModes.get_by_name("shiny"), AppModes.SHINY)
self.assertIs(AppModes.get_by_name("bad-name", True), AppModes.UNKNOWN)
self.assertIs(AppModes.get_by_name(None, True), AppModes.UNKNOWN)
with self.assertRaises(ValueError):
AppModes.get_by_name("bad-name")
with self.assertRaises(ValueError):
AppModes.get_by_name(None)
def test_get_by_extension(self):
self.assertIs(AppModes.get_by_extension(".R"), AppModes.SHINY)
self.assertIs(AppModes.get_by_extension(".bad-ext", True), AppModes.UNKNOWN)
self.assertIs(AppModes.get_by_extension(None, True), AppModes.UNKNOWN)
with self.assertRaises(ValueError):
AppModes.get_by_extension(".bad-ext")
with self.assertRaises(ValueError):
AppModes.get_by_extension(None)
def test_glob_matcher(self):
cases = [
("dir", "dir", True),
("dir", "file", False),
("*.txt", "file.txt", True),
("*.txt", "file.csv", False),
("dir", "dir/file", False),
("dir/*", "file", False),
("dir/*", "dir/file", True),
("dir/*", "dir/sub/file", False),
("dir/*.txt", "file", False),
("dir/*.txt", "dir/file", False),
("dir/*.txt", "dir/file.txt", True),
("dir/*.txt", "dir/.txt", True),
# recursive wildcard pattern using "/" (input paths using OS separator)
("dir/**/*", "dirfile.txt", False),
("dir/**/*", os.path.join("dirother", "a.txt"), False),
("dir/**/*", os.path.join("dir", "a.txt"), True),
("dir/**/*", os.path.join("dir", "sub", "a.txt"), True),
("dir/**/*.txt", os.path.join("dirother", "a.txt"), False),
("dir/**/*.txt", os.path.join("dir", "a.txt"), True),
("dir/**/*.txt", os.path.join("dir", "a.csv"), False),
("dir/**/*.txt", os.path.join("dir", "sub", "a.txt"), True),
("dir/**/*.txt", os.path.join("dir", "sub", "a.csv"), False),
# recursive wildcards using OS path separator.
(os.path.join("dir", "**", "*.txt"), os.path.join("dir", "a.txt"), True),
(os.path.join("dir", "**", "*.txt"), os.path.join("dir", "sub", "a.txt"), True),
(os.path.join("dir", "**", "*.txt"), os.path.join("dir", "sub", "sub", "a.txt"), True),
(os.path.join("dir", "**", "*.txt"), os.path.join("dir", "sub", "sub", "a.obj"), False),
(os.path.join("dir", "**", "*"), os.path.join("dir", "sub", "sub", "sub", "a.txt"), True),
(os.path.join("dir", "**", "*"), os.path.join("dir", "sub", "sub", "a.bob"), True),
(os.path.join("dir", "**", "*"), os.path.join("dir", "sub", "z.o"), True),
(os.path.join("dir", "**", "*"), os.path.join("dir", "abc"), True),
]
for pattern, path, expected in cases:
matcher = GlobMatcher(pattern)
self.assertEqual(
matcher.matches(path),
expected,
f"pattern: {pattern}; path: {path}; expected: {expected}",
)
with self.assertRaises(ValueError):
GlobMatcher(os.path.join(".", "blah", "**", "blah", "**", "*.txt"))