Example #1
0
    def test_get_entries__filter_by_range_date__expected_correct_result(self):
        america_timezone = pytz.timezone('America/Sao_Paulo')

        connection = Connection('postgres', 'root', '127.0.0.1:5432', 'planting_manager_teste')
        session = connection.session()
        meeiro_id = create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')
        entry_type_id = create_entry_type('despesas', connection)
        session.expunge_all()
        session.close()

        execute_session = connection.session()
        Entry.insert(meeiro_id=meeiro_id, entry_date=america_timezone.localize(datetime(2018, 10, 2, 1, 1)),
                     entry_type_id=entry_type_id, entry_value=100.0,
                     description='veneno', db_session=execute_session)

        Entry.insert(meeiro_id=meeiro_id, entry_date=america_timezone.localize(datetime(2018, 10, 1, 23, 58)),
                     entry_type_id=entry_type_id, entry_value=1520.0,
                     description='combustivel', db_session=execute_session)

        filters_ = Entry.get_filters_to_query_entry(db_session=execute_session,
                                                    date_filter=
                                                    DateRange(min_date=america_timezone.localize(datetime(2018, 10, 1, 23, 59)),
                                                              max_date=america_timezone.localize(datetime(2018, 10, 2, 1, 10)),
                                                              use_equal=True),
                                                    )
        entries = Entry.list(execute_session, filters_)
        self.assertEqual(len(entries), 1)
        expected_entry = entries[0]

        self.assertEqual(expected_entry._entry_type_id, entry_type_id)
        self.assertEqual(str(expected_entry._entry_date), str(america_timezone.localize(datetime(2018, 10, 2, 1, 1))))
        self.assertEqual(expected_entry._meeiro_id, meeiro_id)
        self.assertEqual(expected_entry._description, 'veneno')
        self.assertEqual(expected_entry._entry_value, 100.0)
Example #2
0
    def test__get_meeiro_by_cpf__expect_none(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')
        create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')

        m = Meeiro.get_meeiro(connection.session(), cpf='whatever')
        connection.session().close_all()

        self.assertIsNone(m)
Example #3
0
    def test__get_meeiro_by_cpf__expect_correct_instance(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')
        create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')
        m = Meeiro.get_meeiro(connection.session(), cpf='55584447213')
        connection.session().close_all()

        self.assertEqual(m.name, 'tadeu')
        self.assertEqual(m.rg, '50658045x')
def clear_database():
    connection = Connection('postgres', 'root', '127.0.0.1:5432',
                            'planting_manager_teste')
    session = connection.session()
    for table in reversed(Base.metadata.sorted_tables):
        session.execute(table.delete())
        print('Deleted table {}'.format(table.name))
    session.commit()
    session.close_all()
    print("Cleared all database")
def destroy_database():
    root = Connection('postgres', 'root', '127.0.0.1:5432', 'postgres')
    root_session = root.session()
    root_session.connection().connection.set_isolation_level(0)
    root_session.execute("""SELECT pg_terminate_backend(pid) 
                            FROM pg_stat_activity WHERE datname = 'planting_manager_teste';"""
                         )
    root_session.execute("DROP DATABASE planting_manager_teste")
    root_session.connection().connection.set_isolation_level(1)
    root_session.close_all()
    print("Dropped database...")
Example #6
0
    def test_insert_entry__invalid_meeiro__expect_raise_error(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432', 'planting_manager_teste')
        session = connection.session()
        entry_type_id = create_entry_type('despesas', connection)
        session.expunge_all()
        session.close()

        with self.assertRaises(RowNotFound):
            Entry.insert(meeiro_id=21, entry_date=datetime(2018, 10, 1),
                         entry_type_id=entry_type_id, entry_value=100.0,
                         description='veneno', db_session=connection.session())
Example #7
0
    def test__insert_meeiro_already_saved__expect_error(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')
        create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')
        connection.session().close_all()

        with self.assertRaises(DuplicatedValue):
            Meeiro.insert(connection.session(),
                          name='tadeu',
                          cpf='55584447213',
                          rg='50658045x')
Example #8
0
    def test_insert_entry__invalid_entry_type__expect_raise_error(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432', 'planting_manager_teste')
        session = connection.session()
        meeiro_id = create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')
        session.expunge_all()
        session.close()

        with self.assertRaises(RowNotFound):
            Entry.insert(meeiro_id=meeiro_id, entry_date=datetime(2018, 10, 1),
                         entry_type_id=23, entry_value=100.0,
                         description='veneno', db_session=connection.session())
def create_meeiro(name: str, cpf: str, rg: str) -> int:
    connection = Connection('postgres', 'root', '127.0.0.1:5432',
                            'planting_manager_teste')
    session = connection.session()
    meeiro_obj = Meeiro()
    meeiro_obj.name = name
    meeiro_obj.cpf = cpf
    meeiro_obj.rg = rg
    session.add(meeiro_obj)
    session.flush()
    session.commit()
    meeiro_id = meeiro_obj.id
    session.close()

    return meeiro_id
def initialize_database():
    root = Connection('postgres', 'root', '127.0.0.1:5432', 'postgres')
    root_session = root.session()
    root_session.connection().connection.set_isolation_level(0)
    try:
        print("Trying to create database...")
        root_session.execute("CREATE DATABASE planting_manager_teste")
        root_session.connection().connection.set_isolation_level(1)
    except exc.ProgrammingError:
        print("Trying to remove already exists database and creating...")
        root_session.execute("DROP DATABASE planting_manager_teste")
        root_session.execute("CREATE DATABASE planting_manager_teste")

    root_session.close_all()

    connection = Connection('postgres', 'root', '127.0.0.1:5432',
                            'planting_manager_teste')
    Base.metadata.create_all(connection.engine)
    print("Initialized database")
Example #11
0
    def test_insert_entry__expect_correct_insert(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432', 'planting_manager_teste')
        session = connection.session()
        meeiro_id = create_meeiro(name='tadeu', cpf='55584447213', rg='50658045x')
        entry_type_id = create_entry_type('despesas', connection)
        session.expunge_all()
        session.close()

        Entry.insert(meeiro_id=meeiro_id, entry_date=datetime(2018, 10, 1),
                     entry_type_id=entry_type_id, entry_value=100.0,
                     description='veneno', db_session=connection.session())

        america_timezone = pytz.timezone('America/Sao_Paulo')
        entry = session.query(EntryMapping).one()
        self.assertEqual(entry.entry_type, entry_type_id)
        self.assertEqual(str(entry.entry_date), str(america_timezone.localize(datetime(2018, 10, 1))))
        self.assertEqual(entry.meeiro_id, meeiro_id)
        self.assertEqual(entry.description, 'veneno')
        self.assertEqual(entry.entry_value, 100.0)
def create_entry_type(name: str, connection: Connection) -> int:
    session = connection.session()

    entry_type = EntryType()
    entry_type.name = name
    session.add(entry_type)
    session.flush()

    entry_type_id = entry_type.id

    session.commit()
    session.close()

    return entry_type_id
Example #13
0
    def test__update_meeiro_cpf_already_saved_for_another__expected_ok(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')
        meeiro_id = create_meeiro(name='tadeu',
                                  cpf='55584447213',
                                  rg='50658045x')
        create_meeiro(name='another', cpf='1111111111', rg='2222222222')
        connection.session().expunge_all()
        connection.session().close_all()

        with self.assertRaises(DuplicatedValue):
            Meeiro.update(connection.session(),
                          new_cpf='1111111111',
                          new_rg='new_rg',
                          new_name='new_name',
                          id_=meeiro_id)
def create_entry(meeiro_id: int, entry_date: datetime, entry_type_id: int,
                 entry_value: float, description: str,
                 connection: Connection) -> int:
    entry = Entry()
    entry.meeiro_id = meeiro_id
    entry.entry_date = entry_date
    entry.entry_type = entry_type_id
    entry.entry_value = entry_value
    entry.description = description

    session = connection.session()
    session.add(entry)
    session.flush()

    entry_id = entry.id

    session.commit()
    session.close()

    return entry_id
Example #15
0
    def test__insert_new_meeiro__expect_success(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')

        Meeiro.insert(connection.session(),
                      cpf='whatever',
                      rg='bla',
                      name='lalala')
        connection.session().close_all()

        m = connection.session().query(MeeiroMapping).one()
        self.assertIsNotNone(m)
        self.assertEqual(m.cpf, 'whatever')
        self.assertEqual(m.rg, 'bla')
        self.assertEqual(m.name, 'lalala')
Example #16
0
    def test__update_meeiro__expected_ok(self):
        connection = Connection('postgres', 'root', '127.0.0.1:5432',
                                'planting_manager_teste')
        meeiro_id = create_meeiro(name='tadeu',
                                  cpf='55584447213',
                                  rg='50658045x')
        connection.session().expunge_all()
        connection.session().close_all()

        result_set = Meeiro.update(connection.session(), 'new_cpf', 'new_rg',
                                   'new_name', meeiro_id)
        self.assertTrue(result_set)
        connection.session().expunge_all()
        connection.session().close_all()

        altered_obj = connection.session().query(MeeiroMapping).filter(
            MeeiroMapping.id == meeiro_id).one()
        self.assertEqual(altered_obj.name, 'new_name')
        self.assertEqual(altered_obj.rg, 'new_rg')
        self.assertEqual(altered_obj.cpf, 'new_cpf')
Example #17
0
from flask import Flask, render_template

from orm.postgres_connection import Connection
from views.general import mod as general_view
from views.meeiro import mod as meeiro_view
from views.entry import mod as entry_view
from views.entry_type import mod as entry_type_view

app = Flask(__name__)

config_path = os.path.join(os.path.dirname(__file__), '.config.json')
configs = json.loads(open(config_path).read())

connection = Connection(user=configs['username'],
                        pwd=configs['password'],
                        host='127.0.0.1:5432',
                        db=configs['database'])

app.config['database'] = connection.session()


@app.errorhandler(Exception)
def handler_errors(error):
    app.config['database'].rollback()
    app.config['database'].close()


@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404
from orm.postgres_connection import Connection
from orm.planting import Base

connection = Connection('postgres', 'root', '127.0.0.1:5432', 'auto_teste')

Base.metadata.create_all(connection.engine)