def test_fk_to_pk_value(self):
     company = Company.objects.create(name='Macrohard')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=1, manager=None)
     employee_fixture.load()
     self.assertEqual(Employee.objects.count(), 1)
     self.assertTrue(Employee.objects.all()[0].company == company)
 def test_empty_fixture(self):
     # Since we allow dynamic populating of Fixture instances, loading
     # empty ones should not produce an error (unlike Django, where empty
     # fixture files will do that).
     band_fixture = Fixture(Band)
     band_fixture.load()
     self.assertEqual(Band.objects.count(), 0)
    def test_natural_keys_to_preexisting_objects(self):
        rails_n00b = Competency.objects.create(framework='Ruby on Rails', level=1)
        cake_adept = Competency.objects.create(framework='CakePHP', level=2)
        spring_master = Competency.objects.create(framework='Spring', level=3)
        django_guru = Competency.objects.create(framework='Django', level=4)

        jobs = Fixture(JobPosting)
        # No M2M
        jobs.add(1, title='Rails Intern', main_competency=('Ruby on Rails', 1))
        # Single M2M with a tuple in a single-item list
        jobs.add(2, title='Elder Django Deity', main_competency=('Django', 4),
            additional_competencies=[('Ruby on Rails', 1)])
        # Several M2Ms with a list of tuples
        jobs.add(3, title='A man of many talents', main_competency=('Spring', 3),
            additional_competencies=[('CakePHP', 2), ('Ruby on Rails', 1)]
        )
        jobs.load()

        self.assertEqual(JobPosting.objects.count(), 3)
        no_m2m_job = JobPosting.objects.get(pk=1)
        self.assertEqual(no_m2m_job.main_competency, rails_n00b)
        single_m2m_job = JobPosting.objects.get(pk=2)
        self.assertEqual(single_m2m_job.additional_competencies.count(), 1)
        self.assertTrue(rails_n00b in single_m2m_job.additional_competencies.all())
        multi_m2m_job = JobPosting.objects.get(pk=3)
        self.assertEqual(multi_m2m_job.additional_competencies.count(), 2)
        self.assertTrue(all([c in multi_m2m_job.additional_competencies.all() for c in [cake_adept, rails_n00b]]))
 def test_single_m2m_to_pk_value(self):
     band = Band.objects.create(pk=1, name="Nuns N' Hoses")
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[1])
     roadie_fixture.load()
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.filter(name="Nuns N' Hoses").count(), 1)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.filter(name='Marshall Amp').count(), 1)
 def test_circular_dependency(self):
     company_fixture = Fixture(Company)
     employee_fixture = Fixture(Employee)
     company_fixture.add(1, name='Dewey, Cheatem & Howe')
     # Trying to be each other's managers
     employee_fixture.add(1, name='Sly M. Ball', company=company_fixture.fk(1), manager=employee_fixture.fk(2))
     employee_fixture.add(2, name='Mei Ting', company=company_fixture.fk(1), manager=employee_fixture.fk(1))
     company_fixture.load()
     self.assertRaises(RelatedObjectError, employee_fixture.load)
 def test_fk_relation(self):
     company_fixture = Fixture(Company)
     company_fixture.add(1, name='Macrohard')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=company_fixture.fk(1), manager=None)
     company_fixture.load()
     employee_fixture.load()
     self.assertEqual(Company.objects.count(), 1)
     self.assertEqual(Employee.objects.count(), 1)
     self.assertTrue(Employee.objects.all()[0].company == Company.objects.all()[0])
 def test_one_level_fk_hierarchy(self):
     # FK from Employee to Company
     company_fixture = Fixture(Company)
     company_fixture.add(1, name='Macrohard')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=company_fixture.fk(1), manager=None)
     # Load in reverse order: 2nd, 1st level
     employee_fixture.load()
     self.assertEqual(Company.objects.count(), 1)
     self.assertEqual(Employee.objects.count(), 1)
 def test_o2o_to_preexisting_object(self):
     company = Company.objects.create(pk=1, name='Macrohard')
     employee = Employee.objects.create(pk=1, name='Andy Depressant', company=company, manager=None)
     history_fixture = Fixture(EmployeeHistory)
     history_fixture.add(1, employee=employee, date_joined='2007-02-22')
     history_fixture.load()
     self.assertEqual(Company.objects.count(), 1)
     self.assertEqual(Employee.objects.count(), 1)
     self.assertEqual(EmployeeHistory.objects.count(), 1)
     self.assertEqual(Employee.objects.all()[0].employeehistory, EmployeeHistory.objects.all()[0])
 def test_multiple_m2ms_to_pk_values(self):
     band1 = Band.objects.create(pk=1, name="Nuns N' Hoses")
     band2 = Band.objects.create(pk=2, name='Led Dirigible')
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[1, 2])
     roadie_fixture.load()
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.count(), 2)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.count(), 1)
     self.assertEqual(Band.objects.get(name='Led Dirigible').roadie_set.count(), 1)
 def test_with_generated_relation(self):
     if self.milkman_found:
         employee_fixture = Fixture(Employee)
         employee_fixture.add_random(1)
         employee_fixture.load()
         self.assertEqual(Employee.objects.count(), 1)
         # Since the Company FK is required, there should be one of those
         # around as well
         self.assertEqual(Company.objects.count(), 1)
         # And that company should be connected to the employee
         self.assertTrue(Employee.objects.all()[0] in Company.objects.all()[0].employee_set.all())
 def test_reverse_single_m2m_relation(self):
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp')
     band_fixture = Fixture(Band)
     band_fixture.add(1, name="Nuns N' Hoses", roadie_set=[roadie_fixture.m2m(1)])
     roadie_fixture.load()
     band_fixture.load()
     self.assertEqual(Band.objects.count(), 1)
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.filter(name="Nuns N' Hoses").count(), 1)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.filter(name='Marshall Amp').count(), 1)
 def test_two_level_fk_o2o_hierarchy_mixed(self):
     # FK from Employee to Company, O2O from EmployeeHistory to Employee
     company_fixture = Fixture(Company)
     company_fixture.add(1, name='Macrohard')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=company_fixture.fk(1), manager=None)
     history_fixture = Fixture(EmployeeHistory)
     history_fixture.add(1, employee=employee_fixture.o2o(1), date_joined='2007-02-22')
     # Load in mixed order: 3rd, 1st, 2nd level
     history_fixture.load()
     self.assertEqual(Company.objects.count(), 1)
     self.assertEqual(Employee.objects.count(), 1)
     self.assertEqual(EmployeeHistory.objects.count(), 1)
 def test_multiple_m2m_relations(self):
     band_fixture = Fixture(Band)
     band_fixture.add(1, name="Nuns N' Hoses")
     band_fixture.add(2, name='Led Dirigible')
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[band_fixture.m2m(1), band_fixture.m2m(2)])
     band_fixture.load()
     roadie_fixture.load()
     self.assertEqual(Band.objects.count(), 2)
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.count(), 2)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.count(), 1)
     self.assertEqual(Band.objects.get(name='Led Dirigible').roadie_set.count(), 1)
    def test_with_explicit_relation_to_preexisting_object(self):
        if self.milkman_found:
            company = Company.objects.create(name='Macrohard')
            manager = Employee.objects.create(name='Sue Ecide-Note', company=company, manager=None)

            employee_fixture = Fixture(Employee)
            # Name will be random
            employee_fixture.add_random(2, company=company, manager=manager)
            employee_fixture.load()

            self.assertEqual(Company.objects.count(), 1)
            self.assertEqual(Employee.objects.count(), 2)
            self.assertEqual(manager.employee_set.count(), 1)
            self.assertEqual(company.employee_set.count(), 2)
    def test_with_explicit_delayed_m2m_relation(self):
        if self.milkman_found:
            band_fixture = Fixture(Band)
            roadie_fixture = Fixture(Roadie)

            band_fixture.add(1, name='Bar Fighters')
            # Name will be random
            roadie_fixture.add_random(1, hauls_for=[band_fixture.fk(1)])
            roadie_fixture.load()

            self.assertEqual(Band.objects.count(), 1)
            self.assertEqual(Roadie.objects.count(), 1)
            self.assertEqual(Roadie.objects.get(pk=1).hauls_for.count(), 1)
            self.assertEqual(Roadie.objects.get(pk=1).hauls_for.all()[0].name, 'Bar Fighters')
 def test_multiple_m2ms_to_mixed_fixture_pk_preexisting(self):
     band1 = Band.objects.create(pk=1, name="Nuns N' Hoses")
     band2_fixture = Fixture(Band)
     band2_fixture.add(2, name='Led Dirigible')
     band3 = Band.objects.create(pk=3, name='Bar Fighters')
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[band1, band2_fixture.fk(2), 3])
     roadie_fixture.load()
     self.assertEqual(Band.objects.count(), 3)
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.count(), 3)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.count(), 1)
     self.assertEqual(Band.objects.get(name='Led Dirigible').roadie_set.count(), 1)
     self.assertEqual(Band.objects.get(name='Bar Fighters').roadie_set.count(), 1)
    def test_with_explicit_delayed_fk_relation(self):
        if self.milkman_found:
            company_fixture = Fixture(Company)
            employee_fixture = Fixture(Employee)

            company_fixture.add(1, name='Macrohard')
            # Name will be random
            employee_fixture.add_random(1, company=company_fixture.fk(1), manager=None)
            employee_fixture.load()

            self.assertEqual(Company.objects.count(), 1)
            self.assertEqual(Employee.objects.count(), 1)
            company = Company.objects.get(pk=1)
            self.assertEqual(company.employee_set.count(), 1)
 def test_m2m_dependent_first(self):
     # The above FK tests already apply to explicit "through" M2Ms, so this
     # only tests normal, "non-through" ones.
     band_fixture = Fixture(Band)
     band_fixture.add(1, name="Nuns N' Hoses")
     band_fixture.add(2, name='Led Dirigible')
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[band_fixture.m2m(1), band_fixture.m2m(2)])
     # M2M relations are two-way, but like only one end has the
     # ManyToManyField, the fixture dependency is also defined in one end
     # of the relation. roadie_fixture is the dependent here, load it first
     # to test proper dependency resolution.
     roadie_fixture.load()
     self.assertEqual(Band.objects.count(), 2)
     self.assertEqual(Roadie.objects.count(), 1)
     self.assertEqual(Roadie.objects.get(name='Marshall Amp').hauls_for.count(), 2)
     self.assertEqual(Band.objects.get(name="Nuns N' Hoses").roadie_set.count(), 1)
     self.assertEqual(Band.objects.get(name='Led Dirigible').roadie_set.count(), 1)
 def test_o2o_relation(self):
     company_fixture = Fixture(Company)
     company_fixture.add(1, name='Macrohard')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=company_fixture.fk(1), manager=None)
     history_fixture = Fixture(EmployeeHistory)
     history_fixture.add(1, employee=employee_fixture.o2o(1), date_joined='2007-02-22')
     company_fixture.load()
     employee_fixture.load()
     history_fixture.load()
     self.assertEqual(Company.objects.count(), 1)
     self.assertEqual(Employee.objects.count(), 1)
     self.assertEqual(EmployeeHistory.objects.count(), 1)
     self.assertEqual(Employee.objects.all()[0].employeehistory, EmployeeHistory.objects.all()[0])
 def test_raw_mode(self):
     company_fixture = Fixture(Company)
     company_fixture.add(1, name='Bloatware Corporation')
     employee_fixture = Fixture(Employee)
     employee_fixture.add(1, name='Andy Depressant', company=company_fixture.fk(1), manager=None)
     # Due to raw=True, the custom save() method that sets
     # 'cog_in_the_machine' to True should not get run.
     raw_employee_fixture = Fixture(Employee, raw=True)
     raw_employee_fixture.add(2, name='Sadie Peon', company=company_fixture.fk(1), manager=None)
     company_fixture.load()
     employee_fixture.load()
     raw_employee_fixture.load()
     self.assertEqual(Employee.objects.count(), 2)
     normal = Employee.objects.get(name='Andy Depressant')
     raw = Employee.objects.get(name='Sadie Peon')
     self.assertEqual(normal.cog_in_the_machine, True)
     self.assertEqual(raw.cog_in_the_machine, False)
 def test_simple_fixture(self):
     band_fixture = Fixture(Band)
     band_fixture.add(1, name="Nuns N' Hoses")
     band_fixture.load()
     self.assertEqual(Band.objects.count(), 1)
 def test_empty_m2m_relation(self):
     roadie_fixture = Fixture(Roadie)
     roadie_fixture.add(1, name='Marshall Amp', hauls_for=[])
     roadie_fixture.load()
     self.assertEqual(Roadie.objects.count(), 1)