Move test runs into the test-run folder, add TimestampStrings, add basic tests via pytest

pytest added for dev.
Move all the test run python scripts into the "test-run" folder, so we can use the tests/ folder for just tests.

Setup one test for the TimestampStrings class with pytests in tests/unit/string_handling/test_timestamp_strings.py
This commit is contained in:
Clemens Schwaighofer
2025-07-08 14:54:26 +09:00
parent cd07267475
commit 060e3b4afe
12 changed files with 291 additions and 7 deletions

View File

@@ -1,66 +0,0 @@
#!/usr/bin/env -S uv run --script
"""
Test for double byte format
"""
from corelibs.string_handling.double_byte_string_format import DoubleByteFormatString
def main():
"""
Main call
"""
string = [
"Some string 123 other text",
"Some string 日本語 other text",
"日本語は string 123 other text",
"あいうえおかきくけこさしすせそなにぬねのまみむめも〜",
"あいうえおかきくけこさしす 1 other text",
"Some string すせそなにぬねのまみむめも〜",
"SOME OTHER STRING THAT IS LONGER THAN TWENTYSIX CHARACTERS",
"日本語は string 123 other text Some string 日本語 other text"
]
format_str = "{{:<{len}}}"
length_set = [
(26, 25),
(26, 26),
(26, 60),
(26, 20),
(26, -5),
(-6, -5),
]
for _length_set in length_set:
cut_length = _length_set[0]
format_length = _length_set[1]
print(f"========= Cut: {cut_length} | Format: {format_length} ==> ")
for _string in string:
string_test = DoubleByteFormatString(_string, cut_length, format_length)
formated = format_str.format(
len=string_test.get_format_length()
).format(
string_test.get_string_short()
)
print(
"* Shorten string: shorten length: "
f"Req: {string_test.get_requested_cut_length()} ({cut_length}) / "
f"Set: {string_test.get_cut_length()}, "
"format length: "
f"Req: {string_test.get_requested_format_length()} ({format_length}) / "
f"Set: {string_test.get_format_length()}"
f"\nOrig: |{_string}|"
f"\nGSS : |{string_test.get_string_short()}|"
f"\nF : |{formated}|"
f"\nGSSF: |{string_test.get_string_short_formated()}|"
)
print("-------")
if __name__ == "__main__":
main()
# __END__

View File

