Skip to content

Commit

Permalink
Merge pull request #10 from cloudblue/LITE-create-entity-api-represen…
Browse files Browse the repository at this point in the history
…tation-for-configuration-object

LITE-27890 Add entity api representation to configuration objects
  • Loading branch information
jonatrios authored Jul 7, 2023
2 parents 7fad1ad + 4a185f9 commit abd62a4
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 19 deletions.
10 changes: 5 additions & 5 deletions connect_ext_ppr/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from connect_ext_ppr.db import Model
from connect_ext_ppr.models.deployment import Deployment
from connect_ext_ppr.models.enums import ConfigurationStateChices
from connect_ext_ppr.models.enums import ConfigurationStateChoices
from connect_ext_ppr.models.file import File


Expand All @@ -17,16 +17,16 @@ class Configuration(Model):
file = db.Column(db.ForeignKey(File.id))
deployment = db.Column(db.ForeignKey(Deployment.id))
state = db.Column(
db.Enum(ConfigurationStateChices, validate_strings=True),
default=ConfigurationStateChices.INACTIVE,
db.Enum(ConfigurationStateChoices, validate_strings=True),
default=ConfigurationStateChoices.INACTIVE,
)
created_at = db.Column(db.DateTime(), default=datetime.utcnow)
created_by = db.Column(db.String(20))
updated_at = db.Column(db.DateTime(), default=datetime.utcnow)
updated_by = db.Column(db.String(20))

def activate(self):
self.state = ConfigurationStateChices.ACTIVE
self.state = ConfigurationStateChoices.ACTIVE

def deleted(self):
self.state = ConfigurationStateChices.DELETED
self.state = ConfigurationStateChoices.DELETED
2 changes: 1 addition & 1 deletion connect_ext_ppr/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MimeTypeChoices(str, enum.Enum):
application_vnd_ms_xslx = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'


class ConfigurationStateChices(str, enum.Enum):
class ConfigurationStateChoices(str, enum.Enum):
ACTIVE = 'active'
INACTIVE = 'inactive'
DELETED = 'deleted'
29 changes: 22 additions & 7 deletions connect_ext_ppr/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@

from pydantic import BaseModel

from connect_ext_ppr.models.enums import DeploymentStatusChoices
from connect_ext_ppr.models.enums import ConfigurationStateChoices, DeploymentStatusChoices


class Vendor(BaseModel):
class VendorSchema(BaseModel):
id: str
name: str
icon: Optional[str]


class Product(BaseModel):
class ProductSchema(BaseModel):
id: str
name: str
icon: Optional[str]


class Hub(BaseModel):
class HubSchema(BaseModel):
id: str
name: str


class DeploymentSchema(BaseModel):
id: str
product: Product
hub: Hub
product: ProductSchema
hub: HubSchema
account_id: str
owner: Vendor
owner: VendorSchema
last_sync_at: datetime
status: DeploymentStatusChoices
events: Dict[str, Dict[str, Union[datetime, str]]]
Expand All @@ -45,3 +45,18 @@ class DeploymentRequestSchema(BaseModel):

class Config:
orm_mode = True


class FileSchema(BaseModel):
id: str
name: str
location: str
size: int


class ConfigurationSchema(BaseModel):
id: str
file: FileSchema
deployment: Dict[str, str]
state: ConfigurationStateChoices
events: Dict[str, Dict[str, Union[datetime, str]]]
17 changes: 17 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from connect_ext_ppr.db import get_db, get_db_ctx_manager, VerboseBaseSession


def test_get_db(engine):
db = next(get_db(engine))
assert isinstance(db, VerboseBaseSession)
assert db.connection().connect()


def test_get_db_ctx_manager(mocker, engine):
mocker.patch(
'connect_ext_ppr.db.get_engine',
return_value=engine,
)
with get_db_ctx_manager({}) as db:
assert isinstance(db, VerboseBaseSession)
assert db.connection().connect()
66 changes: 61 additions & 5 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@

