def setUp(self): for table in self.tables: table.create_table().run_sync() manager_1 = Manager(name="Guido") manager_1.save().run_sync() band_1 = Band( name="Pythonistas", manager=manager_1.id, popularity=1000 ) band_1.save().run_sync() manager_2 = Manager(name="Graydon") manager_2.save().run_sync() band_2 = Band(name="Rustaceans", manager=manager_2.id) band_2.save().run_sync() venue = Venue(name="Grand Central", capacity=1000) venue.save().run_sync() concert = Concert(band_1=band_1.id, band_2=band_2.id, venue=venue.id) concert.save().run_sync() ticket = Ticket(concert=concert, price=decimal.Decimal(50.0)) ticket.save().run_sync()
def test_where_is_not_null(self): self.insert_rows() Band(name="Managerless", popularity=0, manager=None).save().run_sync() queries = ( Band.select(Band.name).where(Band.manager != None), # noqa Band.select(Band.name).where(Band.manager.is_not_null()), ) for query in queries: response = query.run_sync() self.assertEqual( response, [ { "name": "Pythonistas" }, { "name": "Rustaceans" }, { "name": "CSharps" }, ], )
def test_insert(self): self.insert_rows() Band.insert(Band(name="Rustaceans", popularity=100)).run_sync() response = Band.select(Band.name).run_sync() names = [i["name"] for i in response] self.assertIn("Rustaceans", names)
def test_insert_curly_braces(self): """ You should be able to insert curly braces without an error. """ self.insert_rows() Band.insert(Band(name="{}", popularity=100)).run_sync() response = Band.select(Band.name).run_sync() names = [i["name"] for i in response] self.assertIn("{}", names)
def test_save_specific_columns(self): """ Make sure that we can save a subset of columns. """ manager = Manager(name="Guido") manager.save().run_sync() band = Band(name="Pythonistas", popularity=1000, manager=manager) band.save().run_sync() self.assertEqual( Band.select().run_sync(), [{ "id": 1, "name": "Pythonistas", "manager": 1, "popularity": 1000, }], ) band.name = "Pythonistas 2" band.popularity = 2000 band.save(columns=[Band.name]).run_sync() # Only the name should update, and not the popularity: self.assertEqual( Band.select().run_sync(), [{ "id": 1, "name": "Pythonistas 2", "manager": 1, "popularity": 1000, }], ) ####################################################################### # Also test it using strings to identify columns band.name = "Pythonistas 3" band.popularity = 3000 band.save(columns=["popularity"]).run_sync() # Only the popularity should update, and not the name: self.assertEqual( Band.select().run_sync(), [{ "id": 1, "name": "Pythonistas 2", "manager": 1, "popularity": 3000, }], )
def test_using_expression(self): """ Test the `using_expression` option, which can be used to tell Postgres how to convert certain column types. """ Band(name="1").save().run_sync() alter_query = Band.alter().set_column_type( old_column=Band.name, new_column=Integer(), using_expression="name::integer", ) alter_query.run_sync() popularity = Band.select(Band.name).first().run_sync()["name"] self.assertEqual(popularity, 1)
def test_get_related(self): """ Make sure you can get a related object from another object instance. """ manager = Manager(name="Guido") manager.save().run_sync() band = Band(name="Pythonistas", manager=manager.id, popularity=100) band.save().run_sync() _manager = band.get_related(Band.manager).run_sync() self.assertTrue(_manager.name == "Guido") # Test non-ForeignKey with self.assertRaises(ValueError): band.get_related(Band.name) # Make sure it also works using a string _manager = band.get_related("manager").run_sync() self.assertTrue(_manager.name == "Guido") # Test an invalid string with self.assertRaises(ValueError): band.get_related("abc123")
def test_non_existant_column(self): with self.assertRaises(ValueError): Band(name="Pythonistas", foo="bar")
def test_insert_sqlite(self): Pythonistas = Band(name="Pythonistas") self.assertEqual(Pythonistas.__str__(), "(null,'Pythonistas',null,0)")
def test_insert_postgres(self): Pythonistas = Band(name="Pythonistas") self.assertEqual( Pythonistas.__str__(), "(DEFAULT,'Pythonistas',null,0)" )