Skip to content

Commit

Permalink
qase-python-commons: add more debug logs
Browse files Browse the repository at this point in the history
Added a base class for all models. It helps to serialize the models to json.

Added logs before and after important functions in the common package.
  • Loading branch information
gibiw committed Apr 29, 2024
1 parent b126422 commit 0183fdb
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 38 deletions.
2 changes: 1 addition & 1 deletion qase-python-commons/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "qase-python-commons"
version = "3.0.0"
version = "3.0.1"
description = "A library for Qase TestOps and Qase Report"
readme = "README.md"
authors = [{name = "Qase Team", email = "[email protected]"}]
Expand Down
3 changes: 3 additions & 0 deletions qase-python-commons/src/qase/commons/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ def _get_config(self, key):
for key in keys[:-1]:
config = config.get(key, {})
return config.get(keys[-1], None)

def __str__(self):
return json.dumps(self.config, indent=4, sort_keys=True)
4 changes: 4 additions & 0 deletions qase-python-commons/src/qase/commons/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def log(self, message: str, level: str = 'info'):
with open(self.log_file, 'a') as f:
f.write(log)

def log_debug(self, message: str):
if self.debug:
self.log(message, 'debug')

@staticmethod
def _get_timestamp(format: str = "%Y%m%d_%H:%M:%S"):
now = datetime.datetime.now()
Expand Down
8 changes: 5 additions & 3 deletions qase-python-commons/src/qase/commons/models/attachment.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import uuid
from typing import Optional, Union
from io import BytesIO, StringIO
import json
import pathlib

from typing import Optional, Union
from io import BytesIO, StringIO
from .basemodel import BaseModel


