paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
390 stars 67 forks source link

InvalidConfigurationError when creating an object with a primary key #116

Closed andrkhr closed 4 years ago

andrkhr commented 4 years ago

Tests fall when trying to create objects with a primary key. Sample Models and Test

# models.py
class Course(models.Model):
    name = models.CharField(max_length=20)

class Lesson(models.Model):
    name = models.CharField(max_length=20)

class CourseLesson(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)

# test.py
def test_create_course_lesson():
    course = G(Course)
    lesson = G(Lesson)

    lesson_course = G(CourseLesson, course=course, lesson=lesson)

    assert lesson_course.id is not None

I get an error

============================================================================================= FAILURES ==============================================================================================
_____________________________________________________________________________________ test_create_course_lesson _____________________________________________________________________________________

    def test_create_course_lesson():
        course = G(Course)
        lesson = G(Lesson)

>       lesson_course = G(CourseLesson, course=course, lesson=lesson)

home/tests.py:10: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
<string>:6: in get
    ???
../env/dj3/lib/python3.8/site-packages/django_dynamic_fixture/__init__.py:142: in _get
    return d.get(model, lesson=lesson, **kwargs)
../env/dj3/lib/python3.8/site-packages/django_dynamic_fixture/ddf.py:585: in get
    instance = self.new(model_class, lesson=lesson, **kwargs)
../env/dj3/lib/python3.8/site-packages/django_dynamic_fixture/ddf.py:477: in new
    configuration = self._configure_params(model_class, lesson, **kwargs)
../env/dj3/lib/python3.8/site-packages/django_dynamic_fixture/ddf.py:457: in _configure_params
    configuration_custom = library.get_configuration(model_class, name=lesson)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django_dynamic_fixture.ddf.DDFLibrary object at 0x1080f5ee0>, model_class = <class 'home.models.CourseLesson'>, name = <Lesson: Lesson object (1)>

    def get_configuration(self, model_class, name=None):
        if name is None:
            name = self.DEFAULT_KEY
        # copy is important because this dict will be updated every time in the algorithm.
        config = self.configs.get(model_class, {})
        if name != self.DEFAULT_KEY and name not in config.keys():
>           raise InvalidConfigurationError('There is no lesson for model %s with the name "%s"' % (get_unique_model_name(model_class), name))
E           django_dynamic_fixture.ddf.InvalidConfigurationError: There is no lesson for model home.models.CourseLesson with the name "Lesson object (1)"

../env/dj3/lib/python3.8/site-packages/django_dynamic_fixture/ddf.py:221: InvalidConfigurationError

django 3.0.2 django-dynamic-fixture 3.0.1 pytest 5.3.2 pytest-django 3.7.0

On version django-dynamic-fixture 2.0.0 everything works fine

paulocheque commented 4 years ago

Oh, there is a name conflict with the lesson parameter. I think it is better to rename to _lesson or ddf_lesson

I will fix that and create a new release for you.

Thanks for creating the issue!

andrkhr commented 4 years ago

Oh thanks! But here the problem is something else. I tried to rename the variables, but I get exactly the same problem.

Here is how I did it:

# test.py
def test_create_course_lesson():
    ddf_course = G(Course)
    ddf_lesson = G(Lesson)

    lesson_course = G(CourseLesson, course=ddf_course, lesson=ddf_lesson)
    assert lesson_course.id is not None
paulocheque commented 4 years ago

@hauworkarea It should be: lesson_course = G(CourseLesson, course=ddf_course, ddf_lesson=ddf_lesson)

This PR fixes that: https://github.com/paulocheque/django-dynamic-fixture/pull/117

Thanks again for reporting!