def test_include_one_to_one(self):
        """Test including a one-to-one relationship."""
        model = PersonModel.mock()
        EmployeeModel.mock(person_id=1)

        parameters = {'include': 'employee'}
        included = Resource(self.model, parameters).compound_response(model)
        self.assertTrue(len(included) == 1)
    def test_include_nested(self):
        """Test including a nested relationship."""
        company = CompanyModel.mock()
        model = PersonModel.mock(companies=[company])
        EmployeeModel.mock(person_id=model.id)

        parameters = {'include': 'employee.person.companies'}
        included = Resource(self.model, parameters).compound_response(model)
        self.assertTrue(len(included) == 1)
    def test_filter_one_to_many_relationship(self):
        """Test filtering by a foreign key relationship field."""
        person = PersonModel.mock()
        EmployeeModel.mock(name="employee", person_id=person.id)

        parameters = {'filter[employee.name]': 'EMPLOYEE'}
        query = Resource(self.model, parameters).filter_query(self.query)

        result = query.all()
        self.assertEqual(len(result), 1)
    def test_include_one_to_many_relationship(self):
        """Test including a one to many relationship field."""
        model = PersonModel.mock()
        EmployeeModel.mock(person_id=1)

        include = ['employee']
        iv_objects, errors = IncludeValue.generate(self.view, include)
        data = IncludeValue.include(iv_objects, model)

        self.assertTrue(errors == [])
        self.assertTrue(len(data) == 1)
    def test_include_multiple_relationships(self):
        """Test including multiple relationship fields of differing
        types.
        """
        company = CompanyModel.mock()
        model = PersonModel.mock(companies=[company])
        EmployeeModel.mock(person_id=1)

        include = ['companies', 'employee']
        iv_objects, errors = IncludeValue.generate(self.view, include)
        data = IncludeValue.include(iv_objects, model)

        self.assertTrue(errors == [])
        self.assertTrue(len(data) == 2)
    def test_include_foreign_key(self):
        """Test including a foreign key relationship field."""
        PersonModel.mock()
        model = EmployeeModel.mock(person_id=1)

        include = ['person']
        iv_objects, errors = IncludeValue.generate(EmployeeSchema, include)
        data = IncludeValue.include(iv_objects, model)

        self.assertTrue(errors == [])
        self.assertTrue(len(data) == 1)