Skip to content

Commit

Permalink
Use Factory boy for manifest generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-melnacouzi committed Nov 15, 2024
1 parent 8e1cfb0 commit ef27032
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 42 deletions.
30 changes: 30 additions & 0 deletions tests/nativeapp/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,33 @@ class ProjectV11Factory(ProjectV10Factory):

class ProjectV2Factory(_ProjectFactory):
pdf = factory.SubFactory(PdfV2Factory)


class ManifestFactory(factory.DictFactory):
version = factory.Dict(
{
"name": factory.Faker("word"),
"label": factory.Faker("word"),
"comment": factory.Faker("sentence"),
}
)
artifacts = factory.Dict(
{
"setup_script": "setup.sql",
"extension_code": True,
"readme": "README.md",
}
)
configuration = factory.Dict(
{
"log_level": "fatal",
"trace_level": "always",
"telemetry_event_definitions": None,
}
)

@classmethod
def _create(cls, model_class, *args, **kwargs) -> str:
res = cls._build(model_class, *args, **kwargs)

return yaml.dump(res)
79 changes: 37 additions & 42 deletions tests_integration/nativeapp/test_event_sharing.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import yaml

from tests.nativeapp.factories import (
ApplicationEntityModelFactory,
ApplicationPackageEntityModelFactory,
ManifestFactory,
ProjectV2Factory,
)
from tests_integration.test_utils import row_from_snowflake_session


def build_manifest(configuration: dict):
manifest_json = {
"manifest_version": "1",
"artifacts": {
"setup_script": "setup.sql",
"readme": "README.md",
},
"configuration": configuration,
}

return yaml.dump(manifest_json)


@pytest.fixture
def events_assertion(snowflake_session, resource_suffix):
def _events_assertion(events):
Expand Down Expand Up @@ -56,13 +57,11 @@ def assert_events_in_app(snowflake_session, resource_suffix, events):
def test_given_event_sharing_with_mandatory_events_and_sharing_allowed_then_success(
temp_dir, runner, events_assertion, nativeapp_teardown
):
manifest_yml = build_manifest(
{
"telemetry_event_definitions": [
{"type": "ERRORS_AND_WARNINGS", "sharing": "MANDATORY"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
}
manifest_yml = ManifestFactory(
configuration__telemetry_event_definitions=[
{"type": "ERRORS_AND_WARNINGS", "sharing": "MANDATORY"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
)
ProjectV2Factory(
pdf__entities=dict(
Expand Down Expand Up @@ -112,13 +111,11 @@ def test_given_event_sharing_with_mandatory_events_and_sharing_allowed_then_succ
def test_given_event_sharing_with_mandatory_events_and_sharing_not_allowed_then_error(
temp_dir, runner, nativeapp_teardown
):
manifest_yml = build_manifest(
{
"telemetry_event_definitions": [
{"type": "ERRORS_AND_WARNINGS", "sharing": "MANDATORY"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
}
manifest_yml = ManifestFactory(
configuration__telemetry_event_definitions=[
{"type": "ERRORS_AND_WARNINGS", "sharing": "MANDATORY"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
)
ProjectV2Factory(
pdf__entities=dict(
Expand Down Expand Up @@ -152,14 +149,13 @@ def test_given_event_sharing_with_mandatory_events_and_sharing_not_allowed_then_
def test_given_event_sharing_with_no_mandatory_events_and_sharing_not_allowed_then_success(
temp_dir, runner, events_assertion, nativeapp_teardown
):
manifest_yml = build_manifest(
{
"telemetry_event_definitions": [
{"type": "ERRORS_AND_WARNINGS", "sharing": "OPTIONAL"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
}
manifest_yml = ManifestFactory(
configuration__telemetry_event_definitions=[
{"type": "ERRORS_AND_WARNINGS", "sharing": "OPTIONAL"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
)

ProjectV2Factory(
pdf__entities=dict(
pkg=ApplicationPackageEntityModelFactory(
Expand Down Expand Up @@ -205,14 +201,13 @@ def test_given_event_sharing_with_no_mandatory_events_and_sharing_not_allowed_th
def test_given_event_sharing_with_no_mandatory_events_and_sharing_is_allowed_then_success(
temp_dir, runner, events_assertion, nativeapp_teardown
):
manifest_yml = build_manifest(
{
"telemetry_event_definitions": [
{"type": "ERRORS_AND_WARNINGS", "sharing": "OPTIONAL"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
}
manifest_yml = ManifestFactory(
configuration__telemetry_event_definitions=[
{"type": "ERRORS_AND_WARNINGS", "sharing": "OPTIONAL"},
{"type": "DEBUG_LOGS", "sharing": "OPTIONAL"},
]
)

ProjectV2Factory(
pdf__entities=dict(
pkg=ApplicationPackageEntityModelFactory(
Expand Down

0 comments on commit ef27032

Please sign in to comment.