Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is no available initial state for the content type while testing #74

Closed
superandrew opened this issue May 12, 2019 · 16 comments
Closed

Comments

@superandrew
Copy link

@superandrew superandrew commented May 12, 2019

When I create an integration test, I create objects in the DB for which I have defined a state field.

        p = Procedure.objects.create()

In django admin I defined 4 states, and 3 transitions

Screenshot 2019-05-12 at 17 46 39

So I understood from the documentation that since CREATED is never a destination state, django-river would have guessed that this was the initial state.

But I get this error:

raise RiverException(ErrorCode.NO_AVAILABLE_INITIAL_STATE, 'There is no available initial state for the content type %s. ' % self._content_type)
river.utils.exceptions.RiverException: There is no available initial state for the content type procedure. 

I'm completely lost, as I understand that this is common but I haven't find a solution, also looking at similar issues.

Any idea?

Thanks

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 12, 2019

I notice that in my River applications I have just

States and Transition Approval Meta entities to add or modify. No Transitions or Proceeding Meta like in the tutorial. Does that mean that I miss something from the setup?

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 12, 2019

this is what I tried, after understanding that many of the tutorials refer to old version of the framework:

        cr = State.objects.create(label="Created", slug='cr')
        sb = State.objects.create(label="Submitted", slug='sb')
        va = State.objects.create(label="Validated", slug='va')
        rf = State.objects.create(label="Refused", slug='rf')
        cr.save()
        sb.save()
        va.save()
        rf.save()


        content_type = ContentType.objects.get_for_model(Procedure)

        transition_am1 = TransitionApprovalMeta.objects.create(source_state=cr, destination_state=sb, content_type=content_type)
        transition_am2 = TransitionApprovalMeta.objects.create(source_state=sb, destination_state=va, content_type=content_type)
        transition_am3 = TransitionApprovalMeta.objects.create(source_state=sb, destination_state=rf, content_type=content_type)
        transition_am1.save()
        transition_am2.save()
        transition_am3.save()

In a non testing scenario I suppose that you should run migrate, or maybe there is something that I am still missing.

My test suite starts with:

140549272366536 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta

then, for every test in which I try to create programmatically a Procedure,

line 38, in initial_state raise RiverException(ErrorCode.NO_AVAILABLE_INITIAL_STATE, 'There is no available initial state for the content type %s. ' % self._content_type) river.utils.exceptions.RiverException: There is no available initial state for the content type procedure.

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 12, 2019

ok, I was able to fix it. The problem was that (as a fool) I was doing the setup in the setup method, so every case didn't have data. So Now it works, passing the first state to the object I create

def create_workflow():
cr = State.objects.create(label="Created", slug='cr')
sb = State.objects.create(label="Submitted", slug='sb')
va = State.objects.create(label="Validated", slug='va')
rf = State.objects.create(label="Refused", slug='rf')
cr.save()
sb.save()
va.save()
rf.save()

content_type = ContentType.objects.get_for_model(Procedure)

transition_am1 = TransitionApprovalMeta.objects.create(source_state=cr, destination_state=sb,
                                                       content_type=content_type)
transition_am2 = TransitionApprovalMeta.objects.create(source_state=sb, destination_state=va,
                                                       content_type=content_type)
transition_am3 = TransitionApprovalMeta.objects.create(source_state=sb, destination_state=rf,
                                                       content_type=content_type)
transition_am1.save()
transition_am2.save()
transition_am3.save()

return cr

then,

  p = Procedure.objects.create(current_state=create_workflow())

however, I still have this: 140283630478712 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta when starting tests.

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 12, 2019

When in functional test I post an object, even after the create_workflow call, I have the same error.
probably I should put an initial state in the perform_create override when I am in test mode but I think this kind of misses the point of the initial state guessing.

ErrorCode.NO_AVAILABLE_INITIAL_STATE

In normal mode I have no errors, apart from the continuous <number>field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta when starting tests. warning.

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented May 12, 2019

Hi @superandrew, it seems like you are not setting field_name of TransitionApprovalMeta in your create workflow function. Since you can have multiple workflows for the same model regarding the different model fields, this is the only way that django-river would figure out what workflow it is. It is weird that django allows you to create a TransitionApprovalMeta without a field name. I would not expect that.

But apart from that, I can say that the warnings and error messages should definitely be improved with the field_name part as well.

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 12, 2019

oh, wow. Silly me. It works now.

What do you think about the

