27
27
noid -n/--index
28
28
29
29
"""
30
+ import io
31
+ import os
32
+ import pathlib
30
33
import random
31
34
import re
35
+ import sys
36
+ import tempfile
32
37
import unittest
33
38
34
39
from noid import cli , pynoid , utils
35
40
36
- import pathlib
37
-
38
41
BASE_DIR = pathlib .Path (__file__ ).parent .parent
39
42
CONFIG_FILE = BASE_DIR / 'noid' / 'noid.cfg'
40
43
@@ -53,7 +56,16 @@ def test_default(self):
53
56
54
57
def test_validate (self ):
55
58
"""Validation requires noid positional argument"""
56
- self .assertIsNone (cli .cli (f"noid -v" ))
59
+ self .assertIsNone (cli .cli (f"noid -V" ))
60
+
61
+ def test_check_digit (self ):
62
+ """Show the check digit for the given string"""
63
+ args = cli .cli (f"noid -d 123456" )
64
+ self .assertEqual ('123456' , args .noid )
65
+ self .assertTrue (args .check_digit )
66
+ # you cannot validate and check at the same time
67
+ with self .assertRaises (SystemExit ):
68
+ cli .cli (f"noid -d -V 123456" )
57
69
58
70
def test_config (self ):
59
71
"""Using a config file"""
@@ -66,6 +78,37 @@ def test_config(self):
66
78
self .assertEqual ('zeeeeddddk' , args .template )
67
79
self .assertEqual (- 1 , args .index )
68
80
self .assertFalse (args .verbose )
81
+ sys .stdout = io .StringIO ()
82
+ configs = cli .read_configs (args )
83
+ print (configs )
84
+ self .assertRegex (sys .stdout .getvalue (), r"(?ms:.*[[]noid[]].*template.*scheme.*naa.*)" )
85
+
86
+ def test_missing_noid_validate (self ):
87
+ """Print error for validating a blank noid"""
88
+ args = cli .cli (f"noid -V" )
89
+ self .assertIsNone (args )
90
+
91
+ def test_invalid_configs (self ):
92
+ """Use defaults when configs invalid"""
93
+ # case 1: no 'noid' section
94
+ _configs = """[noids]\n template = zeedddeeek\n scheme = doi:\n naa = 1234\n """
95
+ temp_configs = tempfile .NamedTemporaryFile ()
96
+ with open (temp_configs .name , 'w' ) as f :
97
+ print (_configs , file = f )
98
+ sys .stdout = sys .stderr = io .StringIO ()
99
+ cli .cli (f"noid -c { temp_configs .name } " )
100
+ self .assertRegex (sys .stderr .getvalue (), r"(?ms:^warning: config file .* lacks 'noid' section.*ignoring.*)" )
101
+ # case 2: some sections missing
102
+ _configs = """[noid]\n templates = zeedddeeek\n schemes = doi:\n naat = 1234\n """
103
+ temp_configs = tempfile .NamedTemporaryFile ()
104
+ with open (temp_configs .name , 'w' ) as f :
105
+ print (_configs , file = f )
106
+ sys .stderr = io .StringIO ()
107
+ cli .cli (f"noid -c { temp_configs .name } " )
108
+ self .assertRegex (
109
+ sys .stderr .getvalue (),
110
+ r"(?ms:^warning: configs missing option 'template'.*missing option 'scheme'.*missing option 'naa'.*)"
111
+ )
69
112
70
113
71
114
class PynoidAPI (unittest .TestCase ):
@@ -125,6 +168,68 @@ def test_mint_invalid_template(self):
125
168
self .assertEqual ('' , noid )
126
169
127
170
171
+ class PynoidNoid (unittest .TestCase ):
172
+ """Tests for the entry point"""
173
+
174
+ def test_default (self ):
175
+ """The result of simply calling 'noid'"""
176
+ cli .cli (f"noid -v" )
177
+ sys .stdout = sys .stderr = io .StringIO ()
178
+ pynoid .main ()
179
+ self .assertRegex (sys .stdout .getvalue (), r"(?ms:^info: generating noid.*template=.*scheme=.*ark[:][/][\w\d]+)" )
180
+
181
+ def test_error (self ):
182
+ """Exit status on *nix is os.EX_USAGE"""
183
+ cli .cli (f"noid -V" )
184
+ ex = pynoid .main ()
185
+ self .assertEqual (os .EX_USAGE , ex )
186
+
187
+ def test_config (self ):
188
+ """Using config"""
189
+ cli .cli (f"noid -v -c { CONFIG_FILE } " )
190
+ sys .stdout = sys .stderr = io .StringIO ()
191
+ pynoid .main ()
192
+ self .assertRegex (
193
+ sys .stdout .getvalue (),
194
+ r"(?ms:^info: generating noid.*template=zeeeeddddk.*scheme="
195
+ r"http[:][/][/].*naa=83812.*http[:][/][/]83812[/][\w\d]+)"
196
+ )
197
+
198
+ def test_validate (self ):
199
+ """Using check digit"""
200
+ noid = 'xg64G'
201
+ cli .cli (f"noid -v -V { noid } " )
202
+ sys .stdout = sys .stderr = io .StringIO ()
203
+ pynoid .main ()
204
+ self .assertRegex (sys .stdout .getvalue (), rf"(?ms:^info: validating '{ noid } '.*'{ noid } ' valid[?] True)" )
205
+
206
+ def test_check_digit (self ):
207
+ """Using check digit"""
208
+ noid = '123456'
209
+ check_digit = pynoid .calculate_check_digit (noid )
210
+ cli .cli (f"noid -v -d { noid } " )
211
+ sys .stdout = sys .stderr = io .StringIO ()
212
+ pynoid .main ()
213
+ self .assertRegex (sys .stdout .getvalue (), rf"(?ms:^info: computing check digit for '{ noid } '.*{ check_digit } )" )
214
+
215
+ def test_scheme_naa_template (self ):
216
+ """Minting options"""
217
+ cli .cli (f"noid -v -t zeeddk -s https:// -N 54321" )
218
+ sys .stdout = sys .stderr = io .StringIO ()
219
+ pynoid .main ()
220
+ self .assertRegex (sys .stdout .getvalue (),
221
+ r"(?ms:^info: generating noid.*template=zeeddk.*scheme=https[:][/][/].*naa=54321.*https[:][/][/]54321[/][\w\d]+)" )
222
+
223
+ def test_index (self ):
224
+ """Set index"""
225
+ index = random .randint (1000 , 2000 )
226
+ cli .cli (f"noid -v -n { index } " )
227
+ sys .stdout = sys .stderr = io .StringIO ()
228
+ pynoid .main ()
229
+ self .assertRegex (sys .stdout .getvalue (),
230
+ rf"(?ms:^info: generating noid.*template=zeeddk.*n={ index } .*scheme=ark[:][/].*naa=.*ark[:][/][\w\d]+)" )
231
+
232
+
128
233
class PynoidUtils (unittest .TestCase ):
129
234
def test_validate_mask (self ):
130
235
"""Validate a mask"""
0 commit comments