Python 代码正在运行,但断言函数不起作用

时间:2021-04-04 23:58:14

标签: python python-unittest assert

我正在创建一个代码来将日期格式从“dd/mm/yyyy”转换为“日月年”。 如果提供的日期有误,该函数应删除多余的空格并返回 ""。 代码运行良好,但没有一个断言函数不起作用。它不断给我错误消息,尽管当我使用函数 convDate 时它运行良好。

我的代码是:

monthName = ['janvier', 'fevrier', 'mars',        
             'avril','mai', 'juin', 'juillet',
             'aout', 'septembre', 'octobre',
             'novembre', 'decembre']

def month():                                      
    global monthNumber
    monthNumber = []
    for i in range(1, 13):
        if i < 10:
            monthNumber.append("0" + str(i))     
        else:                                   
            monthNumber.append(str(i))
    return monthNumber

def cleaningSpaces():                          
    global splitDate                            
    splitDateSpace = splitDate                  
    splitDate = []
    for i in splitDateSpace:
        if i != "":
            splitDate.append(i)

def checkDateFormat1(date):                    
    global splitDate                          
    if date.find("/") != -1:
        date = date.replace(" ", "")          
        splitDate = date.split("/")
        cleaningSpaces()

        if splitDate[1].isdecimal():
            if len(splitDate[1]) == 1:
                splitDate[1] = "0" + splitDate[1]
                checkDateFormat(date)
        else:
            return ""
    else:
        checkDateFormat2(date)

def checkDateFormat2(date):
    global splitDate
    if date.find("/") == -1:
        splitDate = date.split(" ")
        cleaningSpaces()
        if date.isdecimal():
            return ""
        splitDate[1] = splitDate[1].lower()
        checkDateFormat(date)
    else:
        return ""

def checkDateFormat(date):
    global splitDate
    if True:
        if len(splitDate) != 3:
            return ""
        if len(splitDate[0]) == 1:
            splitDate[0] = "0" + splitDate[0]
        if len(splitDate[2]) != 4:
            return ""
        if splitDate[0].isalpha() | splitDate[2].isalpha():
            return ""
        if int(splitDate[0]) < 1 or int(splitDate[2]) < 1:
            return ""
        getMonthIndex(splitDate)
    else:
        return ""

def getMonthIndex(splitDate):
    global index
    index = -1
    month()
    search_index = False
    while not search_index:
        for i in range(len(monthNumber)):
            if monthNumber[i] == splitDate[1]:
                index = i
                splitDate[1] = monthName[index]
                search_index = True
        break
    while not search_index:
        for i in range(len(monthName)):
            if monthName[i] == splitDate[1]:
                index = i
                splitDate[1] = monthNumber[index]
                search_index = True
        break
    if index == -1:
        return ""
    else:
        validateDate(splitDate)

def validateDate(splitDate):
    indexMonth30 = [3, 5, 8, 10]
    indexMonth31 = [0, 2, 4, 6, 7, 9, 11]
    for i in indexMonth30:
        if index == int(i) and int(splitDate[0])>30:
            return ""
    for i in indexMonth31:
        if index == int(i) and int(splitDate[0])>31:
            return ""
    if index == 1 and int(splitDate[0]) > 29:
        return ""
    if index == 1 and int(splitDate[0]) > 28 and int(splitDate[2]) % 4 != 0:
        return ""
    else:
        joinDate(splitDate)

def joinDate(splitDate):
    if splitDate[1].isalpha():
        finalDate = " ".join(splitDate)
    else:
        finalDate= "/".join(splitDate)
    print(finalDate)
    return finalDate

def convDate(date):
    checkDateFormat1(date)

def convDateTest():
    assert convDate("  4 SEptembre 2006     ") == "04/09/2006"
    assert convDate("  4 //9  //  2000     ") == "04 septembre 2000"
    assert convDate("  29  fevrier 2000 ") == "29/02/2000"
    assert convDate("29 /2/ 2021") == ""
    assert convDate("31/4/ 2021") == ""
    assert convDate("-1/4/ 2021") == ""
    assert convDate("31/4/ 4/2021") == ""
    assert convDate("31/4/ 4/20210") == ""
    assert convDate("trois /4/ 4/20210") == ""
    assert convDate("trois /4/2021") == ""
    assert convDate("3 /14 /2021") == ""

convDateTest()

我知道我的代码很长而且不专业,但我仍在学习并且我喜欢它的逻辑。

1 个答案:

答案 0 :(得分:0)

好吧,你可能想试试这个

def convDate(date):
    return checkDateFormat1(date)

因此,无论 convDate 将返回任何类型的字符串,都将通过断言进行评估。此外,您可能希望在其他函数的块中的结束逻辑中添加 return 语句,似乎每次都会返回一个空字符串。

相关问题