def handle(self, *args, **options): driver_group = Group.objects.get(name=Groups.Driver.name) passenger_group = Group.objects.get(name=Groups.Passenger.name) self.stdout.write(self.style.SUCCESS('Creating 5 cars')) for _ in tqdm(range(5)): CarFactory.create() self.stdout.write(self.style.SUCCESS('Creating 5 drivers')) drivers = [] for _ in tqdm(range(5)): drivers.append(UserFactory.create(groups=[driver_group]).username) passengers = [] self.stdout.write(self.style.SUCCESS('Creating 10 passengers')) for _ in tqdm(range(10)): passengers.append( UserFactory.create(groups=[passenger_group]).username) self.stdout.write(self.style.SUCCESS('Creating 5 projects')) for _ in tqdm(range(5)): ProjectFactory.create() self.stdout.write(self.style.SUCCESS('Creating 50 drives')) all_drivers = list(User.objects.filter(groups=driver_group)) all_passengers = list(User.objects.filter(groups=passenger_group)) all_projects = list(Project.objects.all()) for _ in tqdm(range(50)): DriveFactory.create( passenger=random.choice(all_passengers), project=random.choice(all_projects), driver=random.choice(all_drivers), ) self.stdout.write( self.style.SUCCESS('Database successfully populated')) self.stdout.write('=' * 50) self.stdout.write(self.style.SUCCESS('Newly created drivers:')) for index, username in enumerate(drivers): self.stdout.write( self.style.SUCCESS('{index}. {username}'.format( index=index, username=username, ))) self.stdout.write(self.style.SUCCESS('Newly created passengers:')) for index, username in enumerate(passengers): self.stdout.write( self.style.SUCCESS('{index}. {username}'.format( index=index, username=username, ))) default_user = User.objects.filter( email='*****@*****.**').first() driver_group.user_set.add(default_user) passenger_group.user_set.add(default_user)
def test_can_retrieve_only_my_drives(self): new_driver = UserFactory.make(groups=[self.drivers_group]) DriveFactory.make_batch(size=4, driver=new_driver, passenger=self.passenger) self.client.force_login(self.driver) res = self.client.get(self.url) self.assertEqual(res.status_code, status.HTTP_200_OK) drives = res.data self.assertEqual(len(drives), len(self.drives)) self.assertEqual({d["driver"]["id"] for d in drives}, {self.driver.id}) for drive in drives: self.assertEqual(drive["driver"]["id"], self.driver.id)
def handle(self, *args, **options): """Populate database with fake objects.""" self.stdout.write(self.style.SUCCESS('Creating 30 cars')) for _ in tqdm(range(30)): CarFactory.create() self.stdout.write(self.style.SUCCESS('Creating 50 users')) usernames = [] for _ in tqdm(range(50)): usernames.append(UserFactory.create().username) self.stdout.write(self.style.SUCCESS('Creating 150 passengers')) for _ in tqdm(range(150)): PassengerFactory.create() self.stdout.write(self.style.SUCCESS('Creating 30 projects')) for _ in tqdm(range(30)): ProjectFactory.create() self.stdout.write(self.style.SUCCESS('Creating 500 drives')) all_users = list(User.objects.all()) all_passengers = list(Passenger.objects.all()) all_projects = list(Project.objects.all()) for _ in tqdm(range(500)): DriveFactory.create( passengers=random.sample(all_passengers, random.randint(1, 4)), project=random.choice(all_projects), driver=random.choice(all_users), ) self.stdout.write( self.style.SUCCESS('Database successfully populated')) self.stdout.write('=' * 50) self.stdout.write(self.style.SUCCESS('Newly created users:')) for index, username in enumerate(usernames): self.stdout.write( self.style.SUCCESS('{index}. {username}'.format( index=index, username=username, )))
def setUp(self): self.url = reverse("drives") self.drivers_group = Group.objects.get(name=Groups.Driver.name) self.passengers_group = Group.objects.get(name=Groups.Passenger.name) self.car = CarFactory.make(id=29) self.project = ProjectFactory.make(id=42, country="UA") self.driver = UserFactory.make(id=51, groups=[self.drivers_group]) self.passenger = UserFactory.make( id=35, rsa_pub_e=257, rsa_priv_d=30593, rsa_modulus_n=50927, groups=[self.passengers_group], ) self.drives = DriveFactory.make_batch(size=10, driver=self.driver, passenger=self.passenger)
def handle(self, *args, **options): driver_group = Group.objects.get(name=Groups.Driver.name) passenger_group = Group.objects.get(name=Groups.Passenger.name) def info(message): self.stdout.write(self.style.SUCCESS(message)) cars = [ CarFactory.create( plates="UA000000", description="Default Car", fuel_consumption=10.0, country="UA", ), ] info("Creating 5 random cars") for _ in tqdm(range(5)): cars.append(CarFactory.create()) drivers = [ UserFactory.create( username="******", first_name="Default", last_name="Driver", country="UA", groups=[driver_group], ), ] info("Creating 5 random drivers") for _ in tqdm(range(5)): drivers.append(UserFactory.create(groups=[driver_group])) passengers = [ UserFactory.create( username="******", first_name="Default", last_name="Passenger", country="UA", groups=[passenger_group], ), ] info("Creating 5 random passengers") for _ in tqdm(range(5)): passengers.append(UserFactory.create(groups=[passenger_group])) projects = [ ProjectFactory.create( title="Default Project", description="Default Project", country="UA", ) ] info("Creating 5 random projects") for _ in tqdm(range(5)): projects.append(ProjectFactory.create()) DriveFactory.create( driver=drivers[0], project=projects[0], passenger=passengers[0], car=cars[0], start_mileage=1, end_mileage=100, description="Default verified drive", start_location="Start", end_location="End", is_verified=True, ) DriveFactory.create( driver=drivers[0], project=projects[0], passenger=passengers[0], car=cars[0], start_mileage=1, end_mileage=100, description="Default unverified drive", start_location="Start", end_location="End", is_verified=False, ) info("Creating 50 random drives") for _ in tqdm(range(50)): DriveFactory.create( passenger=random.choice(passengers), project=random.choice(projects), driver=random.choice(drivers), car=random.choice(cars), ) info("Database successfully populated with random data") info("=" * 60) info(f"{'Newly created drivers (username : password)':>56}") for index, user in enumerate(drivers, start=1): info(f"{index:02d}. {user.username:>40} : {UserFactory.password}") info("=" * 60) info(f"{'Newly created passengers (username : password)':>56}") for index, user in enumerate(passengers, start=1): info(f"{index:02d}. {user.username:>40} : {UserFactory.password}")
def test_fuel_consumption_is_valid(self): drive = DriveFactory(start_mileage=100300, end_mileage=100500) drive.car.fuel_consumption = 9.73 self.assertEqual(drive.fuel_consumption, 19.46)
def test_diff_mileage(self): drive = DriveFactory.make(start_mileage=100300, end_mileage=100800) self.assertEqual(drive.diff_mileage, 500)