@@ -1,26 +0,0 @@
"6275980340000110250","okusuribukuro@kza.biglobe.ne.jp","2260029","神奈川県","横浜市緑区森の台","","","御薬袋","","0459310853","448ウ"
"6275980340000110821","tanoshiku-nakayoku@s5.dion.ne.jp","2260026","神奈川県","横浜市緑区長津田町","","","稲垣","理佐","0459837010ウ","448"
"6275980340000111167","snow4136@da2.so-net.ne.jp","2220036","神奈川県","横浜市港北区小机町","","","吉田","幸子","0454765658ウ"","448"
"6275980340000111241","kaori-5.29@ka.tnc.ne.jp","4200947","静岡県","静岡市葵区堤町","","","矢口","香緒里","0542711840","448ウ""
"6275980340000111324","Taikoowanrd@yahoo.co.jp","2300077","神奈川県","横浜市鶴見区東寺尾","","","菊地","そのへ","045ウ"5715949","448"
"6275980340000111407","suzuki37@hotmail.com","2260016","神奈川県","横浜市緑区霧が丘","","","鈴木","三奈","0459223865","448"
"6275980340000111738","ajoa@bredband.net","2710067","千葉県","松戸市樋野口",""," \"  ","〓〓","","","448"
"6275980340000111993","g7-chiro@mbp.nifty.com","2220031","神奈川県","横浜市港北区太尾町","","","角田","郁代","0455482822","448"
"6275980340000112231","k_hama@w4.dion.ne.jp","2360052","神奈川県","横浜市金沢区富岡西","","","浜田","和久","0457754803","448"
"6275980340000113064",""reiko_hoshikawa"@hotmail.com","2220026","神奈川県","横浜市港北区","篠原町72-2","コンフォール篠原2-104","茂木","玲子","0454230900","448"
"6275980340000113148","ali06@nifty.com","1640011","東京都","中野区中央","","","齋藤","亞理","0333843247","448"
"6275980340125312757","satokich@hotmail.com","6740094","兵庫県","明石市","二見町西二見1981-1","レーウ"フロンスA202","來田","淳子","","486"
"6275980340079774754","m,a,n,a,mail@docomo.ne.jp","2630043","千葉県","千葉市稲毛区小仲台","","レクセル稲毛パークス702","横山","雅菜","","447"
"6275980340032948677","maya..mama.natsuki@docomo.ne.jp","2210863","神奈川県","横浜市神奈川区","羽沢町984","サンハイツ羽沢2-301","岡部","麻弥","","448"
"6275980340032951044","ino.0827.yok@docomo.ne.jp","2310847","神奈川県","横浜市中区","竹之丸55","グリーンヒル山手A-2","猪俣","葉子","0456638634","448"
"6275980340032952455","jigoro.ore@ezweb.ne.jp","2400007","神奈川県","横浜市保土ヶ谷区明神台","","","土屋","尚子","045-331-5524
test
","448"
"6275980340032954196","acchi0819-lucida@docomo.ne.jp","2260006","神奈川県","横浜市緑区鴨居","","","佐藤","","","448"
"6275980340032955185","y.t-vs-a.h@docomo.ne.jp","2510052","神奈川県","藤沢市藤沢","","ミサワホームズ藤沢1106号室","土岐","麻美","0466244345","448"
"6275980340076656582","yukko4513@ezweb.ne.jp","2200101","神奈川県","相模原市城山町","町屋4-24-24 ","","中野","幸子","0427825439
","448"
"6275980340125281648","k-tomomi-6.14@ezweb.ne.jp","6570024","兵庫県","神戸市灘区楠ヶ岡町","","高羽グリーンハイツ203","神吉","友美","
","486"
"6275980340059092649","motoyaworld@hotmail.com","2220031","神奈川県","横浜市港北区","太尾町 3445 ",""ェラ大倉山 205号室","佐藤","基哉","0455470266","448"
Can't render this file because it contains an unexpected character in line 3 and column 165.

File diff suppressed because it is too large Load Diff

View File

