diff --git a/SatNOGS/base/tests/__init__.py b/SatNOGS/base/tests/__init__.py new file mode 100644 index 0000000..101bb9b --- /dev/null +++ b/SatNOGS/base/tests/__init__.py @@ -0,0 +1,87 @@ +import factory + +from datetime import timedelta +from django.utils.timezone import now +from factory import fuzzy + +from base.models import (ANTENNA_BANDS, ANTENNA_TYPES, MODE_CHOICES, + Antenna, Satellite, Station, Transponder, Observation) +from users.tests import UserFactory + + +class TransponderFactory(factory.django.DjangoModelFactory): + """Transponder model factory.""" + description = fuzzy.FuzzyText() + alive = fuzzy.FuzzyChoice(choices=[True, False]) + uplink_low = fuzzy.FuzzyInteger(0, 100) + uplink_high = fuzzy.FuzzyInteger(0, 100) + downlink_low = fuzzy.FuzzyInteger(0, 100) + downlink_high = fuzzy.FuzzyInteger(0, 100) + mode = fuzzy.FuzzyChoice(choices=MODE_CHOICES) + invert = fuzzy.FuzzyChoice(choices=[True, False]) + baud = fuzzy.FuzzyFloat(0, 100) + + class Meta: + model = Transponder + + +class AntennaFactory(factory.django.DjangoModelFactory): + """Antenna model factory.""" + frequency = fuzzy.FuzzyFloat(0, 100) + band = fuzzy.FuzzyChoice(choices=ANTENNA_BANDS) + antenna_type = fuzzy.FuzzyChoice(choices=ANTENNA_TYPES) + + class Meta: + model = Antenna + + +class StationFactory(factory.django.DjangoModelFactory): + """Station model factory.""" + owner = factory.SubFactory(UserFactory) + name = fuzzy.FuzzyText() + image = factory.django.ImageField() + alt = fuzzy.FuzzyInteger(0, 100) + lat = fuzzy.FuzzyFloat(-90, 90) + lng = fuzzy.FuzzyFloat(-180, 180) + featured = fuzzy.FuzzyChoice(choices=[True, False]) + + @factory.post_generation + def antennas(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for antenna in extracted: + self.antenna.add(antenna) + + class Meta: + model = Station + + +class SatelliteFactory(factory.django.DjangoModelFactory): + """Sattelite model factory.""" + norad_cat_id = fuzzy.FuzzyInteger(1, 4000) + name = fuzzy.FuzzyText() + + @factory.post_generation + def transponders(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for transponder in extracted: + self.transponders.add(transponder) + + class Meta: + model = Satellite + + +class ObservationFactory(factory.django.DjangoModelFactory): + """Observation model factory.""" + satellite = factory.SubFactory(SatelliteFactory) + author = factory.SubFactory(UserFactory) + start = fuzzy.FuzzyDateTime(now() - timedelta(days=3), now()) + end = fuzzy.FuzzyDateTime(now(), now() + timedelta(days=3)) + + class Meta: + model = Observation diff --git a/SatNOGS/users/tests/__init__.py b/SatNOGS/users/tests/__init__.py new file mode 100644 index 0000000..6bc01bb --- /dev/null +++ b/SatNOGS/users/tests/__init__.py @@ -0,0 +1,39 @@ +import datetime +import factory + +from factory import fuzzy + +from users.models import User + + +def int2roman(n): + roman_map = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), + ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), + ('IX', 9), ('V', 5), ('IV', 4), ('I', 1)) + + if not (0 < n < 5000): + raise Exception('Out of range error.') + result = '' + for numeral, integer in roman_map: + while n >= integer: + result += numeral + n -= integer + return result + + +class UserFactory(factory.django.DjangoModelFactory): + """User model factory.""" + username = factory.Sequence(lambda n: 'username%s' % n) + first_name = 'John' + last_name = factory.Sequence(lambda n: 'Doe %s' % int2roman(n)) + email = factory.LazyAttribute(lambda o: '%s@example.com' % o.username) + password = factory.PostGenerationMethodCall('set_password', 'passwd') + is_staff = False + is_active = True + is_superuser = False + last_login = datetime.datetime(2012, 1, 1) + date_joined = datetime.datetime(2011, 1, 1) + bio = fuzzy.FuzzyText() + + class Meta: + model = User diff --git a/requirements/local.txt b/requirements/local.txt index b6539af..ba0a69c 100644 --- a/requirements/local.txt +++ b/requirements/local.txt @@ -5,3 +5,4 @@ Sphinx # django-debug-toolbar that works with Django 1.5+ django-debug-toolbar==1.2.1 django-extensions +factory_boy