Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Add tests and prepare for merge with issue #441
Browse files Browse the repository at this point in the history
  • Loading branch information
GideonKoenig committed Apr 29, 2022
1 parent e83df6e commit 784d447
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,16 @@ def __get_default_type_from_value(default_value: str) -> tuple[str, str]:


def __get_required_annotations(usages: UsageStore, api: API) -> dict[str, dict[str, dict[str, str]]]:
"""
Returns all required annotations
:param usages: Usage store
:param api: Description of the API
"""
result = {}

parameters = (api.parameters())
# Takes all parameters with default value
optional_parameter = [(it, parameters[it]) for it in parameters if parameters[it].default_value is not None]

for qname, parameter in optional_parameter:
Expand All @@ -208,6 +215,11 @@ def __get_required_annotations(usages: UsageStore, api: API) -> dict[str, dict[s


def __get_parameter_type(values: list[tuple[str, int]]) -> (ParameterType, str):
"""
Returns a tuple of the parameter Typ and parameter value
:param values: list of tuples where the first value represents the parameter name and the second represents the count of occurrences
"""
if len(values) == 0:
return ParameterType.Unused, None
elif len(values) == 1:
Expand All @@ -216,9 +228,9 @@ def __get_parameter_type(values: list[tuple[str, int]]) -> (ParameterType, str):
n = len(values)
m = sum([count for value, count in values])

most_used_value, seconds_most_used_value = sorted(values, key=lambda tup: tup[1])[:2]
seconds_most_used_value, most_used_value = sorted(values, key=lambda tup: tup[1])[-2:]

if most_used_value[1] - seconds_most_used_value[1] <= n/m:
if most_used_value[1] - seconds_most_used_value[1] <= m/n:
return ParameterType.Required, None
else:
return ParameterType.Optional, most_used_value[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
import os
import pytest

from package_parser.commands.find_usages import UsageStore
from package_parser.commands.generate_annotations.generate_annotations import (
generate_annotations, __get_unused_annotations, __get_constant_annotations, ____qname_to_target_name,
_preprocess_usages)
from package_parser.commands.get_api import API

REQUIRED_EXPECTED = {"constant": {
'test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_required':
{'target': 'test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_required'},
'test/test/commonly_used_global_required_and_optional_function/required_that_should_be_required':
{'target': 'test/test/commonly_used_global_required_and_optional_function/required_that_should_be_required'},
'test/test/commonly_used_global_required_and_optional_function/commonly_used_barely_required':
{'target': 'test/test/commonly_used_global_required_and_optional_function/commonly_used_barely_required'}}
}


# Reihenfolge ist wichtig, siehe Reihenfolge von annotation_functions in generate_annotations.py
FULL_EXPECTED = {**UNUSED_EXPECTED, **CONSTANT_EXPECTED, **REQUIRED_EXPECTED}

def test_required_annotation():
pass
209 changes: 0 additions & 209 deletions package-parser/tests/data/requried/usage_data.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"classes": [],
"functions": [
"test.unused_global_function",
"test.commonly_used_global_function"
"test.commonly_used_global_required_and_optional_function"
]
}
],
Expand All @@ -24,24 +24,14 @@
"decorators": [],
"parameters": [
{
"name": "unused_required_parameter",
"name": "unused_parameter",
"default_value": null,
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
"type": "str",
"description": ""
}
},
{
"name": "unused_optional_parameter",
"default_value": "'bla'",
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
"type": "str",
"description": ""
}
}
],
"results": [],
Expand All @@ -51,14 +41,24 @@
"source_code": ""
},
{
"name": "commonly_used_global_function",
"unique_name": "commonly_used_global_function",
"qname": "test.commonly_used_global_function",
"unique_qname": "test.commonly_used_global_function",
"name": "commonly_used_global_required_and_optional_function",
"unique_name": "commonly_used_global_required_and_optional_function",
"qname": "test.commonly_used_global_required_and_optional_function",
"unique_qname": "test.commonly_used_global_required_and_optional_function",
"decorators": [],
"parameters": [
{
"name": "useless_required_parameter",
"name": "optional_that_should_be_required",
"default_value": "'test'",
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
"type": "str",
"description": ""
}
},
{
"name": "required_that_should_be_required",
"default_value": null,
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
Expand All @@ -68,7 +68,7 @@
}
},
{
"name": "useful_required_parameter",
"name": "required_that_should_be_optional",
"default_value": null,
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
Expand All @@ -78,8 +78,18 @@
}
},
{
"name": "unused_optional_parameter",
"default_value": "'bla'",
"name": "optional_that_should_be_optional",
"default_value": "'captain_morgan'",
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
"type": "str",
"description": ""
}
},
{
"name": "commonly_used_almost_required",
"default_value": "'marvel'",
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
Expand All @@ -88,8 +98,8 @@
}
},
{
"name": "useless_optional_parameter",
"default_value": "'bla'",
"name": "commonly_used_barely_required",
"default_value": "'otto'",
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
Expand All @@ -98,8 +108,8 @@
}
},
{
"name": "useful_optional_parameter",
"default_value": "'bla'",
"name": "constant_parameter",
"default_value": null,
"is_public": true,
"assigned_by": "POSITION_OR_NAME",
"docstring": {
Expand Down
Loading

0 comments on commit 784d447

Please sign in to comment.