Skip to content

Commit

Permalink
Update tests to handle timestamp server
Browse files Browse the repository at this point in the history
  • Loading branch information
tnaccarato committed Apr 15, 2024
1 parent 92be663 commit 238addd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
17 changes: 17 additions & 0 deletions custom_admin/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
from threading import Thread
from unittest.mock import patch

from django.contrib.auth.models import User, Group
from django.test import TestCase, Client
from django.urls import reverse
from payapp.models import Account, Transfer, Request
from register.forms import UserForm
from timestamp_server import thrift_server


class CustomAdminViewTests(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Start the Thrift server in a separate thread
cls.thrift_server_thread = Thread(target=thrift_server.start_thrift_server)
cls.thrift_server_thread.daemon = True
cls.thrift_server_thread.start()

@classmethod
def tearDownClass(cls):
super().tearDownClass()
# Stop the Thrift server
thrift_server.stop_thrift_server()

def setUp(self):
super().setUp()
self.client = Client()
# Create a user and an admin user
self.user = User.objects.create_user(username='user', password='userpassword')
Expand Down
18 changes: 17 additions & 1 deletion payapp/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
from threading import Thread
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User, Group
from payapp.models import Account, Request, Notification

from timestamp_server import thrift_server

class PayAppViewTests(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Start the Thrift server in a separate thread
cls.thrift_server_thread = Thread(target=thrift_server.start_thrift_server)
cls.thrift_server_thread.daemon = True
cls.thrift_server_thread.start()

@classmethod
def tearDownClass(cls):
super().tearDownClass()
# Stop the Thrift server
thrift_server.stop_thrift_server()

def setUp(self):
super().setUp()
self.client = Client()
# Create a user and an admin user
self.user = User.objects.create_user(username='user', password='userpassword')
Expand Down
18 changes: 17 additions & 1 deletion register/tests.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
from threading import Thread
from unittest.mock import patch

from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User, Group
from register.forms import UserForm, LoginForm
from payapp.utils import convert_currency
from timestamp_server import thrift_server


class UserViewTests(TestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
# Start the Thrift server in a separate thread
cls.thrift_server_thread = Thread(target=thrift_server.start_thrift_server)
cls.thrift_server_thread.daemon = True
cls.thrift_server_thread.start()

@classmethod
def tearDownClass(cls):
super().tearDownClass()
# Stop the Thrift server
thrift_server.stop_thrift_server()

def setUp(self):
super().setUp()
# Setup code for the tests, like creating a user or a group if needed
# self.admin_group = Group.objects.create(name='AdminGroup')
User.objects.create_user(username='testuser1',first_name='Test',last_name='User' ,password='testpassword123', email='test@example.com')
Expand Down
13 changes: 12 additions & 1 deletion timestamp_server/thrift_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

from gen_py.timestamp_service import TimestampService

# Global variable to hold the Thrift server instance
server = None
# Global variable to control the server loop
server_running = True

class TimestampHandler:
def getCurrentTimestamp(self):
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

def start_thrift_server():
global server
global server_running
handler = TimestampHandler()
processor = TimestampService.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
Expand All @@ -22,7 +28,12 @@ def start_thrift_server():

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Starting the Thrift server...")
server.serve()
while server_running:
server.handle()

def stop_thrift_server():
global server_running
server_running = False

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
Expand Down
Binary file modified webapps.db
Binary file not shown.

0 comments on commit 238addd

Please sign in to comment.