@@ -1,104 +0,0 @@
"6275980340000110250","okusuribukuro@kza.biglobe.ne.jp","2260029","神奈川県","横浜市緑区森の台","","","御薬袋","","0459310853","448ウ"
"6275980340000110821","tanoshiku-nakayoku@s5.dion.ne.jp","2260026","神奈川県","横浜市緑区長津田町","","","稲垣","理佐","0459837010ウ","448"
"6275980340000111167","snow4136@da2.so-net.ne.jp","2220036","神奈川県","横浜市港北区小机町","","","吉田","幸子","0454765658ウ"","448"
"6275980340000111241","kaori-5.29@ka.tnc.ne.jp","4200947","静岡県","静岡市葵区堤町","","","矢口","香緒里","0542711840","448ウ""
"6275980340000111324","Taikoowanrd@yahoo.co.jp","2300077","神奈川県","横浜市鶴見区東寺尾","","","菊地","そのへ","045ウ"5715949","448"
"6275980340000111407","suzuki37@hotmail.com","2260016","神奈川県","横浜市緑区霧が丘","","","鈴木","三奈","0459223865","448"
"6275980340000111738","ajoa@bredband.net","2710067","千葉県","松戸市樋野口",""," \"  ","〓〓","","","448"
"6275980340000111993","g7-chiro@mbp.nifty.com","2220031","神奈川県","横浜市港北区太尾町","","","角田","郁代","0455482822","448"
"6275980340000112231","k_hama@w4.dion.ne.jp","2360052","神奈川県","横浜市金沢区富岡西","","","浜田","和久","0457754803","448"
"6275980340000113064",""reiko_hoshikawa"@hotmail.com","2220026","神奈川県","横浜市港北区","篠原町72-2","コンフォール篠原2-104","茂木","玲子","0454230900","448"
"6275980340000113148","ali06@nifty.com","1640011","東京都","中野区中央","","","齋藤","亞理","0333843247","448"
"6275980340125312757","satokich@hotmail.com","6740094","兵庫県","明石市","二見町西二見1981-1","レーウ"フロンスA202","來田","淳子","","486"
"6275980340079774754","m,a,n,a,mail@docomo.ne.jp","2630043","千葉県","千葉市稲毛区小仲台","","レクセル稲毛パークス702","横山","雅菜","","447"
"6275980340032948677","maya..mama.natsuki@docomo.ne.jp","2210863","神奈川県","横浜市神奈川区","羽沢町984","サンハイツ羽沢2-301","岡部","麻弥","","448"
"6275980340032951044","ino.0827.yok@docomo.ne.jp","2310847","神奈川県","横浜市中区","竹之丸55","グリーンヒル山手A-2","猪俣","葉子","0456638634","448"
"6275980340032952455","jigoro.ore@ezweb.ne.jp","2400007","神奈川県","横浜市保土ヶ谷区明神台","","","土屋","尚子","045-331-5524
test
","448"
"6275980340032954196","acchi0819-lucida@docomo.ne.jp","2260006","神奈川県","横浜市緑区鴨居","","","佐藤","","","448"
"6275980340032955185","y.t-vs-a.h@docomo.ne.jp","2510052","神奈川県","藤沢市藤沢","","ミサワホームズ藤沢1106号室","土岐","麻美","0466244345","448"
"6275980340076656582","yukko4513@ezweb.ne.jp","2200101","神奈川県","相模原市城山町","町屋4-24-24 ","","中野","幸子","0427825439
","448"
"6275980340125281648","k-tomomi-6.14@ezweb.ne.jp","6570024","兵庫県","神戸市灘区楠ヶ岡町","","高羽グリーンハイツ203","神吉","友美","
","486"
"6275980340059092649","motoyaworld@hotmail.com","2220031","神奈川県","横浜市港北区","太尾町 3445 ",""ェラ大倉山 205号室","佐藤","基哉","0455470266","448"
"6275980340000110250","okusuribukuro@kza.biglobe.ne.jp","2260029","神奈川県","横浜市緑区森の台","","","御薬袋","","0459310853","448ウ"
"6275980340000110821","tanoshiku-nakayoku@s5.dion.ne.jp","2260026","神奈川県","横浜市緑区長津田町","","","稲垣","理佐","0459837010ウ","448"
"6275980340000111167","snow4136@da2.so-net.ne.jp","2220036","神奈川県","横浜市港北区小机町","","","吉田","幸子","0454765658ウ"","448"
"6275980340000111241","kaori-5.29@ka.tnc.ne.jp","4200947","静岡県","静岡市葵区堤町","","","矢口","香緒里","0542711840","448ウ""
"6275980340000111324","Taikoowanrd@yahoo.co.jp","2300077","神奈川県","横浜市鶴見区東寺尾","","","菊地","そのへ","045ウ"5715949","448"
"6275980340000111407","suzuki37@hotmail.com","2260016","神奈川県","横浜市緑区霧が丘","","","鈴木","三奈","0459223865","448"
"6275980340000111738","ajoa@bredband.net","2710067","千葉県","松戸市樋野口",""," \"  ","〓〓","","","448"
"6275980340000111993","g7-chiro@mbp.nifty.com","2220031","神奈川県","横浜市港北区太尾町","","","角田","郁代","0455482822","448"
"6275980340000112231","k_hama@w4.dion.ne.jp","2360052","神奈川県","横浜市金沢区富岡西","","","浜田","和久","0457754803","448"
"6275980340000113064",""reiko_hoshikawa"@hotmail.com","2220026","神奈川県","横浜市港北区","篠原町72-2","コンフォール篠原2-104","茂木","玲子","0454230900","448"
"6275980340000113148","ali06@nifty.com","1640011","東京都","中野区中央","","","齋藤","亞理","0333843247","448"
"6275980340125312757","satokich@hotmail.com","6740094","兵庫県","明石市","二見町西二見1981-1","レーウ"フロンスA202","來田","淳子","","486"
"6275980340079774754","m,a,n,a,mail@docomo.ne.jp","2630043","千葉県","千葉市稲毛区小仲台","","レクセル稲毛パークス702","横山","雅菜","","447"
"6275980340032948677","maya..mama.natsuki@docomo.ne.jp","2210863","神奈川県","横浜市神奈川区","羽沢町984","サンハイツ羽沢2-301","岡部","麻弥","","448"
"6275980340032951044","ino.0827.yok@docomo.ne.jp","2310847","神奈川県","横浜市中区","竹之丸55","グリーンヒル山手A-2","猪俣","葉子","0456638634","448"
"6275980340032952455","jigoro.ore@ezweb.ne.jp","2400007","神奈川県","横浜市保土ヶ谷区明神台","","","土屋","尚子","045-331-5524
test
","448"
"6275980340032954196","acchi0819-lucida@docomo.ne.jp","2260006","神奈川県","横浜市緑区鴨居","","","佐藤","","","448"
"6275980340032955185","y.t-vs-a.h@docomo.ne.jp","2510052","神奈川県","藤沢市藤沢","","ミサワホームズ藤沢1106号室","土岐","麻美","0466244345","448"
"6275980340076656582","yukko4513@ezweb.ne.jp","2200101","神奈川県","相模原市城山町","町屋4-24-24 ","","中野","幸子","0427825439
","448"
"6275980340125281648","k-tomomi-6.14@ezweb.ne.jp","6570024","兵庫県","神戸市灘区楠ヶ岡町","","高羽グリーンハイツ203","神吉","友美","
","486"
"6275980340059092649","motoyaworld@hotmail.com","2220031","神奈川県","横浜市港北区","太尾町 3445 ",""ェラ大倉山 205号室","佐藤","基哉","0455470266","448"
"6275980340000110250","okusuribukuro@kza.biglobe.ne.jp","2260029","神奈川県","横浜市緑区森の台","","","御薬袋","","0459310853","448ウ"
"6275980340000110821","tanoshiku-nakayoku@s5.dion.ne.jp","2260026","神奈川県","横浜市緑区長津田町","","","稲垣","理佐","0459837010ウ","448"
"6275980340000111167","snow4136@da2.so-net.ne.jp","2220036","神奈川県","横浜市港北区小机町","","","吉田","幸子","0454765658ウ"","448"
"6275980340000111241","kaori-5.29@ka.tnc.ne.jp","4200947","静岡県","静岡市葵区堤町","","","矢口","香緒里","0542711840","448ウ""
"6275980340000111324","Taikoowanrd@yahoo.co.jp","2300077","神奈川県","横浜市鶴見区東寺尾","","","菊地","そのへ","045ウ"5715949","448"
"6275980340000111407","suzuki37@hotmail.com","2260016","神奈川県","横浜市緑区霧が丘","","","鈴木","三奈","0459223865","448"
"6275980340000111738","ajoa@bredband.net","2710067","千葉県","松戸市樋野口",""," \"  ","〓〓","","","448"
"6275980340000111993","g7-chiro@mbp.nifty.com","2220031","神奈川県","横浜市港北区太尾町","","","角田","郁代","0455482822","448"
"6275980340000112231","k_hama@w4.dion.ne.jp","2360052","神奈川県","横浜市金沢区富岡西","","","浜田","和久","0457754803","448"
"6275980340000113064",""reiko_hoshikawa"@hotmail.com","2220026","神奈川県","横浜市港北区","篠原町72-2","コンフォール篠原2-104","茂木","玲子","0454230900","448"
"6275980340000113148","ali06@nifty.com","1640011","東京都","中野区中央","","","齋藤","亞理","0333843247","448"
"6275980340125312757","satokich@hotmail.com","6740094","兵庫県","明石市","二見町西二見1981-1","レーウ"フロンスA202","來田","淳子","","486"
"6275980340079774754","m,a,n,a,mail@docomo.ne.jp","2630043","千葉県","千葉市稲毛区小仲台","","レクセル稲毛パークス702","横山","雅菜","","447"
"6275980340032948677","maya..mama.natsuki@docomo.ne.jp","2210863","神奈川県","横浜市神奈川区","羽沢町984","サンハイツ羽沢2-301","岡部","麻弥","","448"
"6275980340032951044","ino.0827.yok@docomo.ne.jp","2310847","神奈川県","横浜市中区","竹之丸55","グリーンヒル山手A-2","猪俣","葉子","0456638634","448"
"6275980340032952455","jigoro.ore@ezweb.ne.jp","2400007","神奈川県","横浜市保土ヶ谷区明神台","","","土屋","尚子","045-331-5524
test
","448"
"6275980340032954196","acchi0819-lucida@docomo.ne.jp","2260006","神奈川県","横浜市緑区鴨居","","","佐藤","","","448"
"6275980340032955185","y.t-vs-a.h@docomo.ne.jp","2510052","神奈川県","藤沢市藤沢","","ミサワホームズ藤沢1106号室","土岐","麻美","0466244345","448"
"6275980340076656582","yukko4513@ezweb.ne.jp","2200101","神奈川県","相模原市城山町","町屋4-24-24 ","","中野","幸子","0427825439
","448"
"6275980340125281648","k-tomomi-6.14@ezweb.ne.jp","6570024","兵庫県","神戸市灘区楠ヶ岡町","","高羽グリーンハイツ203","神吉","友美","
","486"
"6275980340059092649","motoyaworld@hotmail.com","2220031","神奈川県","横浜市港北区","太尾町 3445 ",""ェラ大倉山 205号室","佐藤","基哉","0455470266","448"
"6275980340000110250","okusuribukuro@kza.biglobe.ne.jp","2260029","神奈川県","横浜市緑区森の台","","","御薬袋","","0459310853","448ウ"
"6275980340000110821","tanoshiku-nakayoku@s5.dion.ne.jp","2260026","神奈川県","横浜市緑区長津田町","","","稲垣","理佐","0459837010ウ","448"
"6275980340000111167","snow4136@da2.so-net.ne.jp","2220036","神奈川県","横浜市港北区小机町","","","吉田","幸子","0454765658ウ"","448"
"6275980340000111241","kaori-5.29@ka.tnc.ne.jp","4200947","静岡県","静岡市葵区堤町","","","矢口","香緒里","0542711840","448ウ""
"6275980340000111324","Taikoowanrd@yahoo.co.jp","2300077","神奈川県","横浜市鶴見区東寺尾","","","菊地","そのへ","045ウ"5715949","448"
"6275980340000111407","suzuki37@hotmail.com","2260016","神奈川県","横浜市緑区霧が丘","","","鈴木","三奈","0459223865","448"
"6275980340000111738","ajoa@bredband.net","2710067","千葉県","松戸市樋野口",""," \"  ","〓〓","","","448"
"6275980340000111993","g7-chiro@mbp.nifty.com","2220031","神奈川県","横浜市港北区太尾町","","","角田","郁代","0455482822","448"
"6275980340000112231","k_hama@w4.dion.ne.jp","2360052","神奈川県","横浜市金沢区富岡西","","","浜田","和久","0457754803","448"
"6275980340000113064",""reiko_hoshikawa"@hotmail.com","2220026","神奈川県","横浜市港北区","篠原町72-2","コンフォール篠原2-104","茂木","玲子","0454230900","448"
"6275980340000113148","ali06@nifty.com","1640011","東京都","中野区中央","","","齋藤","亞理","0333843247","448"
"6275980340125312757","satokich@hotmail.com","6740094","兵庫県","明石市","二見町西二見1981-1","レーウ"フロンスA202","來田","淳子","","486"
"6275980340079774754","m,a,n,a,mail@docomo.ne.jp","2630043","千葉県","千葉市稲毛区小仲台","","レクセル稲毛パークス702","横山","雅菜","","447"
"6275980340032948677","maya..mama.natsuki@docomo.ne.jp","2210863","神奈川県","横浜市神奈川区","羽沢町984","サンハイツ羽沢2-301","岡部","麻弥","","448"
"6275980340032951044","ino.0827.yok@docomo.ne.jp","2310847","神奈川県","横浜市中区","竹之丸55","グリーンヒル山手A-2","猪俣","葉子","0456638634","448"
"6275980340032952455","jigoro.ore@ezweb.ne.jp","2400007","神奈川県","横浜市保土ヶ谷区明神台","","","土屋","尚子","045-331-5524
test
","448"
"6275980340032954196","acchi0819-lucida@docomo.ne.jp","2260006","神奈川県","横浜市緑区鴨居","","","佐藤","","","448"
"6275980340032955185","y.t-vs-a.h@docomo.ne.jp","2510052","神奈川県","藤沢市藤沢","","ミサワホームズ藤沢1106号室","土岐","麻美","0466244345","448"
"6275980340076656582","yukko4513@ezweb.ne.jp","2200101","神奈川県","相模原市城山町","町屋4-24-24 ","","中野","幸子","0427825439
","448"
"6275980340125281648","k-tomomi-6.14@ezweb.ne.jp","6570024","兵庫県","神戸市灘区楠ヶ岡町","","高羽グリーンハイツ203","神吉","友美","
","486"
"6275980340059092649","motoyaworld@hotmail.com","2220031","神奈川県","横浜市港北区","太尾町 3445 ",""ェラ大倉山 205号室","佐藤","基哉","0455470266","448"
Can't render this file because it contains an unexpected character in line 3 and column 165.

