def test_order_by_relation(client, db_session): company = Company(name="Headquarters", location="Metropolis") db_session.add(company) db_session.flush() address_1 = Address(city="Gotham") address_2 = Address(city="Wakanda") address_3 = Address(city="Asgard") db_session.add(address_1) db_session.add(address_2) db_session.add(address_3) db_session.flush() employee_1 = Employee(firstname="Batman", company_id=company.id, address=address_1) employee_2 = Employee(firstname="Tchala", company_id=company.id, address=address_2) employee_3 = Employee(firstname="Thor", company_id=company.id, address=address_3) db_session.add(employee_1) db_session.add(employee_2) db_session.add(employee_3) db_session.commit() response = client.get(f"/company/{company.id}/employees") data_list = response.json assert len(data_list) == 3 assert data_list[0]["address"]["city"] == "Gotham" assert data_list[1]["address"]["city"] == "Wakanda" assert data_list[2]["address"]["city"] == "Asgard" response = client.get(f"/company/{company.id}/employees?order_by=city") data_list = response.json assert data_list[0]["address"]["city"] == "Asgard" assert data_list[1]["address"]["city"] == "Gotham" assert data_list[2]["address"]["city"] == "Wakanda" response = client.get(f"/company/{company.id}/employees?order_by=-city") data_list = response.json assert data_list[0]["address"]["city"] == "Wakanda" assert data_list[1]["address"]["city"] == "Gotham" assert data_list[2]["address"]["city"] == "Asgard"
def create_test_sample(db_session): company = Company(id=5, name="Terrans") emp1 = Employee(id=1, firstname="Jim", lastname="Raynor", company=company) addr1 = Address(street="5 Av", number="943", city="Tarsonis") emp1.address = addr1 db_session.add(company) db_session.add(emp1) db_session.commit()
def seed_data(db_session): company = Company(id=5, name='Terrans', location='Korhal') emp1 = Employee(id=1, firstname='Jim', lastname='Raynor', company=company) emp2 = Employee(id=2, firstname='Sarah', lastname='Kerrigan', company=company) emp3 = Employee(id=3, firstname='Tychus', lastname='Findlay') addr1 = Address(street="5 Av", number="943", city="Tarsonis") emp1.address = addr1 emp2.address = addr1 db_session.add_all([company, emp1, emp2, emp3]) db_session.commit()
def create_test_sample(db_session): company = Company(id=5, name='Terrans') emp1 = Employee(id=1, firstname='Jim', lastname='Raynor', company=company) emp2 = Employee(id=2, firstname='Sarah', lastname='Kerrigan', company=company) addr1 = Address(street="5 Av", number="943", city="Tarsonis") emp1.address = addr1 db_session.add(company) db_session.add(emp1) db_session.add(emp2) db_session.commit()
def init_test_data(flask_app, db_session): for name, location in CLIENTS: company = Company(name=name, location=location) db_session.add(company) address = Address(street="5th Av.") emp1 = Employee(firstname="John", lastname="Doe", address=address) db_session.add(address) db_session.add(emp1) db_session.commit() api = Api(flask_app) api.add_model(Company) api.add_model(Employee, serializer_class=EmployeeSerializer) api.add_relation(Company.employees, serializer_class=EmployeeSerializer) return api
def create_test_sample(db_session): contact_type1 = ContactType(label="Phone") contact_type2 = ContactType(label="Email") company = Company(id=5, name="Terrans") emp1 = Employee(id=1, firstname="Jim", lastname="Raynor", company=company) emp2 = Employee(id=2, firstname="Sarah", lastname="Kerrigan", company=company) addr1 = Address(street="5 Av", number="943", city="Tarsonis") emp1.address = addr1 db_session.add(contact_type1) db_session.add(contact_type2) db_session.add(company) db_session.add(emp1) db_session.add(emp2) db_session.commit()