1#!/usr/bin/python 2 3import os 4import re 5import shutil 6import unittest 7import uuid 8 9import napi.fs 10import napi.sandbox 11import napi.subtitles 12import napi.testcase 13 14class FormatsConversionTest(napi.testcase.NapiTestCase): 15 16 def _napiFormatConversion(self, fromFormat, toFormat): 17 media = None 18 with napi.sandbox.Sandbox() as sandbox: 19 # generate a media file 20 media = self.videoAssets.prepareRandomMedia(sandbox) 21 subs = self.subtitlesAssets.prepareRandomMedia(sandbox, 22 fromFormat) 23 24 # program napiprojekt mock 25 self.napiMock.programXmlRequest( 26 media, 27 napi.subtitles.CompressedSubtitles.fromFile( 28 media['asset'], 29 subs['path'])) 30 31 fs = napi.fs.Filesystem(media) 32 33 # get the subs 34 self.napiScan('-f', toFormat, media['path']) 35 36 # check assertions 37 req = self.napiMock.getRequest() 38 self.assertEquals(req.method, "POST") 39 self.assertEquals(req.url, '/api/api-napiprojekt3.php') 40 self.assertTrue(self.output.stdoutContains( 41 re.compile(r'napisy pobrano pomyslnie'))) 42 43 # confirm the format 44 self.assertTrue(fs.subtitlesExists()) 45 for s in fs.getSubtitlesPaths(): 46 self.subotageExecute('-gi', '-i', s) 47 self.assertTrue(self.output.stdoutContains( 48 re.compile(r'IN_FORMAT -> {}'.format(toFormat)))) 49 50 def _subotageFormatDetect(self, fmt): 51 with napi.sandbox.Sandbox() as sandbox: 52 # generate a subs file 53 subs = self.subtitlesAssets.prepareRandomMedia(sandbox, fmt) 54 self.subotageExecute('-gi', '-i', subs['path']) 55 self.assertTrue(self.output.stdoutContains( 56 re.compile(r'IN_FORMAT -> {}'.format(fmt)))) 57 58 def _subotageFormatConversion(self, fromFormat, toFormat): 59 with napi.sandbox.Sandbox() as sandbox: 60 # generate a subs file 61 subs = self.subtitlesAssets.prepareRandomMedia(sandbox, 62 fromFormat) 63 64 outputFile = os.path.join(sandbox.path, 'outputfile.txt') 65 66 # ... convert 67 self.subotageExecute('-i', subs['path'], 68 '-of', toFormat, 69 '-o', outputFile) 70 71 # if formats match, no conversion happened so, just check if the 72 # original file remains unchanged 73 if fromFormat == toFormat: 74 outputFile = subs['path'] 75 76 # ... verify 77 self.subotageExecute('-gi', '-i', outputFile) 78 self.assertTrue(self.output.stdoutContains( 79 re.compile(r'IN_FORMAT -> {}'.format(toFormat)))) 80 81 def _subotageMassFormatConversion(self, fromFormat, toFormat): 82 with napi.sandbox.Sandbox() as sandbox: 83 # generate a subs files 84 for subs in self.subtitlesAssets.prepareMediaRange(sandbox, 85 fromFormat): 86 87 self.logger.debug("Attempt to convert asset: " + str(subs)) 88 89 outputFile = os.path.join(sandbox.path, 90 uuid.uuid4().hex) 91 92 # ... convert 93 self.subotageExecute('-i', subs['path'], 94 '-of', toFormat, 95 '-o', outputFile) 96 97 # if formats match, no conversion happened so, just check if the 98 # original file remains unchanged 99 if fromFormat == toFormat: 100 outputFile = subs['path'] 101 102 # ... verify 103 self.subotageExecute('-gi', '-i', outputFile) 104 self.assertTrue(self.output.isSuccess()) 105 self.assertTrue(self.output.stdoutContains( 106 re.compile(r'IN_FORMAT -> {}'.format(toFormat)))) 107 108 def test_ifSubotageDetectsFormatsCorrectly(self): 109 """ 110 Brief: 111 Procedure: 112 Expected Results: 113 """ 114 formats = [ "microdvd", "mpl2", 115 "subrip", "subviewer2", "tmplayer" ] 116 for fmt in formats: 117 self._subotageFormatDetect(fmt) 118 119 def test_ifDownloadsAndConvertsToSubripFormat(self): 120 """ 121 Brief: Verify if the conversion to subrip format is being performed 122 Procedure: 123 1. Prepare a media asset 124 2. Prepare subtitles for the asset 125 3. Call napi with a request for media asset and a request for conversion 126 4. Verify subs presence 127 5. Verify subs format 128 129 Expected Results: 130 Subs format should be subrip 131 """ 132 formats = [ "microdvd", "mpl2", 133 "subrip", "subviewer2", "tmplayer" ] 134 for fmt in formats: 135 self._napiFormatConversion(fmt, 'subrip') 136 137 def test_ifDownloadsAndConvertsToMicrodvdFormat(self): 138 """ 139 Brief: Verify if the conversion to microdvd format is being performed 140 Procedure: 141 1. Prepare a media asset 142 2. Prepare subtitles for the asset 143 3. Call napi with a request for media asset and a request for conversion 144 4. Verify subs presence 145 5. Verify subs format 146 147 Expected Results: 148 Subs format should be microdvd 149 """ 150 formats = [ "microdvd", "mpl2", 151 "subrip", "subviewer2", "tmplayer" ] 152 for fmt in formats: 153 self._napiFormatConversion(fmt, 'microdvd') 154 155 def test_ifDownloadsAndConvertsToTmplayerFormat(self): 156 """ 157 Brief: Verify if the conversion to tmplayer format is being performed 158 Procedure: 159 1. Prepare a media asset 160 2. Prepare subtitles for the asset 161 3. Call napi with a request for media asset and a request for conversion 162 4. Verify subs presence 163 5. Verify subs format 164 165 Expected Results: 166 Subs format should be tmplayer 167 """ 168 formats = [ "microdvd", "mpl2", 169 "subrip", "subviewer2", "tmplayer" ] 170 for fmt in formats: 171 self._napiFormatConversion(fmt, 'tmplayer') 172 173 def test_ifDownloadsAndConvertsToSubviewer2Format(self): 174 """ 175 Brief: Verify if the conversion to subviewer2 format is being performed 176 Procedure: 177 1. Prepare a media asset 178 2. Prepare subtitles for the asset 179 3. Call napi with a request for media asset and a request for conversion 180 4. Verify subs presence 181 5. Verify subs format 182 183 Expected Results: 184 Subs format should be subviewer2 185 """ 186 formats = [ "microdvd", "mpl2", 187 "subrip", "subviewer2", "tmplayer" ] 188 for fmt in formats: 189 self._napiFormatConversion(fmt, 'subviewer2') 190 191 def test_ifDownloadsAndConvertsToMpl2Format(self): 192 """ 193 Brief: Verify if the conversion to mpl2 format is being performed 194 Procedure: 195 1. Prepare a media asset 196 2. Prepare subtitles for the asset 197 3. Call napi with a request for media asset and a request for conversion 198 4. Verify subs presence 199 5. Verify subs format 200 201 Expected Results: 202 Subs format should be mpl2 203 """ 204 formats = [ "microdvd", "mpl2", 205 "subrip", "subviewer2", "tmplayer" ] 206 for fmt in formats: 207 self._napiFormatConversion(fmt, 'mpl2') 208 209 def test_ifConvertsToSubripFormat(self): 210 """ 211 Brief: Verify if the conversion to subrip format is being performed 212 Procedure: 213 2. Prepare subtitles for the asset 214 3. Call subotage with a given subs and desired output format 215 4. Verify subs presence 216 5. Verify subs format 217 218 Expected Results: 219 Subs format should be subrip 220 """ 221 formats = [ "microdvd", "mpl2", 222 "subrip", "subviewer2", "tmplayer" ] 223 for fmt in formats: 224 self._subotageFormatConversion(fmt, 'subrip') 225 226 def test_ifConvertsToMicrodvdFormat(self): 227 """ 228 Brief: Verify if the conversion to microdvd format is being performed 229 Procedure: 230 2. Prepare subtitles for the asset 231 3. Call subotage with a given subs and desired output format 232 4. Verify subs presence 233 5. Verify subs format 234 235 Expected Results: 236 Subs format should be microdvd 237 """ 238 formats = [ "microdvd", "mpl2", 239 "subrip", "subviewer2", "tmplayer" ] 240 for fmt in formats: 241 self._subotageFormatConversion(fmt, 'microdvd') 242 243 def test_ifConvertsToTmplayerFormat(self): 244 """ 245 Brief: Verify if the conversion to tmplayer format is being performed 246 Procedure: 247 2. Prepare subtitles for the asset 248 3. Call subotage with a given subs and desired output format 249 4. Verify subs presence 250 5. Verify subs format 251 252 Expected Results: 253 Subs format should be tmplayer 254 """ 255 formats = [ "microdvd", "mpl2", 256 "subrip", "subviewer2", "tmplayer" ] 257 for fmt in formats: 258 self._subotageFormatConversion(fmt, 'tmplayer') 259 260 def test_ifConvertsToSubviewer2Format(self): 261 """ 262 Brief: Verify if the conversion to subviewer2 format is being performed 263 Procedure: 264 2. Prepare subtitles for the asset 265 3. Call subotage with a given subs and desired output format 266 4. Verify subs presence 267 5. Verify subs format 268 269 Expected Results: 270 Subs format should be subviewer2 271 """ 272 formats = [ "microdvd", "mpl2", 273 "subrip", "subviewer2", "tmplayer" ] 274 for fmt in formats: 275 self._subotageFormatConversion(fmt, 'subviewer2') 276 277 def test_ifConvertsToMpl2Format(self): 278 """ 279 Brief: Verify if the conversion to mpl2 format is being performed 280 Procedure: 281 2. Prepare subtitles for the asset 282 3. Call subotage with a given subs and desired output format 283 4. Verify subs presence 284 5. Verify subs format 285 286 Expected Results: 287 Subs format should be mpl2 288 """ 289 formats = [ "microdvd", "mpl2", 290 "subrip", "subviewer2", "tmplayer" ] 291 for fmt in formats: 292 self._subotageFormatConversion(fmt, 'mpl2') 293 294 @unittest.skipIf(bool(os.environ.get( 295 'NAPI_INTEGRATION_TESTS_LONG_ENABLED', 0)) == False, 296 "Long tests disabled") 297 def test_ifStressConversionWorksWithAllAssets(self): 298 """ 299 Brief: Iterate over all available subtitles of given format and attempt 300 conversion 301 Procedure: 302 1. Prepare a set of all available subtitles files of given type 303 2. Attempt conversion to all supported format 304 305 Expected results: 306 No conversion errors should be present at all times. 307 """ 308 formats = [ "microdvd", "mpl2", 309 "subrip", "subviewer2", "tmplayer" ] 310 311 for fromFormat in formats: 312 toFormats = formats[:] 313 toFormats.remove(fromFormat) 314 for toFormat in toFormats: 315 self._subotageMassFormatConversion(fromFormat, toFormat) 316 317if __name__ == '__main__': 318 napi.testcase.runTests() 319