Skip to content

Commit

Permalink
[Python] Fixed bug in enum namespace docs (Azure#1074)
Browse files Browse the repository at this point in the history
* Fixed bug in enum namespace docs

* Fixed parameter order

* Fixed decimal serialization

* Added decimal tests

* formatting cleanups
  • Loading branch information
annatisch authored and tbombach committed Jun 2, 2016
1 parent ae0f0a3 commit 14a2cdf
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import isodate
import tempfile
import json
from decimal import Decimal
from datetime import date, datetime, timedelta
import os
from os.path import dirname, pardir, join, realpath, sep, pardir
Expand Down Expand Up @@ -59,13 +60,24 @@ def test_numbers(self):
client.number.put_small_double(2.5976931e-101)
client.number.put_big_double_negative_decimal(-99999999.99)
client.number.put_big_double_positive_decimal(99999999.99)
client.number.put_big_decimal(Decimal(2.5976931e+101))
client.number.put_small_decimal(Decimal(2.5976931e-101))
client.number.put_big_decimal_positive_decimal(Decimal(99999999.99))
client.number.put_big_decimal_negative_decimal(Decimal(-99999999.99))
client.number.get_null()
self.assertEqual(client.number.get_big_float(), 3.402823e+20)
self.assertEqual(client.number.get_small_float(), 3.402823e-20)
self.assertEqual(client.number.get_big_double(), 2.5976931e+101)
self.assertEqual(client.number.get_small_double(), 2.5976931e-101)
self.assertEqual(client.number.get_big_double_negative_decimal(), -99999999.99)
self.assertEqual(client.number.get_big_double_positive_decimal(), 99999999.99)
self.assertEqual(client.number.get_big_decimal(), 2.5976931e+101)
self.assertEqual(client.number.get_small_decimal(), 2.5976931e-101)
self.assertEqual(client.number.get_big_decimal_negative_decimal(), -99999999.99)
self.assertEqual(client.number.get_big_decimal_positive_decimal(), 99999999.99)

with self.assertRaises(DeserializationError):
client.number.get_invalid_decimal()

with self.assertRaises(DeserializationError):
client.number.get_invalid_double()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Basic(Model):
:param color: Possible values include: 'cyan', 'Magenta', 'YELLOW',
'blacK'
:type color: str or :class:`CMYKColors
<autorestcomplextestservice.models.CMYKColors>`
<fixtures.acceptancetestsbodycomplex.models.CMYKColors>`
"""

_attribute_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_invalid_decimal(
deserialized = None

if response.status_code == 200:
deserialized = self._deserialize('Decimal', response)
deserialized = self._deserialize('decimal', response)

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down Expand Up @@ -584,7 +584,7 @@ def put_big_decimal(
header_parameters.update(custom_headers)

# Construct body
body_content = self._serialize.body(number_body, 'Decimal')
body_content = self._serialize.body(number_body, 'decimal')

# Construct and send request
request = self._client.put(url, query_parameters)
Expand Down Expand Up @@ -634,7 +634,7 @@ def get_big_decimal(
deserialized = None

if response.status_code == 200:
deserialized = self._deserialize('Decimal', response)
deserialized = self._deserialize('decimal', response)

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down Expand Up @@ -671,7 +671,7 @@ def put_big_decimal_positive_decimal(
header_parameters.update(custom_headers)

# Construct body
body_content = self._serialize.body(number_body, 'Decimal')
body_content = self._serialize.body(number_body, 'decimal')

# Construct and send request
request = self._client.put(url, query_parameters)
Expand Down Expand Up @@ -721,7 +721,7 @@ def get_big_decimal_positive_decimal(
deserialized = None

if response.status_code == 200:
deserialized = self._deserialize('Decimal', response)
deserialized = self._deserialize('decimal', response)

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down Expand Up @@ -758,7 +758,7 @@ def put_big_decimal_negative_decimal(
header_parameters.update(custom_headers)

# Construct body
body_content = self._serialize.body(number_body, 'Decimal')
body_content = self._serialize.body(number_body, 'decimal')

# Construct and send request
request = self._client.put(url, query_parameters)
Expand Down Expand Up @@ -808,7 +808,7 @@ def get_big_decimal_negative_decimal(
deserialized = None

if response.status_code == 200:
deserialized = self._deserialize('Decimal', response)
deserialized = self._deserialize('decimal', response)

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down Expand Up @@ -1019,7 +1019,7 @@ def put_small_decimal(
header_parameters.update(custom_headers)

# Construct body
body_content = self._serialize.body(number_body, 'Decimal')
body_content = self._serialize.body(number_body, 'decimal')

# Construct and send request
request = self._client.put(url, query_parameters)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def get_small_decimal(
deserialized = None

if response.status_code == 200:
deserialized = self._deserialize('Decimal', response)
deserialized = self._deserialize('decimal', response)

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def get_not_expandable(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: :class:`Colors <autorestswaggerbatservice.models.Colors>`
:rtype: :class:`Colors
<fixtures.acceptancetestsbodystring.models.Colors>`
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down Expand Up @@ -85,7 +86,7 @@ def put_not_expandable(
:param string_body: Possible values include: 'red color',
'green-color', 'blue_color'
:type string_body: str or :class:`Colors
<autorestswaggerbatservice.models.Colors>`
<fixtures.acceptancetestsbodystring.models.Colors>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ def param_enum(
:param value: Send a post request with header values 'GREY' .
Possible values include: 'White', 'black', 'GREY'
:type value: str or :class:`GreyscaleColors
<autorestswaggerbatheaderservice.models.GreyscaleColors>`
<fixtures.acceptancetestsheader.models.GreyscaleColors>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def enum_valid(
:param enum_path: send the value green. Possible values include: 'red
color', 'green color', 'blue color'
:type enum_path: str or :class:`UriColor
<autoresturltestservice.models.UriColor>`
<fixtures.acceptancetestsurl.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down Expand Up @@ -688,7 +688,7 @@ def enum_null(
:param enum_path: send null should throw. Possible values include:
'red color', 'green color', 'blue color'
:type enum_path: str or :class:`UriColor
<autoresturltestservice.models.UriColor>`
<fixtures.acceptancetestsurl.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def enum_valid(
:param enum_query: 'green color' enum value. Possible values include:
'red color', 'green color', 'blue color'
:type enum_query: str or :class:`UriColor
<autoresturltestservice.models.UriColor>`
<fixtures.acceptancetestsurl.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down Expand Up @@ -849,7 +849,7 @@ def enum_null(
:param enum_query: null string value. Possible values include: 'red
color', 'green color', 'blue color'
:type enum_query: str or :class:`UriColor
<autoresturltestservice.models.UriColor>`
<fixtures.acceptancetestsurl.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Product(Model):
:param const_string_as_enum: Constant string as Enum. Possible values
include: 'constant_string_as_enum'
:type const_string_as_enum: str or :class:`EnumConst
<autorestvalidationtest.models.EnumConst>`
<fixtures.acceptancetestsvalidation.models.EnumConst>`
"""

_validation = {
Expand Down
8 changes: 7 additions & 1 deletion AutoRest/Generators/Python/Python/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public static string ToPythonRuntimeTypeString(this IType type)
{
return "base64";
}

if (known.Type == KnownPrimaryType.Decimal)
{
return "decimal";
}
}

var enumType = type as EnumType;
Expand Down Expand Up @@ -264,7 +269,8 @@ public static string GetPythonSerializationType(IType type)
{ KnownPrimaryType.DateTimeRfc1123, "rfc-1123" },
{ KnownPrimaryType.TimeSpan, "duration" },
{ KnownPrimaryType.UnixTime, "unix-time" },
{ KnownPrimaryType.Base64Url, "base64" }
{ KnownPrimaryType.Base64Url, "base64" },
{ KnownPrimaryType.Decimal, "decimal" }
};
PrimaryType primaryType = type as PrimaryType;
if (primaryType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ public string GetDocumentationType(IType type)

string result = "object";
var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", "");
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");

var primaryType = type as PrimaryType;
var listType = type as SequenceType;
var enumType = type as EnumType;
Expand Down Expand Up @@ -593,8 +596,6 @@ public string GetDocumentationType(IType type)
}
else if (type is CompositeType)
{
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");
result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ public string GetPropertyDocumentationType(IType type)

string result = "object";
var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", "");
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");

var listType = type as SequenceType;
if (type is PrimaryType)
{
Expand All @@ -485,8 +488,6 @@ public string GetPropertyDocumentationType(IType type)
}
else if (type is CompositeType)
{
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");
result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public virtual string RequiredConstructorParameters
{
get
{
var parameters = this.Properties.OrderBy(item => !item.IsRequired);
var requireParams = new List<string>();
foreach (var property in this.Properties)
foreach (var property in parameters)
{
if (property.IsRequired)
{
Expand All @@ -109,8 +110,9 @@ public virtual string ConfigConstructorParameters
{
get
{
var parameters = this.Properties.OrderBy(item => !item.IsRequired);
var configParams = new List<string>();
foreach (var property in this.Properties)
foreach (var property in parameters)
{
configParams.Add(property.Name.ToPythonCase());
}
Expand Down Expand Up @@ -257,6 +259,10 @@ public string GetPropertyDocumentationType(IType type)
throw new ArgumentNullException("type");
}

var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", "");
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");

string result = "object";
var primaryType = type as PrimaryType;
var listType = type as SequenceType;
Expand All @@ -277,17 +283,14 @@ public string GetPropertyDocumentationType(IType type)
}
else if (type is EnumType)
{
result = "str";
result = string.Format(CultureInfo.InvariantCulture, "str or :class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace);
}
else if (type is DictionaryType)
{
result = "dict";
}
else if (type is CompositeType)
{
var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", "");
if (!ServiceClient.Namespace.IsNullOrEmpty())
modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", "");
result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace);
}

Expand Down
12 changes: 11 additions & 1 deletion AutoRest/TestServer/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,17 @@ var coverage = {
"getInvalidUnixTime": 1,
"getNullUnixTime": 1,
"putUnixTime": 1,
"UrlPathsIntUnixTime": 1
"UrlPathsIntUnixTime": 1,
/* TODO: Once all the languages implement these tests, the scenario counters should be reset to zero. It is currently implemented in Python */
"getDecimalInvalid": 1,
"getDecimalBig": 1,
"getDecimalSmall": 1,
"getDecimalBigPositiveDecimal" : 1,
"getDecimalBigNegativeDecimal" : 1,
"putDecimalBig": 1,
"putDecimalSmall": 1,
"putDecimalBigPositiveDecimal" : 1,
"putDecimalBigNegativeDecimal" : 1,
};

// view engine setup
Expand Down
Loading

0 comments on commit 14a2cdf

Please sign in to comment.