def test_cant_get_all_alg_tests_by_incorrect_id(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) test1 = Test(0, alg1.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg1.alg_id, "TITLE_TEST2", "SOURCE_TEST2") test3 = Test(0, alg2.alg_id, "TITLE_TEST3", "SOURCE_TEST3") test4 = Test(0, alg3.alg_id, "TITLE_TEST4", "SOURCE_TEST4") _, test1 = (self.core.db.addTest(test1)) _, test2 = (self.core.db.addTest(test2)) _, test3 = (self.core.db.addTest(test3)) _, test4 = (self.core.db.addTest(test4)) status1, tests1 = self.core.db.getAllAlgTests(alg1.alg_id + len("typo")) status2, tests2 = self.core.db.getAllAlgTests(alg2.alg_id + len("typo")) status3, tests3 = self.core.db.getAllAlgTests(alg3.alg_id + len("typo")) self.assertEqual(status1, DBStatus.s_data_issue) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(status3, DBStatus.s_data_issue) self.assertEqual(tests1[0].test_id, ERROR_ID) self.assertEqual(tests2[0].test_id, ERROR_ID) self.assertEqual(tests3[0].test_id, ERROR_ID)
def test_can_get_test_by_correct_ids(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) test1 = Test(0, alg1.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg1.alg_id, "TITLE_TEST2", "SOURCE_TEST2") test3 = Test(0, alg2.alg_id, "TITLE_TEST3", "SOURCE_TEST3") test4 = Test(0, alg3.alg_id, "TITLE_TEST4", "SOURCE_TEST4") _, test1 = (self.core.db.addTest(test1)) _, test2 = (self.core.db.addTest(test2)) _, test3 = (self.core.db.addTest(test3)) _, test4 = (self.core.db.addTest(test4)) status1, test5 = self.core.db.getTest(alg1.alg_id, test1.test_id) status2, test6 = self.core.db.getTest(alg1.alg_id, test2.test_id) status3, test7 = self.core.db.getTest(alg2.alg_id, test3.test_id) status4, test8 = self.core.db.getTest(alg3.alg_id, test4.test_id) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(status3, DBStatus.s_ok) self.assertEqual(status4, DBStatus.s_ok) self.assertEqual(test1, test5) self.assertEqual(test2, test6) self.assertEqual(test3, test7) self.assertEqual(test4, test8)
def test_can_get_stats(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) test1 = Test(0, alg1.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg1.alg_id, "TITLE_TEST2", "SOURCE_TEST2") test3 = Test(0, alg2.alg_id, "TITLE_TEST3", "SOURCE_TEST3") test4 = Test(0, alg3.alg_id, "TITLE_TEST4", "SOURCE_TEST4") _, test1 = (self.core.db.addTest(test1)) _, test2 = (self.core.db.addTest(test2)) _, test3 = (self.core.db.addTest(test3)) _, test4 = (self.core.db.addTest(test4)) task1 = Task(0, alg1.alg_id, 0, "MESSAGE_TASK1") task2 = Task(0, alg2.alg_id, 0, "MESSAGE_TASK2") task3 = Task(0, alg3.alg_id, 0, "MESSAGE_TASK3") _, task1 = (self.core.db.addTask(task1)) _, task2 = (self.core.db.addTask(task2)) _, task3 = (self.core.db.addTask(task3)) status, stats = self.core.db.getStats() self.assertEqual(status, DBStatus.s_ok) self.assertEqual(stats.num_of_users, 2) self.assertEqual(stats.num_of_algs, 3) self.assertEqual(stats.num_of_tests, 4) self.assertEqual(stats.num_of_tasks, 3)
def test_can_add_diff_tests(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) test1 = Test(0, alg1.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg1.alg_id, "TITLE_TEST2", "SOURCE_TEST2") test3 = Test(0, alg2.alg_id, "TITLE_TEST3", "SOURCE_TEST3") test4 = Test(0, alg3.alg_id, "TITLE_TEST4", "SOURCE_TEST4") status1, test1 = self.core.db.addTest(test1) status2, test2 = self.core.db.addTest(test2) status3, test3 = self.core.db.addTest(test3) status4, test4 = self.core.db.addTest(test4) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(status3, DBStatus.s_ok) self.assertEqual(status4, DBStatus.s_ok) self.assertEqual(test1.test_id, 1) self.assertEqual(test2.test_id, 2) self.assertEqual(test3.test_id, 1) self.assertEqual(test4.test_id, 1) self.assertEqual(self.core.db._getNextID("1a"), 3) self.assertEqual(self.core.db._getNextID("2a"), 2) self.assertEqual(self.core.db._getNextID("3a"), 2)
def test_cant_add_iden_users(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER1", "FN_USER2", "SN_USER2", "MN_USER2", []) status1, user1 = self.core.db.addUser(user1) status2, user2 = self.core.db.addUser(user2) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(user1.user_id, 1) self.assertEqual(user2.user_id, ERROR_ID)
def test_can_get_all_users(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) status, users = self.core.db.getAllUsers() self.assertEqual(status, DBStatus.s_ok) self.assertEqual(users[0], user1) self.assertEqual(users[1], user2)
def test_can_add_diff_users(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) status1, user1 = self.core.db.addUser(user1) status2, user2 = self.core.db.addUser(user2) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(user1.user_id, 1) self.assertEqual(user2.user_id, 2)
def getAllUsers(self) -> Tuple[DBStatus, List[User]]: try: users_collection = self.db["users"] users_tmp = list(users_collection.find({})) users = [] for user_d in users_tmp: users.append(User.fromDict(user_d)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, [User(ERROR_ID)]) logger.debug("Users was found") return (DBStatus.s_ok, users)
def getUserByLogin(self, login) -> Tuple[DBStatus, User]: try: users_collection = self.db["users"] user_d = users_collection.find_one({"login": login}) if user_d is None: logger.error("This user does not exist") return (DBStatus.s_data_issue, User(ERROR_ID)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, User(ERROR_ID)) logger.debug("User was found") return (DBStatus.s_ok, User.fromDict(user_d))
def test_can_update_user(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) _, user1 = self.core.db.addUser(user1) self.assertEqual(user1.first_name, "FN_USER1") self.assertEqual(user1.second_name, "SN_USER1") self.assertEqual(user1.middle_name, "MN_USER1") user1.first_name = "FN_USER2" user1.second_name = "SN_USER2" user1.middle_name = "MN_USER2" status = self.core.db.updUser(user1) self.assertEqual(status, DBStatus.s_ok) status, user2 = self.core.db.getUserByID(user1.user_id) self.assertEqual(status, DBStatus.s_ok) self.assertEqual(user2.first_name, "FN_USER2") self.assertEqual(user2.second_name, "SN_USER2") self.assertEqual(user2.middle_name, "MN_USER2")
def testPostUserUpdate(self): user = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) self.core.db.addUser(user) header = { "Authorization": ("Token: " + self.client.post('/api/get_token/', { "username": self.username, "password": self.password }).json()["auth_token"]) } response = self.client.post( '/api/users/1/', { "user_id": 1, "login": "******", "first_name": "FN_USER2", "second_name": "SN_USER2", "middle_name": "MN_USER2", }, **header) self.assertEqual(response.status_code, 200) response = self.client.get('/api/users/1/') stats = response.json() self.assertEqual(stats["id"], 1) self.assertEqual(stats["login"], "LOGIN_USER1") self.assertEqual(stats["first_name"], "FN_USER2") self.assertEqual(stats["second_name"], "SN_USER2") self.assertEqual(stats["middle_name"], "MN_USER2")
def testGetUsersEndpointWithAddedUser(self): self.core.db.addUser( User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", [])) response = self.client.get('/api/users/') self.assertEqual(response.status_code, 200) stats = response.json() self.assertEqual(stats["count"], 1)
def testGetAlgsEndpointWithAddedUser(self): self.core.db.addUser( User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", [])) alg = Algorithm(0, "algorithm1", "description1", 1, 'print("Hello")', [], 0, 0) self.core.db.addAlg(alg) response = self.client.get('/api/users/') self.assertEqual(response.status_code, 403)
def testGetAlgByTitleEndpoint(self): user = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) self.core.db.addUser(user) alg = Algorithm(0, "algorithm1", "description1", 1, 'print("Hello")', [], 0, 0) self.core.db.addAlg(alg) response = self.client.get('/api/algs/algorithm1/') self.assertEqual(response.status_code, 403)
def test_can_get_alg_by_correct_title(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user.user_id, "SOURCE_ALG1", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) status, alg2 = self.core.db.getAlgByTitle(alg1.title) self.assertEqual(status, DBStatus.s_ok) self.assertEqual(alg1, alg2)
def test_cant_add_alg_with_incorrect_user_id(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id + len("typo"), "SOURCE_ALG", [], 0, 0) status, alg = self.core.db.addAlg(alg) self.assertEqual(status, DBStatus.s_data_issue) self.assertEqual(alg.alg_id, ERROR_ID) self.assertEqual(self.core.db._getNextID("1a"), DBStatus.s_data_issue)
def test_cant_get_algs_by_incorrect_title(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user.user_id, "SOURCE_ALG1", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) status, alg2 = self.core.db.getAlgByTitle(alg1.title + "typo") self.assertEqual(status, DBStatus.s_data_issue) self.assertEqual(alg2.alg_id, ERROR_ID)
def addUser(self, user) -> Tuple[DBStatus, User]: try: # check login st = (self.getUserByLogin(user.login))[0] if st != DBStatus.s_data_issue: logger.error("This login is already taken") return (DBStatus.s_data_issue, User(ERROR_ID)) # add user users_collection = self.db["users"] user.user_id = self._getNextID("users") if user.user_id == DBStatus.s_data_issue: return (DBStatus.s_connection_error, User(ERROR_ID)) users_collection.insert_one(user.__dict__) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, User(ERROR_ID)) logger.debug("New user was successfully added") return (DBStatus.s_ok, user)
def authenticate(self, login: str, password: str) -> bool: from webforces.server.core import Core core = Core() status, user = core.db.getUserByLogin(login) if status != DBStatus.s_ok: user = User(0, login, "", "", "", []) core.db.addUser(user) logger.debug(f"Add new user to mongodb: {user}") logger.debug(f"Logging in user: '******', password: {password}'")
def testGetUserByLoginEndpoint(self): status, user = self.core.db.addUser( User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", [])) response = self.client.get('/api/users/LOGIN_USER1/') self.assertEqual(response.status_code, 200) stats = response.json() self.assertEqual(stats["id"], 1) self.assertEqual(stats["login"], "LOGIN_USER1") self.assertEqual(stats["first_name"], "FN_USER1") self.assertEqual(stats["second_name"], "SN_USER1") self.assertEqual(stats["middle_name"], "MN_USER1")
def test_can_get_all_author_algs_by_correct_user_id(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) status1, algs1 = self.core.db.getAllAuthorAlgs(user1.user_id) status2, algs2 = self.core.db.getAllAuthorAlgs(user2.user_id) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(alg1, algs1[0]) self.assertEqual(alg2, algs1[1]) self.assertEqual(alg3, algs2[0])
def test_cant_get_all_author_algs_by_incorrect_user_id(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) status1, algs1 = self.core.db.getAllAuthorAlgs(user1.user_id + len("typo")) status2, algs2 = self.core.db.getAllAuthorAlgs(user2.user_id + len("typo")) self.assertEqual(status1, DBStatus.s_data_issue) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(algs1[0].alg_id, ERROR_ID) self.assertEqual(algs2[0].alg_id, ERROR_ID)
def test_can_get_alg_by_correct_alg_id(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) status1, alg4 = self.core.db.getAlgByID(alg1.alg_id) status2, alg5 = self.core.db.getAlgByID(alg2.alg_id) status3, alg6 = self.core.db.getAlgByID(alg3.alg_id) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(status3, DBStatus.s_ok) self.assertEqual(alg1, alg4) self.assertEqual(alg2, alg5) self.assertEqual(alg3, alg6)
def test_cant_get_alg_by_incorrect_alg_id(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) status1, alg4 = self.core.db.getAlgByID(alg1.alg_id + len("typo")) status2, alg5 = self.core.db.getAlgByID(alg2.alg_id + len("typo")) status3, alg6 = self.core.db.getAlgByID(alg3.alg_id + len("typo")) self.assertEqual(status1, DBStatus.s_data_issue) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(status3, DBStatus.s_data_issue) self.assertEqual(alg4.alg_id, ERROR_ID) self.assertEqual(alg5.alg_id, ERROR_ID) self.assertEqual(alg6.alg_id, ERROR_ID)
def test_can_add_diff_algs(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) status1, alg1 = self.core.db.addAlg(alg1) status2, alg2 = self.core.db.addAlg(alg2) status3, alg3 = self.core.db.addAlg(alg3) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(status3, DBStatus.s_ok) self.assertEqual(alg1.alg_id, 1) self.assertEqual(alg2.alg_id, 2) self.assertEqual(alg3.alg_id, 3) self.assertEqual(self.core.db._getNextID("1a"), 1) self.assertEqual(self.core.db._getNextID("2a"), 1) self.assertEqual(self.core.db._getNextID("3a"), 1)
def test_can_get_all_task(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) task1 = Task(0, alg.alg_id, 0, "MESSAGE_TASK1") task2 = Task(0, alg.alg_id, 0, "MESSAGE_TASK2") _, task1 = (self.core.db.addTask(task1)) _, task2 = (self.core.db.addTask(task2)) status, tasks = self.core.db.getAllTasks() self.assertEqual(status, DBStatus.s_ok) self.assertEqual(tasks[0], task1) self.assertEqual(tasks[1], task2)
def post(self, request, login): core = Core() status, user_db = core.db.getUserByLogin(login) if status != DBStatus.s_ok: return Response({"error": f"User does not exist: {status}"}, status=500) user = request.data.dict() user["user_id"] = user_db.user_id user["login"] = login user["algs_id"] = [] user = User.fromDict(user) status = core.db.updUser(user) if status != DBStatus.s_ok: return Response({"error": f"Could not update user: {status}"}, status=500) return Response({"success": f"User {login} was successfully updated"})
def test_cant_add_task_with_incorrect_alg_id(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) task1 = Task(0, alg.alg_id + len("typo"), 0, "MESSAGE_TASK1") task2 = Task(0, alg.alg_id + len("typo"), 0, "MESSAGE_TASK2") status1, task1 = self.core.db.addTask(task1) status2, task2 = self.core.db.addTask(task2) self.assertEqual(status1, DBStatus.s_data_issue) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(task1.task_id, ERROR_ID) self.assertEqual(task2.task_id, ERROR_ID) self.assertEqual(self.core.db._getNextID("tasks"), 1)
def test_cant_add_iden_tests(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) test1 = Test(0, alg.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg.alg_id, "TITLE_TEST1", "SOURCE_TEST2") status1, test1 = self.core.db.addTest(test1) status2, test2 = self.core.db.addTest(test2) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(test1.test_id, 1) self.assertEqual(test2.test_id, ERROR_ID) self.assertEqual(self.core.db._getNextID("1a"), 2)
def testGetAlgByTitleEndpoint(self): user = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) self.core.db.addUser(user) alg = Algorithm(0, "algorithm1", "description1", 1, 'print("Hello")', [], 0, 0) self.core.db.addAlg(alg) response = self.client.get('/api/algs/algorithm1/') self.assertEqual(response.status_code, 200) stats = response.json() self.assertEqual(stats["alg_id"], 1) self.assertEqual(stats["title"], "algorithm1") self.assertEqual(stats["author_id"], 1) self.assertEqual(stats["source"], 'print("Hello")') self.assertEqual(stats["tests_id"], []) self.assertEqual(stats["lang_id"], 0)