File tree Expand file tree Collapse file tree 3 files changed +128
-0
lines changed Expand file tree Collapse file tree 3 files changed +128
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+
3+ from operations import (
4+ init ,
5+ download ,
6+ upload ,
7+ abandon ,
8+ LOWER_LIMIT ,
9+ UPPER_LIMIT
10+ )
11+
12+ if __name__ == "__main__" :
13+
14+ # how i want to use the tool:
15+ # python3 cli.py init --config /path/to/config.ini
16+ # python3 cli.py download --num-images 40
17+ # python3 cli.py upload
18+ # python3 cli.py abandon
19+ parser = argparse .ArgumentParser ()
20+
21+ parser .add_argument (
22+ 'operation' ,
23+ choices = ['init' , 'download' , 'upload' , 'abandon' ]
24+ )
25+
26+ parser .add_argument ('-n' , '--num-images' , type = int )
27+
28+ parser .add_argument ('-c' , '--config' )
29+
30+ args = parser .parse_args ()
31+
32+ operation = args .operation
33+
34+ if operation == 'init' :
35+ init (args .config )
36+ elif operation == 'download' :
37+ download (args .num_images )
38+ elif operation == 'upload' :
39+ upload ()
40+ else :
41+ abandon ()
Original file line number Diff line number Diff line change 1+ DEFAULT_NUM_IMAGES = 40
2+ LOWER_LIMIT = 0
3+ UPPER_LIMIT = 100
4+
5+
6+ class MissingConfigException (Exception ):
7+ pass
8+
9+
10+ class ImageLimitException (Exception ):
11+ pass
12+
13+
14+ def init (config ):
15+ if (config is None ):
16+ raise MissingConfigException ()
17+
18+ raise NotImplementedError
19+
20+
21+ def download (num_images ):
22+ images_to_download = num_images
23+
24+ if num_images is None :
25+ images_to_download = DEFAULT_NUM_IMAGES
26+
27+ if images_to_download <= LOWER_LIMIT or images_to_download > UPPER_LIMIT :
28+ raise ImageLimitException ()
29+
30+ return images_to_download
31+
32+
33+ def upload ():
34+ raise NotImplementedError ()
35+
36+
37+ def abandon ():
38+ raise NotImplementedError ()
Original file line number Diff line number Diff line change 1+ import unittest
2+ from operations import (
3+ init ,
4+ download ,
5+ upload ,
6+ abandon ,
7+ MissingConfigException ,
8+ ImageLimitException ,
9+ DEFAULT_NUM_IMAGES ,
10+ LOWER_LIMIT ,
11+ UPPER_LIMIT
12+ )
13+
14+
15+ class TestCLIOperations (unittest .TestCase ):
16+ def test_init (self ):
17+ with self .assertRaises (NotImplementedError ):
18+ init ("fakeconfig" )
19+
20+ def test_init_missing_config (self ):
21+ with self .assertRaises (MissingConfigException ):
22+ init (None )
23+
24+ def test_download_under_limit (self ):
25+ with self .assertRaises (ImageLimitException ):
26+ download (LOWER_LIMIT )
27+
28+ def test_download_over_limit (self ):
29+ with self .assertRaises (ImageLimitException ):
30+ download (UPPER_LIMIT + 1 )
31+
32+ def test_download_missing_image_count (self ):
33+ downloaded_image_count = download (None )
34+ self .assertEqual (DEFAULT_NUM_IMAGES , downloaded_image_count )
35+
36+ def test_download_with_image_count (self ):
37+ downloaded_image_count = download (10 )
38+ self .assertEqual (10 , downloaded_image_count )
39+
40+ def test_upload (self ):
41+ with self .assertRaises (NotImplementedError ):
42+ upload ()
43+
44+ def test_abandon (self ):
45+ with self .assertRaises (NotImplementedError ):
46+ abandon ()
47+
48+ if __name__ == '__main__' :
49+ unittest .main ()
You can’t perform that action at this time.
0 commit comments