Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SCODOC_R6A06
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paul Milleville
SCODOC_R6A06
Commits
cc7fcded
Commit
cc7fcded
authored
8 months ago
by
Emmanuel Viennet
Browse files
Options
Downloads
Patches
Plain Diff
Améliore heure_to_iso8601 (#1004) et ajoute unit tests
parent
ccc0e58a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/scodoc/sco_utils.py
+11
-7
11 additions, 7 deletions
app/scodoc/sco_utils.py
tests/unit/test_sco_utils.py
+82
-0
82 additions, 0 deletions
tests/unit/test_sco_utils.py
with
93 additions
and
7 deletions
app/scodoc/sco_utils.py
+
11
−
7
View file @
cc7fcded
...
...
@@ -152,16 +152,20 @@ def convert_fr_date(
raise
ScoValueError
(
"
Date (j/m/a) invalide
"
)
def
heure_to_iso8601
(
t
,
null_is_empty
=
False
)
->
str
:
"
convert time string to ISO 8601 (allow 16:03, 16h03, 16)
"
def
heure_to_iso8601
(
t
:
str
|
datetime
.
time
=
None
)
->
str
:
"""
Convert time string or time object to ISO 8601 (allows 16:03, 16h03, 16),
returns hh:mm:ss or empty string
"""
if
isinstance
(
t
,
datetime
.
time
):
return
t
.
isoformat
()
if
not
t
and
null_is_empty
:
if
not
t
:
return
""
t
=
t
.
strip
().
upper
().
replace
(
"
H
"
,
"
:
"
)
if
t
and
t
.
count
(
"
:
"
)
==
0
and
len
(
t
)
<
3
:
t
=
t
+
"
:00
"
return
t
parts
=
t
.
strip
().
upper
().
replace
(
"
H
"
,
"
:
"
).
split
(
"
:
"
)
# here we can have hh, hh:mm or hh:mm:ss
h
=
int
(
parts
[
0
])
m
=
int
(
parts
[
1
])
if
len
(
parts
)
>
1
else
0
s
=
int
(
parts
[
2
])
if
len
(
parts
)
>
2
else
0
return
datetime
.
time
(
h
,
m
,
s
).
isoformat
()
def
print_progress_bar
(
...
...
This diff is collapsed.
Click to expand it.
tests/unit/test_sco_utils.py
0 → 100644
+
82
−
0
View file @
cc7fcded
"""
Test des fonctions utilitaires de sco_utils.py
Utiliser comme:
pytest tests/unit/test_sco_utils.py
"""
# pylint: disable=C0111
# no doc strings in short tests
import
datetime
import
pytest
from
app.scodoc.sco_exceptions
import
ScoValueError
from
app.scodoc.sco_utils
import
convert_fr_date
,
heure_to_iso8601
def
test_convert_fr_date_full_date
():
assert
convert_fr_date
(
"
12/2/1972
"
)
==
datetime
.
datetime
(
1972
,
2
,
12
)
def
test_convert_fr_date_short_date_before_pivot
():
assert
convert_fr_date
(
"
12/2/24
"
)
==
datetime
.
datetime
(
2024
,
2
,
12
)
def
test_convert_fr_date_short_date_after_pivot
():
assert
convert_fr_date
(
"
12/2/72
"
)
==
datetime
.
datetime
(
1972
,
2
,
12
)
def
test_convert_fr_date_datetime_object
():
dt
=
datetime
.
datetime
(
2022
,
2
,
12
)
assert
convert_fr_date
(
dt
)
==
dt
def
test_convert_fr_date_invalid_date
():
with
pytest
.
raises
(
ScoValueError
):
convert_fr_date
(
"
invalid date
"
)
def
test_convert_fr_date_iso_format
():
assert
convert_fr_date
(
"
2022-02-12T00:00:00
"
)
==
datetime
.
datetime
(
2022
,
2
,
12
)
def
test_convert_fr_date_invalid_iso_format
():
with
pytest
.
raises
(
ScoValueError
):
convert_fr_date
(
"
2022-02-30T00:00:00
"
)
def
test_heure_to_iso8601_full_time
():
assert
heure_to_iso8601
(
"
16:01:02
"
)
==
"
16:01:02
"
def
test_heure_to_iso8601_hour_minute
():
assert
heure_to_iso8601
(
"
16:03
"
)
==
"
16:03:00
"
def
test_heure_to_iso8601_hour_only
():
assert
heure_to_iso8601
(
"
16
"
)
==
"
16:00:00
"
def
test_heure_to_iso8601_single_digit
():
assert
heure_to_iso8601
(
"
1:2:3
"
)
==
"
01:02:03
"
def
test_heure_to_iso8601_invalid_input
():
with
pytest
.
raises
(
ValueError
):
heure_to_iso8601
(
"
invalid
"
)
def
test_heure_to_iso8601_datetime_time
():
time_obj
=
datetime
.
time
(
16
,
1
,
2
)
assert
heure_to_iso8601
(
time_obj
)
==
"
16:01:02
"
def
test_heure_to_iso8601_null
():
assert
heure_to_iso8601
(
""
)
==
""
# Run the tests
if
__name__
==
"
__main__
"
:
pytest
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment