def test_select(self): """ Tests that select alone returns all the rows in testtable """ ## Add rows to testtable utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") rows = testtable.select() self.assertListEqual(rows,[("Hello",1),("World",2)])
def test_quickselect_None(self): """ Tests that quickselect correctly rephrases column = None and column__eq = None into "is null" and column__ne = None into "is not null" (as sqlite does not recognize the other forms) """ self.connection.row_factory = objects.dict_factory ## Some additional setup utils.populatetesttable(self) utils.setupadditionaltables(self) testtable2 = self.connection.getadvancedtable("testtable2") inrows = [{"forgnid":0,"myname":None},{"forgnid":0,"myname":"Foo"},{"forgnid":1,"myname":None},{"forgnid":1,"myname":"Bar"},{"forgnid":1,"myname":"BizzBuzz"}] testtable2.addmultiple(*inrows) ## Sanity check rows = testtable2.selectall() self.assertEqual(len(rows),5) self.assertEqual(rows,inrows) ## The test rows = testtable2.quickselect(myname = None) self.assertEqual(len(rows),2) self.assertEqual(rows,[{"forgnid":0,"myname":None},{"forgnid":1,"myname":None}]) rows = testtable2.quickselect(myname__eq=None) self.assertEqual(len(rows),2) self.assertEqual(rows,[{"forgnid":0,"myname":None},{"forgnid":1,"myname":None}]) rows = testtable2.quickselect(myname__ne=None) self.assertEqual(len(rows),3) self.assertEqual(rows,[{"forgnid":0,"myname":"Foo"},{"forgnid":1,"myname":"Bar"},{"forgnid":1,"myname":"BizzBuzz"}])
def test_quickselect(self): """ Tests the functionality of quickselect (since quickselect uses select, which is already tested, not as much testing needs to be done) """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") for kwarg,expected in [ ({"value":1}, [("Hello",1),]), ({"name__like":"world"}, [("World",2),]), ({"name__like":"%orl%"}, [("World",2),]), ({"name__likeany":"orl"}, [("World",2),]), ({"value__eq":1}, [("Hello",1),]), ({"value__ne":1}, [("World",2),]), ({"value__lt":2}, [("Hello",1),]), ({"value__gt":1}, [("World",2),]), ({"value__lte":2}, [("Hello",1),("World",2)]), ({"value__gte":2}, [("World",2),]), ({"value__lte":2, "name":"Hello"}, [("Hello",1),]), ({"value__in":(1,2)}, [("Hello",1),("World",2)]), ({"value__notin":(1,3,5,7)}, [("World",2),]), ({"pk":1},[("Hello",1),]), ({"limit":1},[("Hello",1),]), ({"distinct":True},[("Hello",1),("World",2)]), ]: with self.subTest(kwarg = kwarg, expected = expected): rows = testtable.quickselect(**kwarg) self.assertListEqual(rows, expected)
def test_get(self): """ Tests that the AdvancedTable.get(value) returns the same as AdvancedTable.quickselect(pk = value).first() """ utils.populatetesttable(self) table = self.connection.getadvancedtable("testtable") rowid = 1 correct = table.quickselect(pk = rowid).first() row = table.get(rowid) self.assertEqual(row,correct)
def test_quickupdate_bad(self): """ Tests some error-raising for quickupdate """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") self.assertRaises(ValueError,testtable.quickupdate, constriants = "Hello") self.assertRaises(ValueError, testtable.quickupdate, WHERE = dict(notacolumn=True)) self.assertRaises(ValueError, testtable.quickupdate, notacolumn = False) ## quickupdate does not accept positional arguements self.assertRaises(TypeError, testtable.quickupdate, dict(badconstraint = 0))
def test_deleteall(self): """ Tests that deleteall removes all rows """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") ## Checking that rows are actually in there self.assertTrue(testtable.selectall()) testtable.deleteall() ## Check that they're all gone self.assertListEqual(testtable.selectall(),[])
def test_parseobject(self): """ Tests that the AdvancedTable can parse the correct attributes from an object that shares it's columns """ ## Throw in some rows to turn into objects utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") testtable.row_factory = objects.object_to_factory(utils.TestObject) rows = testtable.quickselect(name = "Hello") result = [testtable.parseobject(row) for row in rows] self.assertListEqual(result,[{"name":"Hello","value":1}])
def test_saverowfactory_dict_factory(self): """ Tests that saverowfactory changes the row_factory to dict_factory """ self.connection.row_factory = utils.RandomRowFactory utils.populatetesttable(self) @objects.saverowfactory def testfunction(connection): return connection.execute("""SELECT * FROM testtable;""").fetchone() value = testfunction(self.connection) self.assertIsInstance(value,dict)
def test_quickdelete_bad(self): """ Tests some error-raising for quickdelete """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") ## Non-existent column self.assertRaises(ValueError, testtable.quickdelete, notacolumn=True) ## quickdelete requires kwargs self.assertRaises(TypeError,testtable.quickdelete) ## quickupdate does not accept positional arguements self.assertRaises(TypeError, testtable.quickupdate, dict(badconstraint = 0))
def test_select_various(self): """ Tests a couple of basic select queries """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") ## Blank query should be the same as No Query rows = testtable.select(query = "") self.assertListEqual(rows,testtable.select()) ## Basic filter rows = testtable.select(query = "value = 1") self.assertListEqual(rows,[("Hello",1),]) ## Tuple Replacement (since we haven't tried that yet) rows = testtable.select(query = "value = ?",replacements = (1,)) self.assertListEqual(rows,[("Hello",1),])
def test_select_rowid(self): """ Tests that select with rowid returns all the rows in testtable with their rowids """ ## Add rows to testtable utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") rows = testtable.select(rowid = True) self.assertListEqual(rows,[(1,"Hello",1),(2,"World",2)]) ## Test with a specified rowid utils.setupadditionaltables(self) utils.populatetesttable3(self) testtable3 = self.connection.getadvancedtable("testtable3") rows = testtable3.select(rowid = True) self.assertListEqual(rows,[(1,True),(2,False),(3,False),(4,True)])
def test_advancedtablefactory_dict_factory(self): """ Tests that advancedtablefactory changes the row_factory to dict_factory """ self.connection.row_factory = utils.RandomRowFactory utils.populatetesttable(self) testtable = self.connection.gettable("testtable") testtable.row_factory = objects.dict_factory @objects.advancedtablefactory def testfunction(testtable): return testtable.database.execute("""SELECT * FROM testtable;""").fetchone() value = testfunction(testtable) ## Result of the function should be dict self.assertIsInstance(value,dict)
def test_quickdelete(self): """ Tests the AdvancedTable's quickdelete method. Like testing quickupdate, this builds off of pretested functions, so it won't be as delineated. """ testtable = self.connection.getadvancedtable("testtable") ## Test one constraint utils.populatetesttable(self) testtable.quickdelete(value__lt=2) ## rowid=1/(Hello,1) should be deleted self.assertListEqual(testtable.selectall(rowid=True),[(2,"World",2),]) ## Test Multiple Constraints utils.populatetesttable(self) testtable.quickdelete(value__gte=2,name__likeany="orl") ## rowid=2/(World,2) should be deleted self.assertListEqual(testtable.selectall(rowid=True),[(1,"Hello",1),]) ## Test Multiple Deletes utils.populatetesttable(self) testtable.quickdelete(value__lte=2) ## Both rows should be deleted self.assertListEqual(testtable.selectall(rowid=True),[]) ## Test No selects utils.populatetesttable(self) testtable.quickdelete(name="Can't Touch This!") ## Both rows should be untouched self.assertListEqual(testtable.selectall(),[("Hello",1),("World",2)])
def test_select_factory(self): """ Tests that select will use the Table's Row Factory instead of the Database's. """ ## Add rows to testtable utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") ## Set Different factories testtable.database.row_factory = objects.dict_factory testtable.row_factory = objects.object_to_factory(utils.TestObject) rows = testtable.select() ## Check Type expectations = [dict(name="Hello",value=1),dict(name="World",value = 2)] for row,expected in zip(rows,expectations): with self.subTest(row = row): self.assertIsInstance(row,utils.TestObject) self.assertEqual(row.name,expected['name']) self.assertEqual(row.value,expected['value'])
def test_get_bad(self): """ Tests that AdvancedTable.get(value) fails with an Exception. """ utils.populatetesttable(self) table = self.connection.getadvancedtable("testtable") rowid = 100 self.assertRaisesRegex(ValueError,f"Table \w+.* has no row: {rowid}",table.get,rowid)
def test_select_replacements_list(self): """ Tests that select correctly substitues replacements as list """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") rows = testtable.select(query = "value < ?", replacements =(2,)) self.assertListEqual(rows,[("Hello",1),])
def test_quickdelete_2(self): """ Some more quick delete tests """ utils.populatetesttable(self) testtable = self.connection.getadvancedtable("testtable") testtable.quickdelete(pk__in = [1,2]) self.assertListEqual(testtable.selectall(),[])
def setUp(self): utils.setupconnection(self) utils.populatetesttable(self) return super().setUp()
def test_quickupdate(self): """ Tests the quickupdate method of AdvancedTable; a lot of the infrastructure was already tested, so we're doing a bunch together""" testtable = self.connection.getadvancedtable("testtable") ## Test Nothing utils.populatetesttable(self) testtable.quickupdate() ## All rows should be unchanged self.assertListEqual(testtable.selectall(),[("Hello",1),("World",2)]) ## Test no constraints utils.populatetesttable(self) testtable.quickupdate(name = "Foobar") ## All rows should have name = "Foobar" self.assertListEqual(testtable.selectall(),[("Foobar",1),("Foobar",2)]) ## Test one constraint utils.populatetesttable(self) testtable.quickupdate(WHERE = dict(value__lt=2), name="Bizzbazz") ## rowid=1/(Hello,1) should have name = "Bizzbazz" instead self.assertListEqual(testtable.selectall(rowid=True),[(1,"Bizzbazz",1),(2,"World",2)]) ## Test Multiple Constraints utils.populatetesttable(self) testtable.quickupdate(WHERE = dict(value__gte=2,name__likeany="orl"), name="BizzBar") ## rowid=2/(World,2) should have name = "BizzBar" instead self.assertListEqual(testtable.selectall(rowid=True),[(1,"Hello",1),(2,"BizzBar",2)]) ## Test Multiple Sets utils.populatetesttable(self) testtable.quickupdate(WHERE = dict(value__lte=2), name="Bang", value = 3) ## Both rows should be updated to be identical (outside of rowid) self.assertListEqual(testtable.selectall(rowid=True),[(1,"Bang",3),(2,"Bang",3)]) ## Test Mutliple Constraints with no result selected utils.populatetesttable(self) testtable.quickupdate(WHERE = dict(value__gte=0, name__like="Foobar"), name="Can't Touch This!") ## Both rows should be untouched self.assertListEqual(testtable.selectall(),[("Hello",1),("World",2)])