Skip to content

Commit

Permalink
[BUG] Fixed pipeline and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton committed Apr 30, 2024
1 parent f83ffe9 commit 226ad5a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ skip-magic-trailing-comma = false
line-ending = "auto"

[lint.per-file-ignores]
"tests/test_collect_data.py" = ["S101"]
"tests/*" = ["S101"]
1 change: 1 addition & 0 deletions backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Need."""
9 changes: 1 addition & 8 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module for running frontend part."""
# import json
import sqlite3
import streamlit as st
import pandas as pd
Expand Down Expand Up @@ -83,12 +82,6 @@ def print_statistics(title: str, db: sqlite3) -> None :
if host_to_monitor:
print_statistics(host_to_monitor, metrics_repo)







# def load_as_json(data: str) -> None:
# """Loads data as json."""
# json_data = json.dumps(data)
Expand All @@ -97,4 +90,4 @@ def print_statistics(title: str, db: sqlite3) -> None :
# # print_statistics(title)
# st.dataframe({"site": ["ya.ru", ""]})
# if st.button('Load data'):
# load_as_json()
# load_as_json()
Empty file removed backend/controller.py
Empty file.
3 changes: 2 additions & 1 deletion backend/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __enter__(self: Self) -> Self:
def save_record(self: Self, host: str, record: dict) -> None:
"""Add new record to metrics."""
acc = dict(record['accessibility'])
current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # pragma: no mutate
# pragma: no mutate
current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.conn.execute('''INSERT INTO metrics
(host, datetime, loss, latency,
http, https, imap, smtp, ssh, dns)
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Need."""
9 changes: 5 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from unittest.mock import patch, MagicMock
import pytest
from backend.main import *
"""Module to test main.py."""
from unittest.mock import patch
from backend.main import collect_and_append_data


@patch('backend.collect_data')
def test_collect_and_append_data(mock_collect_data):
def test_collect_and_append_data(mock_collect_data: {}) -> None:
"""Do test collect_and_append_data() function."""
host = "example.com"
record = {
'accessibility': [('HTTP', 'open'),
Expand Down
21 changes: 14 additions & 7 deletions tests/test_persistence.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Tests for persistence.py."""
import os
from datetime import datetime

Expand All @@ -8,8 +9,9 @@

# Fixture to create an in-memory SQLite database for testing
@pytest.fixture
def in_memory_db():
def delete_file(file_path):
def in_memory_db() -> None:
"""Do fixture to create an in-memory SQLite database."""
def delete_file(file_path: str) -> None :
if os.path.exists(file_path):
os.remove(file_path)

Expand All @@ -18,7 +20,8 @@ def delete_file(file_path):


# Test cases for MetricsRepository class
def test_save_record(in_memory_db):
def test_save_record(in_memory_db: str) -> None:
"""Do test cases for MetricsRepository class."""
with MetricsRepository(in_memory_db) as metrics_repo:
record = {
"loss": 0.0,
Expand Down Expand Up @@ -46,23 +49,26 @@ def test_save_record(in_memory_db):
assert result[10] == 'open'


def test_add_host(in_memory_db):
def test_add_host(in_memory_db: str) -> None:
"""Do test add_host()."""
with MetricsRepository(in_memory_db) as metrics_repo:
metrics_repo.add_host('example.com')
cursor = metrics_repo.conn.execute('''SELECT * FROM hosts''')
result = cursor.fetchone()
assert result[0] == 'example.com'


def test_get_hosts(in_memory_db):
def test_get_hosts(in_memory_db: str) -> None:
"""Do test get_host()."""
with MetricsRepository(in_memory_db) as metrics_repo:
metrics_repo.add_host('example.com')
hosts = metrics_repo.get_hosts()
assert len(hosts) == 1
assert hosts[0][0] == 'example.com'


def test_get_metrics(in_memory_db):
def test_get_metrics(in_memory_db: str) -> None:
"""Do test get_metrics()."""
with MetricsRepository(in_memory_db) as metrics_repo:
current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
metrics_repo.conn.execute('''INSERT INTO metrics
Expand Down Expand Up @@ -94,7 +100,8 @@ def test_get_metrics(in_memory_db):
assert metrics[0][10] == 'open'


def test_delete_host(in_memory_db):
def test_delete_host(in_memory_db: str) -> None:
"""Do test delete_host()."""
with MetricsRepository(in_memory_db) as metrics_repo:
# Add a host to the database
metrics_repo.add_host('example.com')
Expand Down

0 comments on commit 226ad5a

Please sign in to comment.