Beispiel #1
0
def select_user(name=None):
    logger.info("select_user")
    logger.info(f"select_user: {Session}")

    if name is not None:
        res = Session.execute(select(User).where(User.name == name)).first()
    else:
        res = Session.execute(select(User)).all()
    return res
Beispiel #2
0
def user_add(name):
    logger.info("user_add")
    logger.info(f"user_add: {Session}")

    user = User(name=name, email=f"{name}@example.com")
    Session.add(user)

    logger.info(user in Session.dirty)  # False
    logger.info(user in Session)  # True
    logger.info(user in Session.dirty)  # False
Beispiel #3
0
def migrate(project_name):
    DIR = project_name
    sys.path.append(DIR)
    try:
        import settings
        from db2 import Session
    except Exception as e:
        print(
            "Can not perform migrations.\nNo settings.py file in directory %s"
            % DIR)
        raise e

    s = Session(settings)
    s.makemigrations()
    print("Done...")
Beispiel #4
0
def group_add(name):
    logger.info("group_add")
    logger.info(f"group_add: {Session}")

    group = Group(group_name="splatoon", user_name=name)
    Session.add(group)
Beispiel #5
0
        res = Session.execute(select(User)).all()
    return res


try:
    init_db()

    user_add("ika")
    user_add("tako")
    group_add("ika")

    logger.info(f"main: {Session}")

    # flushしてからでないとinsertの前にselectしてしまう。
    # selectはflushも兼ねてるんじゃなかったのか?
    Session.flush()
    res = select_user("ika")
    logger.info(res)  # (<User 'ika'>,)
    logger.info(res.User)  # <User 'ika', '*****@*****.**'>

    # updateの場合はselectした時点でflushされる。
    res[0].email = "*****@*****.**"
    res = select_user()

    for r in res:
        logger.info(r.User)

except Exception as e:
    logger.exception(f"{e.__class__.__name__}: {e}")
    Session.rollback()
finally:
Beispiel #6
0
 def setUp(self):
     self.session = Session(settings)
     self.session.connection.database = settings.DATABASE
Beispiel #7
0
class TestSession(unittest.TestCase):
    def setUp(self):
        self.session = Session(settings)
        self.session.connection.database = settings.DATABASE

    def tearDown(self):
        self.session.close()

    def test_MySQLConnection(self):
        with self.session as conn:
            self.assertIsInstance(conn, mysql.MySQLConnection)
            self.assertIsInstance(self.session.connection,
                                  mysql.MySQLConnection)
            self.assertIsInstance(conn.cursor(), mysql.cursor.MySQLCursor)

    def test_migrations(self):
        OK = self.session.makemigrations()
        self.assertEqual(OK, True)

    def test_database(self):
        self.assertEqual(self.session.connection.database, settings.DATABASE)

    def test_insert_query(self):
        patient = Patient(id=1,
                          name='Abiira Nathan',
                          sex='Male',
                          address='Uganda',
                          mobile='0785434581',
                          next_of_kin='John K',
                          religion='Protestant',
                          marital_status='Married',
                          date='1989-01-01 13:30:20')

        OK = self.session.add(patient)
        try:
            self.assertEqual(OK, True)
        except:
            self.assertIsInstance(OK, mysql.Error)

    def test_update_query(self):
        patient = Patient(id=1,
                          name='Abiira Nathan',
                          sex='Male',
                          address='Uganda',
                          mobile='0785434581',
                          next_of_kin='John K',
                          religion='Catholic',
                          marital_status='Married',
                          date='1989-01-01 13:30:20')

        OK = self.session.update(patient)
        try:
            self.assertEqual(OK, True)
        except:
            self.assertEqual(OK, False)

    def test_descriptors(self):
        with self.assertRaises(ValueError):
            patient = Patient(mobile='07894565434581')

        with self.assertRaises(TypeError):
            patient = Patient(id='Nathan')

        with self.assertRaises(TypeError):
            patient = Patient(name=1000)

        with self.assertRaises(ValueError):
            patient = Patient(name='Abiira Nathan of Uganda')

    def test_select_query(self):
        results = self.session.get(Patient)
        self.assertTrue(isinstance(results, list))

        results2 = self.session.get(Patient, returnDict=True)
        if results:
            self.assertTrue(isinstance(results2[0], dict))

    def test_settings(self):
        self.assertTrue(hasattr(settings, 'USER'))
        self.assertTrue(hasattr(settings, 'HOST'))
        self.assertTrue(hasattr(settings, 'DATABASE'))
        self.assertTrue(hasattr(settings, 'PASSWORD'))
        self.assertTrue(hasattr(settings, 'DEBUG'))

    def test_models(self):
        self.assertTrue(hasattr(Patient, '_fields'))
        self.assertTrue(hasattr(Patient, 'types'))