def test_fixture_with_list_dependencies(self):
        fixture_user = FixtureUser()
        fixture_post = FixturePostWithList()
        fixture_post.init_depended_fixtures({FixtureUser: fixture_user})
        fixture_user.load()
        fixture_post.load()

        self.assertEqual(fixture_post['post1'].authors[0].id,
                         fixture_user['user1'].pk)
    def test_fixture_with_wrong_ref_format(self):
        fixture_user = FixtureUser()
        fixture_post = FixturePostWrongRefFormat()
        fixture_post.init_depended_fixtures({FixtureUser: fixture_user})
        fixture_user.load()

        with self.assertRaises(FixturesMongoengineException) as cm:
            fixture_post.load()
        self.assertRegexpMatches(cm.exception.message,
                                 'Wrong depend reference')
    def test_fixture_with_wrong_dependency(self):
        fixture_user = FixtureUser()
        fixture_post = FixturePostWrongDepended()
        fixture_post.init_depended_fixtures({FixtureUser: fixture_user})
        fixture_user.load()

        with self.assertRaises(FixturesMongoengineException) as cm:
            fixture_post.load()
        self.assertRegexpMatches(cm.exception.message,
                                 'not fount in depended fixtures')
    def test_fixture_with_reference_dependencies(self):
        fixture_user = FixtureUser()
        fixture_post = FixturePostWithReference()
        fixture_post.init_depended_fixtures({FixtureUser: fixture_user})
        fixture_user.load()
        fixture_post.load()

        self.assertIsInstance(fixture_post['post1'].author, User)
        self.assertEqual(fixture_post['post1'].author.pk,
                         fixture_user['user1'].pk)
    def test_fixture_without_dependencies(self):

        fixture = FixtureUser()
        fixture.load()

        self.assertEqual(fixture['user1'].first_name, 'Joyce')
        self.assertEqual(fixture['user2'].first_name, 'Amy')

        user1 = User.objects(first_name='Joyce').first()
        self.assertIsNotNone(user1)
        self.assertEqual(user1.last_name, 'Ray')
    def test_unload(self):

        fixture = FixtureUser()
        fixture.load()
        fixture.unload()

        with self.assertRaises(KeyError):
            print(fixture['user1'].first_name)

        user1 = User.objects(first_name='Joyce').first()
        self.assertIsNone(user1)