Skip to content

Commit

Permalink
AutoTest: 新增DownloadTest, ContentTest
Browse files Browse the repository at this point in the history
  • Loading branch information
haarlemmer committed Apr 20, 2024
1 parent 2aaa546 commit db83458
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dmypy.json

# idea
.idea/

# for AutoTest
TestData/
9 changes: 9 additions & 0 deletions testscripts/testmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
fileList = tests.parseTest(shareUrl, sharePass, expectedFolder)
testPrint('TestMain', 'info', '')
testPrint('TestMain', 'info', 'ParseTest Complete!')

testPrint('TestMain', 'info', 'Starting DownloadTest...')
tests.downloadTest(fileList)
testPrint('TestMain', 'info', 'DownloadTest Complete!')

testPrint('TestMain', 'info', 'Starting ContentTest...')
tests.contentTest(expectedFileContents)
testPrint('TestMain', 'info', 'ContentTest Complete!')

testPrint('TestMain', 'info', 'Test Complete!')
53 changes: 53 additions & 0 deletions testscripts/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from ctparse.CTFile import CTFileShare
from utils.logging import testPrint
from utils.models import CTFile
import os, time, urllib.request

# ParseTest Start

def parseTest(link, passwd, expectedFolder):
ct = CTFileShare(link, passwd)
Expand Down Expand Up @@ -40,4 +42,55 @@ def proccessRetFolder(retFolder):
items.append(proccessRetFolder(retItem))
return ['Folder', retFolder[1], items]

# ParseTest End

# DownloadTest Start

def downloadTest(fileList):
try:
os.mkdir('TestData')
except FileExistsError:
pass
links = getDownloadLinkForFolder(fileList)
testPrint('DownloadTest', 'debug', f'Links: {links}')
for fileName in list(links.keys()):
testPrint('DownloadTest', 'info', f'Downloading {fileName} to TestData/{fileName}')
urllib.request.urlretrieve(links[fileName], f'TestData/{fileName}')


def getDownloadLinkForFolder(fileList):
testPrint('DownloadTest', 'debug', f'Get folder: {fileList}')
links = {}
for file in fileList[2]:
if type(file) == CTFile:
fileName = file.name
testPrint('DownloadTest', 'info', f'Get download link: {fileName}')
link = file.genDownloadLink()
if link == None: # 出问题了就消停会儿再试一次
time.sleep(1)
link = file.genDownloadLink()
links[fileName] = link.fileDownloadLink
testPrint('DownloadTest', 'info', f'Download Link: {links[fileName]}')
elif type(file) == list:
nxtLinks = getDownloadLinkForFolder(file)
for key in list(nxtLinks.keys()):
links[key] = nxtLinks[key]
time.sleep(0.25)
return links

# DownloadTest End

# ContentTest Start
def contentTest(expectedContents):
for fileName in list(expectedContents.keys()):
testPrint('ContentTest', 'info', f'Check file: {fileName}')
f = open(f'TestData/{fileName}')
content = f.read()
if content[-1:] == '\n':
content = content[:-1] # 检测到结尾的换行符就去掉,避免干扰
if content == expectedContents[fileName]:
testPrint('ContentTest', 'info', f'Check PASS: {expectedContents[fileName]} -> {content}')
else:
testPrint('ContentTest', 'err', f'Check FAIL: {expectedContents[fileName]} -> {content}')
raise ValueError

0 comments on commit db83458

Please sign in to comment.