View File

@@ -1,93 +0,0 @@
#!/usr/bin/env -S uv run --script
"""
Test for progress class
"""
import time
from random import randint
import sys
import io
from pathlib import Path
from corelibs.file_handling.progress import Progress
from corelibs.string_handling.datetime_helpers import convert_timestamp, create_time
def main():
"""
test run
"""
# flag for file test or normal run
use_file = 1
# variables for object init
verbose = 1
precision = -2
microtime = 0
wide_time = False
# object init
prg = Progress(
verbose=verbose,
precision=precision,
microtime=microtime,
wide_time=wide_time
)
# prg.SetPrecision(-2)
# prg.SetStartTime(time.time())
prg.set_start_time()
print(
f"PRECISION: {prg.precision} | TEN STEP: {prg.precision_ten_step} | "
f"WIDE TEME: {prg.wide_time} | MICROTIME: {prg.microtime} | VERBOSE: {prg.verbose}"
)
if use_file:
file_name = 'foo2.csv'
folder_name = 'data'
script_path: Path = Path(__file__).resolve().parent
file_path = script_path.joinpath(folder_name, file_name)
current_size = 0
# , newline=''
with open(file_path, 'r', encoding="UTF-8") as fh:
fh.seek(0, io.SEEK_END)
prg.set_filesize(fh.tell())
fh.seek(0, io.SEEK_SET)
print(f"Is FH seekable: {fh.seekable()} | Is TTY: {fh.isatty()}")
print(
f"Buffer size: {io.DEFAULT_BUFFER_SIZE} | "
f"Do Buffering: {fh.line_buffering} | "
f"File size: {prg.filesize}"
)
data = fh.readline()
while data:
current_size += sys.getsizeof(data)
# print(f"Data: {data}", end = '', flush = True)
# print(f"Lenght: {len(data)} | Size: {sys.getsizeof(data):,}/{current_size:,}/{prg.filesize:,}")
# prg.ShowPosition(current_size)
# print(f"Tell: {fh.tell()}")
# ouput progress with current file pos
prg.show_position(fh.tell())
data = fh.readline()
else:
print(f"Starting: {create_time(prg.start if prg.start is not None else 0)}")
prg.set_linecount(256)
i = 1
while i <= prg.linecount:
sleep = randint(1, 9)
sleep /= 7
time.sleep(sleep)
# print(f"[{i}] Sleep for {sleep}")
prg.show_position()
i += 1
prg.set_end_time()
print(
f"Start: {create_time(prg.start if prg.start is not None else 0)}, "
f"End: {create_time(prg.end if prg.end is not None else 0)}, "
f"Run Time: {convert_timestamp(prg.run_time if prg.run_time is not None else 0)}, "
f"Verbose: {prg.verbose}"
)
if __name__ == '__main__':
main()
# __END__

