Test data, template for TCP unittest

This commit is contained in:
DarkCat09 2024-05-31 15:58:46 +04:00
parent e51370ca6e
commit 2ce161205c
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
3 changed files with 59 additions and 0 deletions

19
fixtures.py Normal file
View file

@ -0,0 +1,19 @@
import time
from models import SensorData
cur_time = int(time.time())
device_mac = '9B-89-A9-ED-F2-FB'
device_name = 'My RaspberryPi 4B'
sensors = [
SensorData('owner', 'darkcat09'),
SensorData('lat', 56), # just random data
SensorData('lon', 49),
SensorData('alt', 400),
SensorData('t1', 12.42, cur_time), # let it be temp outdoors
SensorData('t2', 22.50, cur_time, 'Temp in living room'),
SensorData('t3', 60.11, None, 'Poor Raspberry CPU...'),
SensorData('p1', 752.33, None, 'Barometer'),
SensorData('p2', 751.42),
]

30
main.py Normal file
View file

@ -0,0 +1,30 @@
import socket
import urllib
from unittest import TestCase
from fixtures import device_mac, device_name, sensors
TCP_ADDR = ('127.0.0.1', 8283)
UDP_ADDR = ('127.0.0.1', 8283)
HTTP_URL = 'http://127.0.0.1:8080'
class TestTCP(TestCase):
def setUp(self) -> None:
self.sock = socket.socket()
self.sock.connect(TCP_ADDR)
def test_nm(self) -> None:
self.sock.sendall(b'') # TODO
self._wait_for_ok()
def test_json(self) -> None:
self.sock.sendall(b'') # TODO
self._wait_for_ok()
def _wait_for_ok(self) -> None:
data = self.sock.recv(1024)
self.assertEqual(data, b'OK')

10
models.py Normal file
View file

@ -0,0 +1,10 @@
from dataclasses import dataclass
from typing import Any
@dataclass
class SensorData:
mac: str
value: Any
time: int | None = None
name: str | None = None