# Path: yawn/workflow/models.py
# class WorkflowName(models.Model):
#     name = models.TextField(unique=True)
#     current_version = models.OneToOneField('Workflow', on_delete=models.CASCADE,
#                                            null=True, related_name='is_current')
# 
#     def new_version(self, **kwargs):
#         """Create a new version of a workflow"""
#         version = 0
#         if self.current_version_id:
#             version = self.current_version.version
# 
#             # disable the past schedule
#             self.current_version.schedule_active = False
#             self.current_version.save()
# 
#         workflow = Workflow.objects.create(name=self, version=version + 1, **kwargs)
#         self.current_version = workflow
#         self.save()
#         return workflow
# 
# Path: yawn/workflow/tests/utils.py
# def load_sample_workflow():
#     filename = os.path.join(os.path.dirname(__file__), 'workflow.yaml')
#     return yaml.safe_load(open(filename).read())


import pytest
from django.utils.dateparse import parse_datetime
from yawn.workflow.models import WorkflowName
from yawn.workflow.tests.utils import load_sample_workflow
    response = client.post('/api/workflows/', data)
    assert response.status_code == 201, response.data
    assert response.data['version'] == 2


def test_invalid_fields(client, data):
    data['name'] = ''
    data['parameters'] = 'not a dict'
    data['tasks'][1]['upstream'].append('invalid_task')
    response = client.post('/api/workflows/', data)
    assert response.status_code == 400, response.data
    assert 'This field may not be blank' in response.data['name'][0]
    assert 'must be a dictionary' in response.data['parameters'][0]
    assert 'upstream task(s) invalid_task' in response.data['tasks'][0]


def test_more_invalid_fields(client, data):
    data['parameters'] = {'invalid variable': 1}
    data['tasks'] = []
    response = client.post('/api/workflows/', data)
    assert response.status_code == 400, response.data
    assert 'Invalid parameter key' in response.data['parameters'][0]
    assert 'Invalid parameter value' in response.data['parameters'][1]
    assert 'non_field_errors' in response.data['tasks']


def test_list_workflow_names(client):
    name = WorkflowName.objects.create(name='workflow1')
    version1 = name.new_version()
    version2 = name.new_version()

# Path: yawn/workflow/models.py
# class WorkflowName(models.Model):
#     name = models.TextField(unique=True)
#     current_version = models.OneToOneField('Workflow', on_delete=models.CASCADE,
#                                            null=True, related_name='is_current')
# 
#     def new_version(self, **kwargs):
#         """Create a new version of a workflow"""
#         version = 0
#         if self.current_version_id:
#             version = self.current_version.version
# 
#             # disable the past schedule
#             self.current_version.schedule_active = False
#             self.current_version.save()
# 
#         workflow = Workflow.objects.create(name=self, version=version + 1, **kwargs)
#         self.current_version = workflow
#         self.save()
#         return workflow
# 
# Path: yawn/workflow/tests/utils.py
# def load_sample_workflow():
#     filename = os.path.join(os.path.dirname(__file__), 'workflow.yaml')
#     return yaml.safe_load(open(filename).read())


# Path: yawn/workflow/tests/test_views.py
import pytest
from django.utils.dateparse import parse_datetime
from yawn.workflow.models import WorkflowName
from yawn.workflow.tests.utils import load_sample_workflow

    response = client.post('/api/workflows/', data)
    assert response.status_code == 201, response.data
    assert response.data['version'] == 2


def test_invalid_fields(client, data):
    data['name'] = ''
    data['parameters'] = 'not a dict'
    data['tasks'][1]['upstream'].append('invalid_task')
    response = client.post('/api/workflows/', data)
    assert response.status_code == 400, response.data
    assert 'This field may not be blank' in response.data['name'][0]
    assert 'must be a dictionary' in response.data['parameters'][0]
    assert 'upstream task(s) invalid_task' in response.data['tasks'][0]


def test_more_invalid_fields(client, data):
    data['parameters'] = {'invalid variable': 1}
    data['tasks'] = []
    response = client.post('/api/workflows/', data)
    assert response.status_code == 400, response.data
    assert 'Invalid parameter key' in response.data['parameters'][0]
    assert 'Invalid parameter value' in response.data['parameters'][1]
    assert 'non_field_errors' in response.data['tasks']


def test_list_workflow_names(client):
    name = WorkflowName.objects.create(name='workflow1')
    version1 = name.new_version()
    version2 = name.new_version()

    response = client.get('/api/names/')