Esempio n. 1
0
    def test__add(self):

        t = DbObject(DBH.test)
        t.tbl_.truncate()
        t.db_.commit()

        t.company_id = COMPANY_ID
        t.number = NUMBER
        t.name = NAME
        t.amount = AMOUNT
        t.start_date = START_DATE
        t.status = STATUS
        self.assertTrue(t.add())  # Add succeeds
        self.assertTrue(self.i_re.match(str(t.id)))  # Object id field is set
        # Creation and modified dates should be set.
        self.assertTrue(isinstance(t.created_on, datetime.datetime))
        self.assertTrue(isinstance(t.updated_on, datetime.datetime))

        rows = t.db_.executesql(
            """
            SELECT
                id,
                company_id,
                number,
                name,
                amount,
                start_date,
                status,
                created_on,
                updated_on
            FROM test;
            """
        )
        self.assertEqual(rows[0][0], t.id)  # Id matches
        self.assertEqual(rows[0][1], COMPANY_ID)  # company_id matches
        self.assertEqual(rows[0][2], NUMBER)  # number matches
        self.assertEqual(rows[0][3], NAME)  # name matches
        self.assertEqual(rows[0][4], AMOUNT)  # amount matches
        self.assertEqual(rows[0][5], START_DATE)  # start_date matches
        self.assertEqual(rows[0][6], STATUS)  # status matches
        # Creation and modified dates should be set.
        self.assertTrue(isinstance(rows[0][7], datetime.datetime))
        self.assertTrue(isinstance(rows[0][8], datetime.datetime))

        old_id = t.id
        self.assertTrue(t.add())  # Successive add succeeds
        self.assertTrue(t.id != old_id)  # Object has new id
        self.assertEqual(t.db_(t.tbl_.id > 0).count(), 2)  # Now two records

        t.tbl_.truncate()
        t.db_.commit()

        # Test MySQL column default values.
        t2 = DbObject(DBH.test)
        self.assertEqual(t2.status, None)  # Status initially null.
        t2.add()
        rows = t2.db_.executesql(
            """
            SELECT
                id,
                company_id,
                number,
                name,
                amount,
                start_date,
                status,
                created_on,
                updated_on
            FROM test;
            """
        )
        self.assertEqual(rows[0][0], t2.id)  # Id matches
        self.assertEqual(rows[0][6], "a")  # status matches default
        self.assertEqual(t2.status, "a")  # Instance is updated.

        t2.tbl_.truncate()
        t2.db_.commit()
        return
Esempio n. 2
0
    def test____init__(self):
        # R0915: *Too many statements (%s/%s)*
        # pylint: disable=R0915

        t = DbObject(DBH.test)
        DBH.executesql("""DELETE from test;""")
        self.assertTrue(t)  # Object created

        # Ensure object has default properties
        self.assertTrue(hasattr(t, "db_"))
        self.assertTrue(hasattr(t, "set_"))
        self.assertTrue(isinstance(t.set_, Collection))

        # Test a few object properties
        self.assertTrue(hasattr(t, "company_id"))
        self.assertTrue(hasattr(t, "status"))

        # Test setting and getting field values
        t.company_id = COMPANY_ID
        t.status = "d"
        self.assertEqual(t.company_id, COMPANY_ID)
        self.assertEqual(t.status, "d")

        self.assertEqual(t.db_(t.tbl_.id > 0).count(), 0)  # Has no records

        # Raw sql

        t.db_.executesql(
            """
            INSERT into test
                (company_id, status)
            VALUES
                (%s, '%s');
            """
            % (COMPANY_ID, STATUS)
        )
        self.assertEqual(t.db_(t.tbl_.id > 0).count(), 1)  # Has one record
        rows = t.db_.executesql(
            """
            SELECT id, company_id, name, status from test;
            """
        )
        self.assertTrue(self.i_re.match(str(rows[0][0])))  # Id is integer
        self.assertEqual(rows[0][1], COMPANY_ID)  # company_id matches
        self.assertEqual(rows[0][2], None)  # unset name is None
        self.assertEqual(rows[0][3], STATUS)  # status matches

        t.tbl_.truncate()
        t.db_.commit()
        self.assertEqual(t.db_(t.tbl_.id > 0).count(), 0)  # Has no records

        # Test passing attributes as parameters.

        t = DbObject(DBH.test)
        # No parameters, name is None
        self.assertEqual(t.name, None)
        t = DbObject(DBH.test, name=NAME)
        # Provide name as parameter, gets set.
        self.assertEqual(t.name, NAME)
        t = DbObject(DBH.test, name=NAME, xxxx="__fake__")
        # Provide name as parameter, gets set.
        # No field attribute is not set.
        self.assertEqual(t.name, NAME)
        self.assertFalse(hasattr(t, "xxxx"))

        # Test set_ property.
        t = DbObject(DBH.test)
        t.tbl_.truncate()
        t.db_.commit()

        t.company_id = COMPANY_ID
        t.name = NAME
        t.number = NUMBER
        t.start_date = START_DATE
        t.status = STATUS
        self.assertTrue(t.add())  # Add succeeds

        t2 = DbObject(DBH.test)
        t2.company_id = COMPANY_ID
        t2.name = NAME_2
        t2.number = NUMBER_2
        t2.start_date = START_DATE_2
        t2.status = STATUS_2
        self.assertTrue(t2.add())  # Add (2) succeeds

        collection = t.set_.get(id=t.id)
        self.assertEqual(collection.objects, [t])
        collection = t.set_.get(id=t2.id)
        self.assertEqual(collection.objects, [t2])
        collection = t.set_.get(query=t.tbl_.name == "fake")
        self.assertEqual(collection.objects, [])
        collection = t.set_.get(query=t.tbl_.status != "")
        self.assertEqual(collection.objects, [t, t2])

        return