Verified Commit 13639c90 authored by Sli's avatar Sli
Browse files

server: beginning of testing architecture

parent f145ec60
......@@ -8,4 +8,12 @@ pip install -r requirements.txt
./manage.py protoc # Generate protobuf files
./manage.py setup --import 2019.json # Generate database
./manage.py runserver
```
# Testing
```bash
./manage.py test # Launch all tests
./manage.py test TestBalance # Only launch test cases of TestBalance class
./manage.py test TestBalance.test_existing_user # Only test one method of TestBalance class
```
\ No newline at end of file
......@@ -31,6 +31,20 @@ def runserver(host, port, reflect):
server.serve(host, port, reflect)
@default_group.command(name="test", help="Run tests")
@click.argument("name", type=str, required=False)
def test(name=None):
import unittest
loader = unittest.TestLoader()
if name is None:
suite = loader.discover("", pattern="tests.py")
else:
suite = loader.loadTestsFromName("server.tests.%s" % name)
unittest.TextTestRunner(verbosity=2).run(suite)
@default_group.command(name="protoc", help="Generate protoc files")
def protoc():
from subprocess import Popen
......
grpcio==1.29
grpcio-tools==1.29
grpcio-reflection==1.29
grpcio-testing==1.29
SQLAlchemy==1.3.17
click==7.1.2
jsonschema
......
#!/usr/bin/env python3
# -*- coding:utf-8 -*
from google.protobuf.timestamp_pb2 import Timestamp
import decimal
from server import models, com_pb2
def pb_now() -> Timestamp:
timestamp = Timestamp()
timestamp.GetCurrentTime()
return timestamp
def date_to_pb(date) -> Timestamp:
timestamp = Timestamp()
timestamp.FromDatetime(date)
return timestamp
def decimal_to_pb_money(dec: decimal.Decimal) -> com_pb2.Money:
return com_pb2.Money(amount=str(dec))
def pb_money_to_decimal(money: com_pb2.Money) -> decimal.Decimal:
try:
return decimal.Decimal(money.amount)
except:
return decimal.Decimal(0)
def refilling_to_pb(refilling: models.Refilling) -> com_pb2.Refilling:
return com_pb2.Refilling(
id=refilling.id,
customer_id=refilling.customer_id,
counter_id=refilling.counter_id,
device_uuid=refilling.machine_id,
payment_method=refilling.payment_method.id,
amount=decimal_to_pb_money(refilling.amount),
cancelled=refilling.cancelled,
date=date_to_pb(refilling.date),
)
......@@ -9,42 +9,13 @@ from concurrent import futures
from google.protobuf.timestamp_pb2 import Timestamp
from server import db, models, com_pb2, com_pb2_grpc
def pb_now() -> Timestamp:
timestamp = Timestamp()
timestamp.GetCurrentTime()
return timestamp
def date_to_pb(date) -> Timestamp:
timestamp = Timestamp()
timestamp.FromDatetime(date)
return timestamp
def decimal_to_pb_money(dec: decimal.Decimal) -> com_pb2.Money:
return com_pb2.Money(amount=str(dec))
def pb_money_to_decimal(money: com_pb2.Money) -> decimal.Decimal:
try:
return decimal.Decimal(money.amount)
except:
return decimal.Decimal(0)
def refilling_to_pb(refilling: models.Refilling) -> com_pb2.Refilling:
return com_pb2.Refilling(
id=refilling.id,
customer_id=refilling.customer_id,
counter_id=refilling.counter_id,
device_uuid=refilling.machine_id,
payment_method=refilling.payment_method.id,
amount=decimal_to_pb_money(refilling.amount),
cancelled=refilling.cancelled,
date=date_to_pb(refilling.date),
)
from server.pbutils import (
pb_now,
date_to_pb,
decimal_to_pb_money,
pb_money_to_decimal,
refilling_to_pb,
)
def get_or_create_machine(_uuid: str) -> models.Machine:
......
#!/usr/bin/env python3
# -*- coding:utf-8 -*
import unittest
import grpc
from google.protobuf.timestamp_pb2 import Timestamp
from grpc_testing import server_from_dictionary, strict_real_time
from server import db, models, com_pb2, com_pb2_grpc
from server.pbutils import (
pb_now,
date_to_pb,
decimal_to_pb_money,
pb_money_to_decimal,
refilling_to_pb,
)
class PaymentProtocolTestCase(unittest.TestCase):
def __init__(self, method_name):
super().__init__(method_name)
servicers = {
com_pb2.DESCRIPTOR.services_by_name[
"PaymentProtocol"
]: com_pb2_grpc.PaymentProtocolServicer()
}
self.test_server = server_from_dictionary(servicers, strict_real_time())
def setUp(self):
# Create database
pass
class TestBalance(PaymentProtocolTestCase):
def test_non_existing_user(self):
print("HELLO")
self.assertEqual("hello", "hello")
def test_existing_user(self):
print("HELLO2")
self.assertEqual("hello", "hello")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment