Passed
Push — master ( 9dbca9...fe1892 )
by Siro Díaz
01:29
created

TestRandImgProvider.test_download_image_with_dirname()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 11

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 11
dl 12
loc 12
rs 9.85
c 0
b 0
f 0
cc 1
nop 1
1
import unittest
2
import os
3
from faker import Faker
4
from pyrandimg.randimg_provider import RandImgProvider
5
6
7
class TestRandImgProvider(unittest.TestCase):
8
    def setUp(self):
9
        """ Initialize each unit test """
10
        self.faker = Faker()
11
        self.faker.add_provider(RandImgProvider)
12
13
    def test_generate_default_image_url(self):
14
        self.assertRegex(self.faker.image_url(), r'^(https?\:\/\/)?www\.rand\-img\.com\/720\/480$')
15
16
    def test_generate_image_url_with_custom_width(self):
17
        self.assertRegex(self.faker.image_url(200), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/480$')
18
        self.assertRegex(self.faker.image_url(980), r'^(https?\:\/\/)?www\.rand\-img\.com\/980\/480$')
19
20
    def test_generate_image_url_with_custom_width_and_height(self):
21
        self.assertRegex(self.faker.image_url(200, 500), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/500$')
22
        self.assertRegex(self.faker.image_url(320, 280), r'^(https?\:\/\/)?www\.rand\-img\.com\/320\/280$')
23
        self.assertRegex(self.faker.image_url(1920, 1080), r'(https?\:\/\/)?www\.rand\-img\.com\/1920\/1080')
24
25
    def test_generate_image_url_with_category(self):
26
        self.assertRegex(self.faker.image_url(200, 500, 'food'), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/500\/food$')
27
        self.assertRegex(self.faker.image_url(500, 653, 'sky'), r'^(https?\:\/\/)?www\.rand\-img\.com\/500\/653\/sky$')
28
29
    def test_generate_rand_image_url(self):
30
        self.assertRegex(
31
            self.faker.squared_image_url(320, rand=True),
32
            r'^(https?\:\/\/)?www\.rand\-img\.com\/320\/320\?rand=\d+$'
33
        )
34
35
    def test_generate_default_squared_image_url(self):
36
        self.assertRegex(self.faker.squared_image_url(200), r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/200$')
37
38
    def test_generate_squared_image_url_with_category(self):
39
        self.assertRegex(
40
            self.faker.squared_image_url(200, 'food'),
41
            r'^(https?\:\/\/)?www\.rand\-img\.com\/200\/200\/food$'
42
        )
43
        self.assertRegex(
44
            self.faker.squared_image_url(1024, 'sky'),
45
            r'^(https?\:\/\/)?www\.rand\-img\.com\/1024\/1024\/sky$'
46
        )
47
48
    def test_image_url_with_parameters(self):
49
        self.assertRegex(
50
            self.faker.image_url(720, 480, 'food', rand=True, blur=4, gray=1),
51
            r'https?:\/\/www\.rand\-img\.com\/720\/480/food\?([(\&?rand\=\d+)(\&?blur\=4)(\&?gray\=1)])'
52
        )
53
        self.assertRegex(
54
            self.faker.image_url(250, 120, 'fashion', rand=False, blur=4, gray=1),
55
            r'https?:\/\/www\.rand\-img\.com\/250\/120/fashion\?([(\&?blur\=4)(\&?gray\=1)])'
56
        )
57
58
    def test_get_gif(self):
59
        self.assertRegex(self.faker.gif_url(), r'^(https?\:\/\/)?www\.rand\-img\.com\/gif$')
60
61
    def test_get_gif_with_rand(self):
62
        self.assertRegex(self.faker.gif_url(True), r'^(https?\:\/\/)?www\.rand\-img\.com\/gif\?rand\=\d+$')
63
64
    def test_download_gif(self):
65
        gif = self.faker.gif()
66
        gif_dirname = os.path.dirname(gif)
67
        _, file_ext = os.path.splitext(gif)
68
69
        self.assertEqual('.gif', file_ext)
70
        self.assertTrue(os.path.exists(gif))
71
        os.remove(gif)
72
73
    def test_download_gif_with_dirname(self):
74
        os.mkdir('output')
75
        output_dir = os.path.join(os.getcwd(), 'output')
76
        gif = self.faker.gif(dir=output_dir)
77
        gif_dirname = os.path.dirname(gif)
78
        _, file_ext = os.path.splitext(gif)
79
80
        self.assertEqual(output_dir, gif_dirname)
81
        self.assertEqual('.gif', file_ext)
82
        self.assertTrue(os.path.exists(gif))
83
        os.remove(gif)
84
        os.rmdir(output_dir)
85
86
    def test_download_gif_with_invalid_argument_exception(self):
87
        pass
88
89
    def test_download_image(self):
90
        gif = self.faker.gif()
91
        gif_dirname = os.path.dirname(gif)
92
        _, file_ext = os.path.splitext(gif)
93
94
        self.assertEqual('.gif', file_ext)
95
        self.assertTrue(os.path.exists(gif))
96
        os.remove(gif)
97
98 View Code Duplication
    def test_download_image_with_dirname(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
99
        os.mkdir('output')
100
        output_dir = os.path.join(os.getcwd(), 'output')
101
        image = self.faker.image(dir=output_dir)
102
        image_dirname = os.path.dirname(image)
103
        _, file_ext = os.path.splitext(image)
104
105
        self.assertEqual(output_dir, image_dirname)
106
        self.assertEqual('.jpg', file_ext)
107
        self.assertTrue(os.path.exists(image))
108
        os.remove(image)
109
        os.rmdir(output_dir)
110
111 View Code Duplication
    def test_download_image_with_dirname_and_filename(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
112
        os.mkdir('output')
113
        output_dir = os.path.join(os.getcwd(), 'output')
114
        image = self.faker.image(dir=output_dir, filename='testing.png')
115
        image_dirname = os.path.dirname(image)
116
        _, file_ext = os.path.splitext(image)
117
118
        self.assertEqual(output_dir, image_dirname)
119
        self.assertEqual('.png', file_ext)
120
        self.assertTrue(os.path.exists(image))
121
        os.remove(image)
122
        os.rmdir(output_dir)
123
124
    def test_download_image_with_exception(self):
125
        pass
126
127
    def test_download_image_with_invalid_argument_exception(self):
128
        pass
129