import pytest

from connect_ext_ppr.models.deployment import DeploymentStatusChoices
from connect_ext_ppr.schemas import DeploymentSchema, Hub, Product, Vendor
from connect_ext_ppr.models.enums import (
ConfigurationStateChoices,
DeploymentStatusChoices,
)
from connect_ext_ppr.schemas import (
ConfigurationSchema,
DeploymentSchema,
FileSchema,
HubSchema,
ProductSchema,
VendorSchema,
)


@pytest.mark.parametrize(
Expand All @@ -16,17 +26,17 @@ def test_deployment_schema(status):
iso = now.isoformat()
serializer = DeploymentSchema(
id='DPL-000-000-000',
product=Product(
product=ProductSchema(
id='PRD-000-000-000',
name='Some',
icon='/media/VA-000-000/PRD-000-000-000/media/PRD-000-000-000-logo_cLqk6Vm.png',
),
hub=Hub(
hub=HubSchema(
id='HB-1111-2222',
name='Hub Hub',
),
account_id='PA-000-000',
owner=Vendor(
owner=VendorSchema(
id='VA-000-000',
name='Vendor',
icon='/media/VA-000-000/media/icon.png',
Expand Down Expand Up @@ -61,3 +71,49 @@ def test_deployment_schema(status):
},
},
})


@pytest.mark.parametrize(
'state',
ConfigurationStateChoices,
)
def test_configuration_schema(state, file):
now = datetime.utcnow()
iso = now.isoformat()
serializer = ConfigurationSchema(
id='CFL-000-000-000',
file=FileSchema(
id=file.id,
name=file.name,
location=file.location,
size=file.size,
),
deployment={'id': 'DPL-000-000-000'},
state=state,
events={
'created': {'at': now, 'by': 'SU-295-689-628'},
'updated': {'at': now, 'by': 'SU-295-689-628'},
},
)
json = serializer.json()
assert json == j.dumps({
"id": "CFL-000-000-000",
"file": {
"id": file.id,
"name": file.name,
"location": file.location,
"size": file.size,
},
"deployment": {"id": "DPL-000-000-000"},
"state": state,
"events": {
"created": {
"at": iso,
"by": "SU-295-689-628",
},
"updated": {
"at": iso,
"by": "SU-295-689-628",
},
},
})
40 changes: 39 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import copy

import pytest
from connect.client import ClientError
from connect.client.rql import R

from connect_ext_ppr.utils import _process_exc, get_all_info
from connect_ext_ppr.utils import (
_process_exc,
filter_object_list_by_id,
get_all_info,
get_marketplaces,
)


def test_process_exc():
Expand Down Expand Up @@ -39,3 +46,34 @@ def test_get_all_info_success(
all_info = get_all_info(connect_client)
listing['contract']['marketplace'] = marketplace
assert all_info[0] == listing


def test_get_marketplaces(
connect_client,
marketplace,
client_mocker_factory,
):

client_mocker = client_mocker_factory(base_url=connect_client.endpoint)
client_mocker.marketplaces.filter(R().id.in_([marketplace['id']])).mock(
return_value=[marketplace],
)
mkps = get_marketplaces(connect_client, [marketplace['id']])

assert list(mkps) == [marketplace]


def test_filter_objects(marketplace):
mkp_2 = copy.copy(marketplace)
mkp_2['id'] = 'MP-XXXX'
mkp_list = [marketplace, mkp_2]

assert filter_object_list_by_id(mkp_list, mkp_2['id']) == mkp_2


def test_filter_key_error(marketplace):
mkp_list = [marketplace]

with pytest.raises(KeyError) as ex:
filter_object_list_by_id(mkp_list, 'MP-XXXX')
assert str(ex.value).startswith("'MP-XXXX'")

0 comments on commit abd62a4

Please sign in to comment.