def test__export_as_csv(self): t = DbObject(DBH.test) t.tbl_.truncate() t.db_.commit() t.company_id = COMPANY_ID t.name = NAME t.number = NUMBER t.amount = AMOUNT t.start_date = START_DATE t.status = STATUS self.assertTrue(t.add()) # Add succeeds output = cStringIO.StringIO() t.export_as_csv(out=output) # String fields are double quoted. self.assertEqual( """%s,%s,%s,"%s",%s,"%s","%s","%s","%s"\r\n""" % (t.id, COMPANY_ID, NUMBER, NAME, AMOUNT, START_DATE, STATUS, t.created_on, t.updated_on), output.getvalue(), ) t.tbl_.truncate() t.db_.commit() return
def test__remove(self): def names(): n = [] rows = t.db_.executesql("""SELECT name FROM test;""") if len(rows) > 0: for r in rows: if len(r) > 0: n.append(r[0]) return n 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.assertFalse(t.remove()) # Remove fails if record not added 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 # Table populated as expected self.assertEqual(names(), [NAME, NAME_2]) self.assertTrue(t.remove()) # Remove returns success self.assertEqual(names(), [NAME_2]) # Table populated as expected self.assertTrue(t.add()) # Object can be re-added. self.assertTrue(t2.remove()) # Remove (2) returns success self.assertEqual(names(), [NAME]) # Table populated as expected self.assertTrue(t.remove()) # Remove returns success return
def test____deepcopy__(self): t = DbObject(DBH.test) t.company_id = COMPANY_ID t.number = NUMBER t.name = NAME t.amount = AMOUNT t.start_date = START_DATE t.status = STATUS t2 = copy.deepcopy(t) self.assertEqual(t, t2) return
def _obj(self, number=0): if number == 1: t = DbObject(DBH.test) 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 test object return t if number == 2: t2 = DbObject(DBH.test) t2.company_id = COMPANY_ID t2.number = NUMBER_2 t2.name = NAME_2 t2.amount = AMOUNT_2 t2.start_date = START_DATE_2 t2.status = STATUS_2 self.assertTrue(t2.add()) # Add test object 2 return t2
def initialize(): 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()) return t
def test__modify(self): 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 original_updated_on = t.updated_on t.name = NAME_2 time.sleep(1) # Pause one sec so the modified date will change self.assertTrue(t.modify()) # Modify succeeds rows = t.db_.executesql( """ SELECT id, company_id, name, number, 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], NAME_2) # name matches self.assertEqual(rows[0][3], NUMBER) # number matches self.assertEqual(rows[0][4], START_DATE) # start_date matches self.assertEqual(rows[0][5], STATUS) # status matches # Creation and modified dates should be set. self.assertTrue(isinstance(rows[0][6], datetime.datetime)) self.assertTrue(isinstance(rows[0][7], datetime.datetime)) # modified date is updated self.assertNotEqual(t.updated_on, original_updated_on) t.tbl_.truncate() t.db_.commit() return
def test__as_list(self): t = DbObject(DBH.test) # Uninitialized returns list of default values self.assertEqual(t.as_list()[:7], [None, None, None, None, D("0.00"), None, None]) t.company_id = COMPANY_ID t.number = NUMBER t.name = NAME t.amount = AMOUNT t.start_date = START_DATE t.status = STATUS # Initialized returns expected list self.assertEqual(t.as_list()[:7], [None, COMPANY_ID, NUMBER, NAME, AMOUNT, START_DATE, STATUS]) return
def test__as_dict(self): t = DbObject(DBH.test) as_d = t.as_dict() del as_d["created_on"] del as_d["updated_on"] self.assertEqual( as_d, { "id": None, "company_id": None, "number": None, "name": None, "amount": D("0.00"), "start_date": None, "status": None, }, ) t.company_id = COMPANY_ID t.number = NUMBER t.name = NAME t.amount = AMOUNT t.start_date = START_DATE t.status = STATUS as_d = t.as_dict() del as_d["created_on"] del as_d["updated_on"] self.assertEqual( as_d, { "id": None, "company_id": COMPANY_ID, "number": NUMBER, "name": NAME, "amount": AMOUNT, "start_date": START_DATE, "status": STATUS, }, ) return
def test__update(self): def initialize(): 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()) return t records = DbObject(DBH.test).set_.get() self.assertEqual(len(records), 0) t = initialize() records = DbObject(DBH.test).set_.get() self.assertEqual(len(records), 1) self.assertEqual(records[0], t) t2 = DbObject(DBH.test) t2.company_id = COMPANY_ID_2 t2.number = NUMBER_2 t2.name = NAME_2 t2.amount = AMOUNT_2 t2.start_date = START_DATE_2 t2.status = STATUS_2 # Record is not in database, should add self.assertTrue(t2.update()) records = DbObject(DBH.test).set_.get() self.assertEqual(len(records), 2) self.assertNotEqual(t.id, t2.id) # Record is in database, should modify self.assertTrue(t2.update()) records = DbObject(DBH.test).set_.get() self.assertEqual(len(records), 2) self.assertNotEqual(t.id, t2.id) # Update record with identical key fields, should modify t t = initialize() t2 = DbObject(DBH.test) t2.company_id = COMPANY_ID t2.number = NUMBER_2 t2.name = NAME t2.amount = AMOUNT_2 t2.start_date = START_DATE_2 t2.status = STATUS_2 self.assertTrue(t2.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 1) self.assertEqual(t.id, t2.id) # Update record with non-identical key fields but matching id, should # modify. t = initialize() t2 = DbObject(DBH.test) t2.id = t.id t2.company_id = COMPANY_ID_2 t2.number = NUMBER_2 t2.name = NAME_2 t2.amount = AMOUNT_2 t2.start_date = START_DATE_2 t2.status = STATUS_2 self.assertTrue(t2.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 1) self.assertEqual(t.id, t2.id) self.assertEqual(tests[0].company_id, t2.company_id) # Update record with non-identical key fields, no id, should add t = initialize() t2 = DbObject(DBH.test) t2.id = None t2.company_id = COMPANY_ID_2 t2.number = NUMBER_2 t2.name = NAME_2 t2.amount = AMOUNT_2 t2.start_date = START_DATE_2 t2.status = STATUS_2 self.assertTrue(t2.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 2) self.assertNotEqual(t.id, t2.id) # Test with a key field not defined. t = initialize() t2 = DbObject(DBH.test) t2.id = None t2.company_id = COMPANY_ID_2 self.assertTrue(t2.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 2) self.assertNotEqual(t.id, t2.id) t3 = DbObject(DBH.test) t3.id = None t3.company_id = COMPANY_ID_2 self.assertTrue(t3.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 2) self.assertEqual(t2.id, t3.id) # Make sure it distinguishes between NULL and empty string. t = initialize() t2 = DbObject(DBH.test) t2.id = None t2.company_id = COMPANY_ID_2 t2.name = "" self.assertTrue(t2.update(key_fields=["company_id", "name"])) t3 = DbObject(DBH.test) t3.id = None t3.company_id = COMPANY_ID_2 self.assertTrue(t3.update(key_fields=["company_id", "name"])) tests = t.set_.get() self.assertEqual(len(tests), 3) self.assertNotEqual(t2.id, t3.id) t.tbl_.truncate() t.db_.commit() return
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
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