Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
kamaki
Commits
b7ff6e0c
Commit
b7ff6e0c
authored
Jul 18, 2013
by
Stavros Sachtouris
Browse files
Test DateArgument
Refs: #4058
parent
02987e9c
Changes
3
Hide whitespace changes
Inline
Side-by-side
kamaki/cli/argument/__init__.py
View file @
b7ff6e0c
...
...
@@ -231,18 +231,10 @@ class IntArgument(ValueArgument):
class
DateArgument
(
ValueArgument
):
"""
:value type: a string formated in an acceptable date format
:value returns: same date in first of DATE_FORMATS
"""
DATE_FORMATS
=
[
"%a %b %d %H:%M:%S %Y"
,
"%A, %d-%b-%y %H:%M:%S GMT"
,
"%a, %d %b %Y %H:%M:%S GMT"
]
DATE_FORMAT
=
'%a %b %d %H:%M:%S %Y'
INPUT_FORMATS
=
DATE_FORMAT
S
+
[
"
%d-%m-%Y
"
,
"
%H:%M:%S %d-%m-%Y
"
]
INPUT_FORMATS
=
[
DATE_FORMAT
,
'
%d-%m-%Y
'
,
'
%H:%M:%S %d-%m-%Y
'
]
@
property
def
timestamp
(
self
):
...
...
@@ -252,7 +244,7 @@ class DateArgument(ValueArgument):
@
property
def
formated
(
self
):
v
=
getattr
(
self
,
'_value'
,
self
.
default
)
return
v
.
strftime
(
self
.
DATE_FORMAT
S
[
0
]
)
if
v
else
None
return
v
.
strftime
(
self
.
DATE_FORMAT
)
if
v
else
None
@
property
def
value
(
self
):
...
...
@@ -260,8 +252,7 @@ class DateArgument(ValueArgument):
@
value
.
setter
def
value
(
self
,
newvalue
):
if
newvalue
:
self
.
_value
=
self
.
format_date
(
newvalue
)
self
.
_value
=
self
.
format_date
(
newvalue
)
def
format_date
(
self
,
datestr
):
for
format
in
self
.
INPUT_FORMATS
:
...
...
@@ -269,12 +260,10 @@ class DateArgument(ValueArgument):
t
=
dtm
.
strptime
(
datestr
,
format
)
except
ValueError
:
continue
return
t
# .strftime(self.DATE_FORMATS[0])
raiseCLIError
(
None
,
'Date Argument Error'
,
details
=
'%s not a valid date. correct formats:
\n\t
%s'
%
(
datestr
,
self
.
INPUT_FORMATS
))
return
t
# .strftime(self.DATE_FORMAT)
raiseCLIError
(
None
,
'Date Argument Error'
,
details
=
[
'%s not a valid date'
%
datestr
,
'Correct formats:
\n\t
%s'
%
self
.
INPUT_FORMATS
])
class
VersionArgument
(
FlagArgument
):
...
...
kamaki/cli/argument/test.py
View file @
b7ff6e0c
...
...
@@ -31,9 +31,10 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from
mock
import
patch
,
call
from
mock
import
patch
,
call
,
MagicMock
from
unittest
import
TestCase
from
StringIO
import
StringIO
from
datetime
import
datetime
#from itertools import product
from
kamaki.cli
import
argument
,
errors
...
...
@@ -232,6 +233,45 @@ class IntArgument(TestCase):
self
.
assertTrue
(
isinstance
(
e
,
err
))
class
DateArgument
(
TestCase
):
def
test_timestamp
(
self
):
da
=
argument
.
DateArgument
(
parsed_name
=
'--date'
)
self
.
assertEqual
(
da
.
timestamp
,
None
)
date
,
format
,
exp
=
'24-10-1917'
,
'%d-%m-%Y'
,
-
1646964000.0
da
.
_value
=
argument
.
dtm
.
strptime
(
date
,
format
)
self
.
assertEqual
(
da
.
timestamp
,
exp
)
def
test_formated
(
self
):
da
=
argument
.
DateArgument
(
parsed_name
=
'--date'
)
self
.
assertEqual
(
da
.
formated
,
None
)
date
,
format
,
exp
=
(
'24-10-1917'
,
'%d-%m-%Y'
,
'Wed Oct 24 00:00:00 1917'
)
da
.
_value
=
argument
.
dtm
.
strptime
(
date
,
format
)
self
.
assertEqual
(
da
.
formated
,
exp
)
@
patch
(
'kamaki.cli.argument.DateArgument.timestamp'
)
@
patch
(
'kamaki.cli.argument.DateArgument.format_date'
)
def
test_value
(
self
,
format_date
,
timestamp
):
da
=
argument
.
DateArgument
(
parsed_name
=
'--date'
)
self
.
assertTrue
(
isinstance
(
da
.
value
,
MagicMock
))
da
.
value
=
'Something'
format_date
.
assert_called_once
(
call
(
'Something'
))
def
test_format_date
(
self
):
da
=
argument
.
DateArgument
(
parsed_name
=
'--date'
)
for
datestr
,
exp
in
(
(
'Wed Oct 24 01:02:03 1917'
,
datetime
(
1917
,
10
,
24
,
1
,
2
,
3
)),
(
'24-10-1917'
,
datetime
(
1917
,
10
,
24
)),
(
'01:02:03 24-10-1917'
,
datetime
(
1917
,
10
,
24
,
1
,
2
,
3
))):
self
.
assertEqual
(
da
.
format_date
(
datestr
),
exp
)
for
datestr
,
err
in
(
(
'32-40-20134'
,
errors
.
CLIError
),
(
'Wednesday, 24 Oct 2017'
,
errors
.
CLIError
),
(
None
,
TypeError
),
(
0.8
,
TypeError
)):
self
.
assertRaises
(
err
,
da
.
format_date
,
datestr
)
if
__name__
==
'__main__'
:
from
sys
import
argv
from
kamaki.cli.test
import
runTestCase
...
...
@@ -241,3 +281,4 @@ if __name__ == '__main__':
runTestCase
(
FlagArgument
,
'FlagArgument'
,
argv
[
1
:])
runTestCase
(
FlagArgument
,
'ValueArgument'
,
argv
[
1
:])
runTestCase
(
IntArgument
,
'IntArgument'
,
argv
[
1
:])
runTestCase
(
DateArgument
,
'DateArgument'
,
argv
[
1
:])
kamaki/cli/test.py
View file @
b7ff6e0c
...
...
@@ -37,7 +37,7 @@ from inspect import getmembers, isclass
from
kamaki.cli.command_tree.test
import
Command
,
CommandTree
from
kamaki.cli.argument.test
import
(
Argument
,
ConfigArgument
,
RuntimeConfigArgument
,
FlagArgument
,
ValueArgument
,
IntArgument
)
ValueArgument
,
IntArgument
,
DateArgument
)
# TestCase auxiliary methods
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment