Example #1
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 #2
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 #3
0
 def list(self) -> List[MeeiroDto]:
     meeiros = Meeiro.list(self.db_connection)
     serialized_list = []
     for m in meeiros:
         serialized_list.append(
             MeeiroDto(name=m.name, cpf=m.cpf, rg=m.rg, id_=m.id))
     return serialized_list
Example #4
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)
Example #5
0
    def insert(cls, meeiro_id: int, entry_date: datetime, entry_type_id: int,
               entry_value: float, description: str, db_session: Session):
        """
        :raises RowNotFound: caso de valores inválidos como meeiro e tipo de lançamento
        :param meeiro_id:
        :param entry_date:
        :param entry_type_id:
        :param entry_value:
        :param description:
        :param db_session:
        :return:
        """

        meeiro = Meeiro.get_meeiro(db_connection=db_session, id_=meeiro_id)
        entry_type = EntryTypeModel.get_entry_type(db_connection=db_session,
                                                   id_=entry_type_id)

        entry = EntryMapping()
        entry.meeiro_id = meeiro.id
        entry.entry_date = entry_date
        entry.entry_type = entry_type.id
        entry.entry_value = entry_value
        entry.description = description

        db_session.add(entry)
        db_session.commit()
Example #6
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 #7
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')
Example #8
0
 def update(self, id_: int, cpf: str, rg: str, name: str):
     try:
         updated = Meeiro.update(self.db_connection,
                                 new_cpf=cpf,
                                 new_rg=rg,
                                 new_name=name,
                                 id_=id_)
         if updated:
             return True, 'Alterado com sucesso!'
         else:
             return False, 'Erro inesperado. Tente novamente.'
     except DuplicatedValue as error:
         return False, error
Example #9
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 #10
0
    def get_filters_to_query_entry(db_session: Session,
                                   meeiro_id: int = None,
                                   date_filter: DateRange = None,
                                   entry_type_id: int = None,
                                   filter_value: NumValueLimit = None) -> List:
        filters = []
        if meeiro_id:
            meeiro = Meeiro.get_meeiro(db_connection=db_session, id_=meeiro_id)
            filters.append(EntryMapping.meeiro_id == meeiro.id)
        if entry_type_id:
            entry_type = EntryTypeModel.get_entry_type(
                db_connection=db_session, id_=entry_type_id)
            filters.append(EntryMapping.entry_type == entry_type.id)

        if date_filter:
            filters.append(date_filter.get_filter(EntryMapping.entry_date))

        if filter_value:
            filters.append(filter_value.get_filter(EntryMapping.entry_value))

        return filters
Example #11
0
 def get_meeiro(self, id_: int = None, cpf: str = None) -> MeeiroDto:
     m = Meeiro.get_meeiro(db_connection=self.db_connection,
                           id_=id_,
                           cpf=cpf)
     return MeeiroDto(name=m.name, cpf=m.cpf, rg=m.rg, id_=m.id)
Example #12
0
 def insert_new_meeiro(self, name: str, cpf: str, rg: str) -> (bool, str):
     try:
         Meeiro.insert(self.db_connection, cpf, rg, name)
         return True, 'Inserido com Sucesso!'
     except DuplicatedValue as error:
         return False, error