species: tests and data migration
This commit is contained in:
		
							parent
							
								
									831e4091dc
								
							
						
					
					
						commit
						afa22b046b
					
				
					 4 changed files with 74 additions and 0 deletions
				
			
		
							
								
								
									
										15
									
								
								ccdb/species/factories.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								ccdb/species/factories.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,15 @@
 | 
				
			||||||
 | 
					from factory import DjangoModelFactory, Sequence
 | 
				
			||||||
 | 
					from factory.fuzzy import FuzzyText, FuzzyChoice
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .models import Species
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SpeciesFactory(DjangoModelFactory):
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        model = Species
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    common_name = Sequence(lambda n: 'species{}'.format(n))
 | 
				
			||||||
 | 
					    genus = FuzzyText(length=50)
 | 
				
			||||||
 | 
					    species = FuzzyText(length=50)
 | 
				
			||||||
 | 
					    parasite = FuzzyChoice(choices=[True, False])
 | 
				
			||||||
 | 
					    sort_order = Sequence(lambda n: n)
 | 
				
			||||||
							
								
								
									
										40
									
								
								ccdb/species/migrations/0004_DATA_initial.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								ccdb/species/migrations/0004_DATA_initial.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,40 @@
 | 
				
			||||||
 | 
					from django.db import migrations
 | 
				
			||||||
 | 
					from django.forms import modelform_factory
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from ccdb.utils.data import get_data_sources
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					    def migrate(apps, schema_editor):
 | 
				
			||||||
 | 
					        sources = get_data_sources()
 | 
				
			||||||
 | 
					        if not sources:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        c = sources['db0']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Species = apps.get_model('species', 'Species')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Species.objects.all().delete()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SpeciesForm = modelform_factory(Species, fields='__all__')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for r in c.execute('SELECT * FROM tbl_lu_species;'):
 | 
				
			||||||
 | 
					            form = SpeciesForm(dict(common_name=r[1], genus=r[2],
 | 
				
			||||||
 | 
					                                    species=r[3], parasite=r[4],
 | 
				
			||||||
 | 
					                                    sort_order=int(r[5]) if r[5] else None))
 | 
				
			||||||
 | 
					            if form.is_valid():
 | 
				
			||||||
 | 
					                Species.objects.create(id=r[0], **form.cleaned_data)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                print('species', r[0:], form.errors.as_data())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def rollback(apps, schema_editor):
 | 
				
			||||||
 | 
					        Species = apps.get_model('species', 'Species')
 | 
				
			||||||
 | 
					        Species.objects.all().delete()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('species', '0003_collectionspecies'),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    operations = [
 | 
				
			||||||
 | 
					        migrations.RunPython(migrate, rollback),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
							
								
								
									
										0
									
								
								ccdb/species/tests/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								ccdb/species/tests/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										19
									
								
								ccdb/species/tests/test_models.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								ccdb/species/tests/test_models.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,19 @@
 | 
				
			||||||
 | 
					from django.test import TestCase
 | 
				
			||||||
 | 
					from django.db import IntegrityError, transaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from ..models import Species
 | 
				
			||||||
 | 
					from ..factories import SpeciesFactory
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SpeciesTestCase(TestCase):
 | 
				
			||||||
 | 
					    def test_creation(self):
 | 
				
			||||||
 | 
					        s = SpeciesFactory()
 | 
				
			||||||
 | 
					        self.assertTrue(isinstance(s, Species))
 | 
				
			||||||
 | 
					        self.assertEqual(s.__str__(), s.common_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_uniqueness(self):
 | 
				
			||||||
 | 
					        s1 = SpeciesFactory()
 | 
				
			||||||
 | 
					        with transaction.atomic(), self.assertRaises(IntegrityError):
 | 
				
			||||||
 | 
					            SpeciesFactory(common_name=s1.common_name, species=s1.species)
 | 
				
			||||||
 | 
					        s3 = SpeciesFactory()
 | 
				
			||||||
 | 
					        self.assertTrue(isinstance(s3, Species))
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue