Merge lp:~ack/txaws/parameter-type-float into lp:txaws

Proposed by Alberto Donato on 2016-08-04
Status: Merged
Merged at revision: 159
Proposed branch: lp:~ack/txaws/parameter-type-float
Merge into: lp:txaws
Diff against target: 95 lines (+65/-2)
2 files modified
txaws/server/schema.py (+24/-0)
txaws/server/tests/test_schema.py (+41/-2)
To merge this branch: bzr merge lp:~ack/txaws/parameter-type-float
Reviewer Review Type Date Requested Status
Free Ekanayaka 2016-08-04 Approve on 2016-08-04
Review via email: mp+302010@code.launchpad.net

Description of the change

Add a Float parameter type.

To post a comment you must log in.
Free Ekanayaka (free.ekanayaka) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txaws/server/schema.py'
2--- txaws/server/schema.py 2013-08-14 12:10:05 +0000
3+++ txaws/server/schema.py 2016-08-04 10:57:26 +0000
4@@ -237,6 +237,30 @@
5 return int(value)
6
7
8+class Float(Parameter):
9+ """A parameter that must be a {float}."""
10+
11+ kind = "float"
12+
13+ lower_than_min_template = "Value must be at least %s."
14+ greater_than_max_template = "Value exceeds maximum of %s."
15+
16+ def __init__(self, name=None, optional=False, default=None,
17+ min=0, max=None, allow_none=False, validator=None,
18+ doc=None):
19+ super(Float, self).__init__(
20+ name, optional, default, min, max, allow_none, validator, doc=doc)
21+
22+ def parse(self, value):
23+ return float(value)
24+
25+ def format(self, value):
26+ return str(value)
27+
28+ def measure(self, value):
29+ return float(value)
30+
31+
32 class Bool(Parameter):
33 """A parameter that must be a C{bool}."""
34
35
36=== modified file 'txaws/server/tests/test_schema.py'
37--- txaws/server/tests/test_schema.py 2013-08-14 12:10:05 +0000
38+++ txaws/server/tests/test_schema.py 2016-08-04 10:57:26 +0000
39@@ -8,8 +8,8 @@
40
41 from txaws.server.exception import APIError
42 from txaws.server.schema import (
43- Arguments, Bool, Date, Enum, Integer, Parameter, RawStr, Schema, Unicode,
44- UnicodeLine, List, Structure, InconsistentParameterError)
45+ Arguments, Bool, Date, Enum, Integer, Float, Parameter, RawStr, Schema,
46+ Unicode, UnicodeLine, List, Structure, InconsistentParameterError)
47
48
49 class ArgumentsTestCase(TestCase):
50@@ -343,6 +343,45 @@
51 self.assertIn("Invalid integer value %s" % garbage, error.message)
52
53
54+class FloatTestCase(TestCase):
55+
56+ def test_parse(self):
57+ """L{Float.parse} converts the given raw C{value} to C{float}."""
58+ parameter = Float("Test")
59+ self.assertEqual(123.45, parameter.parse("123.45"))
60+
61+ def test_format(self):
62+ """L{Float.format} converts the given float to a string."""
63+ parameter = Float("Test")
64+ self.assertEqual("123.45", parameter.format(123.45))
65+
66+ def test_min_and_max(self):
67+ """The L{Float} parameter properly supports ranges."""
68+ parameter = Float("Test", min=2.3, max=4.5)
69+
70+ error = self.assertRaises(APIError, parameter.coerce, "1.2")
71+ self.assertEqual(400, error.status)
72+ self.assertEqual("InvalidParameterValue", error.code)
73+ self.assertIn("Value must be at least 2.3.", error.message)
74+
75+ error = self.assertRaises(APIError, parameter.coerce, "5")
76+ self.assertIn("Value exceeds maximum of 4.5.", error.message)
77+ self.assertEqual(400, error.status)
78+ self.assertEqual("InvalidParameterValue", error.code)
79+
80+ def test_non_float_string(self):
81+ """
82+ The L{Float} parameter raises an L{APIError} when passed non-float
83+ values (in this case, a string).
84+ """
85+ garbage = "blah"
86+ parameter = Float("Test")
87+ error = self.assertRaises(APIError, parameter.coerce, garbage)
88+ self.assertEqual(400, error.status)
89+ self.assertEqual("InvalidParameterValue", error.code)
90+ self.assertIn("Invalid float value {}".format(garbage), error.message)
91+
92+
93 class BoolTestCase(TestCase):
94
95 def test_parse(self):

Subscribers

People subscribed via source and target branches