140332700806376 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta warning I see everytime I launch django and/or the test suite? Is this a misconfiguration, or is it normal? Also, is it normal that everytime I launch makemigrations I've got a new migration with that id changed?

Thanks!

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented May 12, 2019

There seems to be something wrong with field_name error message though. 140549272366536 in the message is actually the field name. But I don't know why it thinks that this number is the field name :-)

I will take a look at this when I have time. But you try setting your field_name in the TransitionApprovalMeta. It should fix it.

Here is an example of creating it programmatically. It is with factory_boy library but the idea is the same.

https://github.com/javrasya/django-river/blob/master/river/tests/base_test.py

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented May 13, 2019

thanks! however setting the field fixed the error, so all tests now run

@javrasya javrasya closed this May 13, 2019
@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented Jun 18, 2019

@javrasya I think there still is a problem with the fields because every time I run django I have this message:

140692356329112 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta

and everytime I run manage.py makemigrations django river creates a new migration slightly changing the field name:


from django.db import migrations
import django.db.models.deletion
import river.models.fields.state


class Migration(migrations.Migration):

    dependencies = [
        ('organization', '0002_auto_20190512_1919'),
    ]

    operations = [
        migrations.AlterField(
            model_name='procedure',
            name='current_state',
            field=river.models.fields.state.StateField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='rnc2a0dc9cd27e4f9383979698d883a495', to='river.State'),
        ),
    ]

Do you think this is related to this issue or do you think a new one should be created?

Thanks

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jun 18, 2019

@superandrew , I think I know why this is happening and it is a bug in the place where this is logged. It is not affecting anything. Just logging part is buggy. Please ignore it until I fix and release it.

https://github.com/javrasya/django-river/blob/master/river/apps.py#L21

worflow_registry.workflow is a map of lists. It should be flattened and then looped.

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jun 18, 2019

I created a PR @superandrew . I am waiting for the build to see if that breaks anything

javrasya pushed a commit that referenced this issue Jun 18, 2019
javrasya pushed a commit that referenced this issue Jun 18, 2019
javrasya pushed a commit that referenced this issue Jun 18, 2019
javrasya added a commit that referenced this issue Jun 18, 2019
…orrectly (#76)

* [#74] Log all the workflow fields that doesn't have a workflow meta correctly

* [#74] Use django-river lower than 2.2 for coverage

* [#74] Publish change logs for v1.0.1

* [#74] Simplify set to list conversion

* Bump patch version
@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jun 18, 2019

I even published it. Can you try django-river v1.0.1 @superandrew .

@superandrew

This comment has been minimized.

Copy link
Author

@superandrew superandrew commented Jul 1, 2019

thanks @javrasya !

I think the problem has been solved partially, but not completely.
django-river==1.0.0

ipdbackend on  release/1.1.2 [$!] via ipaddi-backend took 1m 44s 
➜ manage.py makemigrations        
140252405073224 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta
Migrations for 'organization':
  ipdbackend/organization/migrations/0004_auto_20190701_1310.py
    - Alter field current_state on procedure

ipdbackend on  release/1.1.2 [$!?] via ipaddi-backend took 3s 
➜ manage.py makemigrations
140333496797320 field doesn't seem have any transition approval meta in database. You should create it's TransitionApprovalMeta
Migrations for 'organization':
  ipdbackend/organization/migrations/0005_auto_20190701_1310.py
    - Alter field current_state on procedure

django-river==1.0.1

ipdbackend on  release/1.1.2 [$!?] via ipaddi-backend took 2s 
➜ manage.py makemigrations       
Migrations for 'organization':
  ipdbackend/organization/migrations/0006_auto_20190701_1313.py
    - Alter field current_state on procedure

ipdbackend on  release/1.1.2 [$!?] via ipaddi-backend took 2s 
➜ manage.py makemigrations
Migrations for 'organization':
  ipdbackend/organization/migrations/0007_auto_20190701_1313.py
    - Alter field current_state on procedure

What happens is, despite not being warned, a new migration is generated every time I run manage.py makemigrations.

Is this normal?

Thanks

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jul 1, 2019

It doesn't seem to be normal 😄

Can you share those migration file contents? So I can have some idea maybe 🤔

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jul 2, 2019

I managed to reproduce that. I will have a look if I can understand why this is happening.

@javrasya

This comment has been minimized.

Copy link
Owner

@javrasya javrasya commented Jul 2, 2019

Here I created another issue for that. Let's discuss it over there. Please don't use this issue. If you have another issue feel free to create a new one for it.

#77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.