0
tests/unit/__init__.py Normal file
View File

View File

@@ -0,0 +1,155 @@
"""
PyTest: string_handling/timestamp_strings
"""
from datetime import datetime
from unittest.mock import patch, MagicMock
from zoneinfo import ZoneInfo
import pytest
# Assuming the class is in a file called timestamp_strings.py
from corelibs.string_handling.timestamp_strings import TimestampStrings
class TestTimestampStrings:
"""Test suite for TimestampStrings class"""
def test_default_initialization(self):
"""Test initialization with default timezone"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings()
assert ts.time_zone == 'Asia/Tokyo'
assert ts.timestamp_now == mock_now
assert ts.today == '2023-12-25'
assert ts.timestamp == '2023-12-25 15:30:45'
assert ts.timestamp_file == '2023-12-25_153045'
def test_custom_timezone_initialization(self):
"""Test initialization with custom timezone"""
custom_tz = 'America/New_York'
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings(time_zone=custom_tz)
assert ts.time_zone == custom_tz
assert ts.timestamp_now == mock_now
def test_invalid_timezone_raises_error(self):
"""Test that invalid timezone raises ValueError"""
invalid_tz = 'Invalid/Timezone'
with pytest.raises(ValueError) as exc_info:
TimestampStrings(time_zone=invalid_tz)
assert 'Zone could not be loaded [Invalid/Timezone]' in str(exc_info.value)
def test_timestamp_formats(self):
"""Test various timestamp format outputs"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
# Mock both datetime.now() calls
mock_now = datetime(2023, 12, 25, 9, 5, 3)
mock_now_tz = datetime(2023, 12, 25, 23, 5, 3, tzinfo=ZoneInfo('Asia/Tokyo'))
mock_datetime.now.side_effect = [mock_now, mock_now_tz]
ts = TimestampStrings()
assert ts.today == '2023-12-25'
assert ts.timestamp == '2023-12-25 09:05:03'
assert ts.timestamp_file == '2023-12-25_090503'
assert 'JST' in ts.timestamp_tz or 'Asia/Tokyo' in ts.timestamp_tz
def test_different_timezones_produce_different_results(self):
"""Test that different timezones produce different timestamp_tz values"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 12, 0, 0)
mock_datetime.now.return_value = mock_now
# Create instances with different timezones
ts_tokyo = TimestampStrings(time_zone='Asia/Tokyo')
ts_ny = TimestampStrings(time_zone='America/New_York')
# The timezone-aware timestamps should be different
assert ts_tokyo.time_zone != ts_ny.time_zone
# Note: The actual timestamp_tz values will depend on the mocked datetime
def test_class_default_timezone(self):
"""Test that class default timezone is correctly set"""
assert TimestampStrings.time_zone == 'Asia/Tokyo'
def test_none_timezone_uses_default(self):
"""Test that passing None for timezone uses class default"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings(time_zone=None)
assert ts.time_zone == 'Asia/Tokyo'
def test_timestamp_file_format_no_colons(self):
"""Test that timestamp_file format doesn't contain colons (safe for filenames)"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings()
assert ':' not in ts.timestamp_file
assert ' ' not in ts.timestamp_file
assert ts.timestamp_file == '2023-12-25_153045'
def test_multiple_instances_independent(self):
"""Test that multiple instances don't interfere with each other"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
ts1 = TimestampStrings(time_zone='Asia/Tokyo')
ts2 = TimestampStrings(time_zone='Europe/London')
assert ts1.time_zone == 'Asia/Tokyo'
assert ts2.time_zone == 'Europe/London'
assert ts1.time_zone != ts2.time_zone
@patch('corelibs.string_handling.timestamp_strings.ZoneInfo')
def test_zoneinfo_called_correctly(self, mock_zoneinfo: MagicMock):
"""Test that ZoneInfo is called with correct timezone"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 15, 30, 45)
mock_datetime.now.return_value = mock_now
custom_tz = 'Europe/Paris'
ts = TimestampStrings(time_zone=custom_tz)
assert ts.time_zone == custom_tz
mock_zoneinfo.assert_called_with(custom_tz)
def test_edge_case_midnight(self):
"""Test timestamp formatting at midnight"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2023, 12, 25, 0, 0, 0)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings()
assert ts.timestamp == '2023-12-25 00:00:00'
assert ts.timestamp_file == '2023-12-25_000000'
def test_edge_case_new_year(self):
"""Test timestamp formatting at new year"""
with patch('corelibs.string_handling.timestamp_strings.datetime') as mock_datetime:
mock_now = datetime(2024, 1, 1, 0, 0, 0)
mock_datetime.now.return_value = mock_now
ts = TimestampStrings()
assert ts.today == '2024-01-01'
assert ts.timestamp == '2024-01-01 00:00:00'