Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(snippetgen): add 2 placeholder values in lists #1013

Merged
merged 8 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def mock_value_original_type(self) -> Union[bool, str, bytes, int, float, List[A
# If this is a repeated field, then the mock answer should
# be a list.
if self.repeated:
answer = [answer]
first_item = self.primitive_mock(suffix=1) or None
second_item = self.primitive_mock(suffix=2) or None
answer = [first_item, second_item]

return answer

Expand Down Expand Up @@ -160,9 +162,12 @@ def inner_mock(self, stack, visited_fields) -> str:
# Done; return the mock value.
return answer

def primitive_mock(self) -> Union[bool, str, bytes, int, float, List[Any], None]:
def primitive_mock(self, suffix: int = 0) -> Union[bool, str, bytes, int, float, List[Any], None]:
"""Generate a valid mock for a primitive type. This function
returns the original (Python) type.

If a suffix is provided, generate a slightly different mock
using the provided integer.
"""
answer: Union[bool, str, bytes, int, float, List[Any], None] = None

Expand All @@ -174,13 +179,14 @@ def primitive_mock(self) -> Union[bool, str, bytes, int, float, List[Any], None]
if self.type.python_type == bool:
answer = True
elif self.type.python_type == str:
answer = f"{self.name}_value"
answer = f"{self.name}_value_{suffix}" if suffix else f"{self.name}_value"
elif self.type.python_type == bytes:
answer = bytes(f"{self.name}_blob", encoding="utf-8")
answer_str = f"{self.name}_blob_{suffix}" if suffix else f"{self.name}_blob"
answer = bytes(answer_str, encoding="utf-8")
elif self.type.python_type == int:
answer = sum([ord(i) for i in self.name])
answer = sum([ord(i) for i in self.name]) + suffix
elif self.type.python_type == float:
name_sum = sum([ord(i) for i in self.name])
name_sum = sum([ord(i) for i in self.name]) + suffix
answer = name_sum * pow(10, -1 * len(str(name_sum)))
else: # Impossible; skip coverage checks.
raise TypeError('Unrecognized PrimitiveType. This should '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def sample_generate_access_token():
# Initialize request argument(s)
request = credentials_v1.GenerateAccessTokenRequest(
name="projects/{project}/serviceAccounts/{service_account}",
scope=['scope_value'],
scope=['scope_value_1', 'scope_value_2'],
)

# Make the request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def sample_generate_access_token():
# Initialize request argument(s)
request = credentials_v1.GenerateAccessTokenRequest(
name="projects/{project}/serviceAccounts/{service_account}",
scope=['scope_value'],
scope=['scope_value_1', 'scope_value_2'],
)

# Make the request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ async def sample_tail_log_entries():

# Initialize request argument(s)
request = logging_v2.TailLogEntriesRequest(
resource_names=['resource_names_value'],
resource_names=['resource_names_value_1', 'resource_names_value_2'],
)

# Make the request
stream = await client.tail_log_entries([resource_names=['resource_names_value']])
stream = await client.tail_log_entries([resource_names=['resource_names_value_1', 'resource_names_value_2']])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize this was broken earlier, but it is now tracked: #1014

async for response in stream:
print(response)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def sample_tail_log_entries():

# Initialize request argument(s)
request = logging_v2.TailLogEntriesRequest(
resource_names=['resource_names_value'],
resource_names=['resource_names_value_1', 'resource_names_value_2'],
)

# Make the request
stream = client.tail_log_entries([resource_names=['resource_names_value']])
stream = client.tail_log_entries([resource_names=['resource_names_value_1', 'resource_names_value_2']])
for response in stream:
print(response)

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/schema/wrappers/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ def test_mock_value_repeated():

def test_mock_value_original_type_repeated():
field = make_field(name='foo_bar', type='TYPE_STRING', label=3)
assert field.mock_value_original_type == ["foo_bar_value"]
assert field.mock_value_original_type == [
"foo_bar_value_1", "foo_bar_value_2"]


def test_mock_value_map():
Expand Down