def test_get_user_by_filter_no_filters(self):
        for i in range(5):
            user_json = UserModelTestCase.get_user_json(UserModelTestCase.get_test_user(i))
            register(user_json)

        users = db_util.get_users_by_filter()
        self.assertEqual(len(users), 5)
    def test_get_user_by_filter_email_unique_constraint(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        registered_user = register(user_json)

        test_user2 = {
            "email": registered_user.email,
            "username": "******",
            "password": "******"
        }

        integrity_error_raised = False
        try:
            register(test_user2)
        except:
            integrity_error_raised = True

        self.assertTrue(integrity_error_raised)
    def test_get_user(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        registered_user = register(user_json)

        user = db_util.get_user(registered_user.id)
        self.assertEqual(registered_user.id, user.id)
        self.assertEqual(test_user.email, user.email)
        self.assertEqual(test_user.username, user.username)
        self.assertEqual(registered_user.password_hash, user.password_hash)
    def test_register_user(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        user = register(user_json)

        self.assertIsNotNone(user)
        self.assertEqual(user.username, test_user.username)
        self.assertNotEqual(user.password_hash, test_user.password)
        self.assertEqual(user.email, test_user.email)
        self.assertFalse(user.confirmed)
    def test_get_user_by_filter_username(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        registered_user = register(user_json)

        user = db_util.get_users_by_filter(username=registered_user.username)
        self.assertGreater(len(user), 0)
        user = user[0]
        self.assertIsNotNone(user)
        self.assertEqual(registered_user.id, user.id)
        self.assertEqual(registered_user.username, user.username)
        self.assertEqual(registered_user.email, user.email)
    def test_update_user(self):
        test_user = UserModelTestCase.get_test_user(0)
        user_json = UserModelTestCase.get_user_json(test_user)
        registered_user = register(user_json)

        modified_json = user_json.copy()
        modified_json["username"] = "******"
        updated_user = db_util.update_user(registered_user.id, modified_json)

        self.assertEqual(modified_json["username"], updated_user.username)
        self.assertEqual(user_json["email"], updated_user.email)
        self.assertNotEqual(user_json["username"], updated_user.username)
        self.assertEqual(registered_user.password_hash, updated_user.password_hash)
    def test_get_all_users(self):
        users = []

        for i in range(3):
            users.append(register(self.get_user_json(UserModelTestCase.get_test_user(i))))
            all_users = db_util.get_all_users()
            self.assertIsNotNone(all_users)
            self.assertEqual(len(all_users), i + 1)

        all_users = db_util.get_all_users()

        for i in range(3):
            self.assertTrue(users[i].username == all_users[i].username)
            self.assertTrue(users[i].email == all_users[i].email)
            self.assertTrue(users[i].password_hash == all_users[i].password_hash)