class Attachment:
class Attachment(BaseModel):
def __init__(self,
file_name: str,
mime_type: str,
Expand Down
7 changes: 7 additions & 0 deletions qase-python-commons/src/qase/commons/models/basemodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json


class BaseModel:
def __str__(self) -> str:
return json.dumps(self, default=lambda o: o.__dict__ if hasattr(o, '__dict__') else str(o), indent=4,
sort_keys=True)
7 changes: 5 additions & 2 deletions qase-python-commons/src/qase/commons/models/relation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class RelationSuite(object):
from .basemodel import BaseModel


class RelationSuite(BaseModel):
def __init__(self, suite_id: int, title: str) -> None:
self.suite_id = suite_id
self.title = title


class Relation(object):
class Relation(BaseModel):
def __init__(self, type: str, data: RelationSuite):
self.type = type
self.data = data
18 changes: 8 additions & 10 deletions qase-python-commons/src/qase/commons/models/result.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from typing import Type, Optional, Union, Dict, List
from pathlib import PosixPath
import time
import uuid
import json

from typing import Type, Optional, Union, Dict, List
from pathlib import PosixPath
from .basemodel import BaseModel
from .step import Step
from .suite import Suite
from .attachment import Attachment
from .relation import Relation
from .. import QaseUtils


class Field:
class Field(BaseModel):
def __init__(self,
name: str,
value: Union[str, list]):
self.name = name
self.value = value


class Execution(object):
class Execution(BaseModel):
def __init__(self,
status: Optional[str] = None,
end_time: int = 0,
Expand Down Expand Up @@ -47,7 +49,7 @@ def complete(self):
self.duration = (int)((self.end_time - self.start_time) * 1000)


class Request(object):
class Request(BaseModel):
def __init__(self,
method: str,
url: str,
Expand All @@ -65,7 +67,7 @@ def __init__(self,
self.response_body = response_body


class Result(object):
class Result(BaseModel):
def __init__(self, title: str, signature: str) -> None:
self.id: str = str(uuid.uuid4())
self.title: str = title
Expand Down Expand Up @@ -137,7 +139,3 @@ def get_suite_title(self) -> Optional[str]:

def set_run_id(self, run_id: str) -> None:
self.run_id = run_id

def to_json(self) -> str:
return json.dumps(self, default=lambda o: o.__str__() if isinstance(o, PosixPath) else o.__dict__,
sort_keys=False, indent=4)
12 changes: 6 additions & 6 deletions qase-python-commons/src/qase/commons/models/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json

from typing import Optional, List

from .basemodel import BaseModel


class RunExecution(object):
class RunExecution(BaseModel):
def __init__(self,
start_time: float,
end_time: float,
Expand All @@ -17,7 +20,7 @@ def track(self, result: dict):
self.cumulative_duration += result["execution"]["duration"]


class RunStats(object):
class RunStats(BaseModel):
def __init__(self) -> None:
self.passed = 0
self.failed = 0
Expand All @@ -41,7 +44,7 @@ def track(self, result: dict):
self.muted += 1


class Run(object):
class Run(BaseModel):
def __init__(self,
title: str,
start_time: float,
Expand All @@ -60,9 +63,6 @@ def __init__(self,
self.suites = suites
self.environment = environment

def to_json(self) -> str:
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=4)

def add_result(self, result: dict):
compact_result = {
"id": result["id"],
Expand Down
22 changes: 12 additions & 10 deletions qase-python-commons/src/qase/commons/models/step.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from enum import Enum
from typing import Optional, Union, Dict, List, Type
import time
import uuid

from enum import Enum
from typing import Optional, Union, Dict, List, Type
from .attachment import Attachment
from .basemodel import BaseModel


class StepType(Enum):
Expand All @@ -14,27 +16,27 @@ class StepType(Enum):
SLEEP = 'sleep'


class StepTextData(object):
class StepTextData(BaseModel):
def __init__(self, action: str, expected_result: Optional[str] = None):
self.action = action
self.expected_result = expected_result


class StepAssertData(object):
class StepAssertData(BaseModel):
def __init__(self, expected: str, actual: str, message: str):
self.expected = expected
self.actual = actual
self.message = message


class StepGherkinData(object):
class StepGherkinData(BaseModel):
def __init__(self, keyword: str, name: str, line: int):
self.keyword = keyword
self.name = name
self.line = line


class StepRequestData(object):
class StepRequestData(BaseModel):
def __init__(self, request_body: str, request_headers: Dict[str, str], request_method: str, request_url: str):
self.response_headers = None
self.response_body = None
Expand All @@ -60,17 +62,17 @@ def add_response(self, status_code: int, response_body: Optional[str] = None,
self.response_headers = response_headers


class StepDbQueryData(object):
class StepDbQueryData(BaseModel):
def __init__(self, query: str, expected_result: str):
self.query = query


class StepSleepData(object):
class StepSleepData(BaseModel):
def __init__(self, duration: int):
self.duration = duration


class StepExecution(object):
class StepExecution(BaseModel):
def __init__(self, status: Optional[str] = 'untested', end_time: int = 0, duration: int = 0):
self.start_time = time.time()
self.status = status
Expand All @@ -88,7 +90,7 @@ def complete(self):
self.duration = int((self.end_time - self.start_time) * 1000)


class Step(object):
class Step(BaseModel):
def __init__(self,
step_type: StepType,
id: Optional[str],
Expand Down
6 changes: 4 additions & 2 deletions qase-python-commons/src/qase/commons/models/suite.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import List, Optional
import uuid

from typing import List, Optional

from .basemodel import BaseModel

class Suite():

class Suite(BaseModel):
def __init__(self, title: str, description: Optional[str] = None, parent_id: Optional[str] = None):
self.title = title
self.description = description
Expand Down
18 changes: 14 additions & 4 deletions qase-python-commons/src/qase/commons/reporters/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
import json
import time

from ..config import ConfigManager as Config
from ..logger import Logger

Expand All @@ -7,10 +11,6 @@
from ..models import Result, Attachment, Runtime
from typing import Union

import os
import json
import time

"""
CoreReporter is a facade for all reporters and it is used to initialize and manage them.
It is also used to pass configuration and logger to reporters, handle fallback logic and error handling.
Expand All @@ -28,6 +28,8 @@ def __init__(self, config: Config):
self._selective_execution_setup()
self.fallback = self._fallback_setup()

self.logger.log_debug(f"Config: {self.config}")

# Reading reporter mode from config file
mode = config.get("mode", "off")

Expand All @@ -48,7 +50,9 @@ def start_run(self) -> Union[str, None]:
if self.reporter:
try:
ts = time.time()
self.logger.log_debug("Starting run")
run_id = self.reporter.start_run()
self.logger.log_debug(f"Run ID: {run_id}")
self.overhead += time.time() - ts
return run_id
except Exception as e:
Expand All @@ -60,7 +64,9 @@ def complete_run(self, exit_code=None) -> None:
if self.reporter:
try:
ts = time.time()
self.logger.log_debug("Completing run")
self.reporter.complete_run(exit_code)
self.logger.log_debug("Run completed")
self.overhead += time.time() - ts
self.logger.log(f"Overhead for Qase Report: {round(self.overhead * 1000)}ms", 'info')
except Exception as e:
Expand All @@ -72,7 +78,9 @@ def add_result(self, result: Result) -> None:
if self.reporter:
try:
ts = time.time()
self.logger.log_debug(f"Adding result {result}")
self.reporter.add_result(result)
self.logger.log_debug(f"Result {result.get_title()} added")
self.overhead += time.time() - ts
except Exception as e:
# Log error, disable reporting and continue
Expand All @@ -84,7 +92,9 @@ def add_attachment(self, attachment: Attachment) -> None:
if self.reporter:
try:
ts = time.time()
self.logger.log_debug(f"Adding attachment {attachment}")
self.reporter.add_attachment(attachment)
self.logger.log_debug(f"Attachment {attachment.id} added")
self.overhead += time.time() - ts
except Exception as e:
# Log error and run fallback
Expand Down
Loading

0 comments on commit 0183fdb

Please sign in to comment.