├── .gitignore ├── .travis.yml ├── Dockerfile ├── README.md ├── airlines ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ├── docker-compose.yml ├── flights ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_auto_20200430_2040.py │ ├── 0003_auto_20200430_2103.py │ ├── 0004_passenger.py │ ├── 0005_auto_20200430_2229.py │ └── __init__.py ├── models.py ├── static │ └── flights │ │ └── styles.css ├── templates │ └── flights │ │ ├── base.html │ │ ├── error.html │ │ ├── flight.html │ │ └── index.html ├── tests.py ├── urls.py └── views.py ├── manage.py ├── pythonTests ├── assert0.py ├── prime.py ├── test0.py ├── test0.sh └── test1.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.7" 4 | # command to install dependencies 5 | install: 6 | - pip install -r requirements.txt 7 | # command to run tests 8 | script: 9 | - python manage.py test 10 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.7 2 | WORKDIR /usr/src/app 3 | ADD requirements.txt /usr/src/app 4 | RUN pip install -r requirements.txt 5 | ADD . /usr/src/app 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unitest-django 2 | -------------------------------------------------------------------------------- /airlines/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saedyousef/unitest-django/e79babfcdcf0db3a0b6f2bb26b9ce498b377b599/airlines/__init__.py -------------------------------------------------------------------------------- /airlines/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for airlines project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'airlines.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /airlines/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for airlines project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.0.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.0/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'tx-f%(w(8qu#iewud#kugu4lku6^ub&08nuimo@)n3o2^i-x(i' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = ['0.0.0.0'] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'flights.apps.FlightsConfig' 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'airlines.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'airlines.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.postgresql', 80 | 'NAME': 'postgres', 81 | 'USER': 'postgres', 82 | 'HOST': 'db', 83 | 'PORT': 5432, 84 | } 85 | } 86 | 87 | 88 | # Password validation 89 | # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators 90 | 91 | AUTH_PASSWORD_VALIDATORS = [ 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 100 | }, 101 | { 102 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 103 | }, 104 | ] 105 | 106 | 107 | # Internationalization 108 | # https://docs.djangoproject.com/en/3.0/topics/i18n/ 109 | 110 | LANGUAGE_CODE = 'en-us' 111 | 112 | TIME_ZONE = 'UTC' 113 | 114 | USE_I18N = True 115 | 116 | USE_L10N = True 117 | 118 | USE_TZ = True 119 | 120 | 121 | # Static files (CSS, JavaScript, Images) 122 | # https://docs.djangoproject.com/en/3.0/howto/static-files/ 123 | 124 | STATIC_URL = '/static/' 125 | -------------------------------------------------------------------------------- /airlines/urls.py: -------------------------------------------------------------------------------- 1 | """airlines URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | 19 | urlpatterns = [ 20 | path('', include('flights.urls')), 21 | path('admin/', admin.site.urls), 22 | ] 23 | -------------------------------------------------------------------------------- /airlines/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for airlines project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'airlines.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | db: 4 | image: postgres 5 | environment: 6 | POSTGRES_DB: "db" 7 | POSTGRES_HOST_AUTH_METHOD: "trust" 8 | migration: 9 | build: . 10 | command: python3 manage.py migrate 11 | volumes: 12 | - .:/usr/src/app 13 | depends_on: 14 | - db 15 | web: 16 | build: . 17 | command: python3 manage.py runserver 0.0.0.0:8000 18 | volumes: 19 | - .:/usr/src/app 20 | ports: 21 | - "8000:8000" 22 | depends_on: 23 | - db 24 | - migration 25 | -------------------------------------------------------------------------------- /flights/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saedyousef/unitest-django/e79babfcdcf0db3a0b6f2bb26b9ce498b377b599/flights/__init__.py -------------------------------------------------------------------------------- /flights/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import Airport, Flight, Passenger 4 | # Register your models here. 5 | 6 | class PassengerInline(admin.StackedInline): 7 | model = Passenger.flights.through 8 | extra = 1 9 | 10 | class FlightAdmin(admin.ModelAdmin): 11 | inlines = [PassengerInline] 12 | 13 | class PassengerAdmin(admin.ModelAdmin): 14 | filter_horizontal = ('flights',) 15 | 16 | admin.site.register(Airport) 17 | admin.site.register(Flight, FlightAdmin) 18 | admin.site.register(Passenger, PassengerAdmin) -------------------------------------------------------------------------------- /flights/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class FlightsConfig(AppConfig): 5 | name = 'flights' 6 | -------------------------------------------------------------------------------- /flights/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.5 on 2020-04-30 20:38 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Flight', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('origin', models.CharField(max_length=64)), 19 | ('destination', models.IntegerField()), 20 | ], 21 | ), 22 | ] 23 | -------------------------------------------------------------------------------- /flights/migrations/0002_auto_20200430_2040.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.5 on 2020-04-30 20:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('flights', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='flight', 15 | name='duration', 16 | field=models.IntegerField(default=1), 17 | preserve_default=False, 18 | ), 19 | migrations.AlterField( 20 | model_name='flight', 21 | name='destination', 22 | field=models.CharField(max_length=64), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /flights/migrations/0003_auto_20200430_2103.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.5 on 2020-04-30 21:03 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('flights', '0002_auto_20200430_2040'), 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Airport', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('code', models.CharField(max_length=3)), 19 | ('city', models.CharField(max_length=64)), 20 | ], 21 | ), 22 | migrations.AlterField( 23 | model_name='flight', 24 | name='destination', 25 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='arrivals', to='flights.Airport'), 26 | ), 27 | migrations.AlterField( 28 | model_name='flight', 29 | name='origin', 30 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='departures', to='flights.Airport'), 31 | ), 32 | ] 33 | -------------------------------------------------------------------------------- /flights/migrations/0004_passenger.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.5 on 2020-04-30 22:23 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('flights', '0003_auto_20200430_2103'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='Passenger', 15 | fields=[ 16 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 17 | ('first', models.CharField(max_length=64)), 18 | ('last', models.CharField(max_length=64)), 19 | ('flights', models.ManyToManyField(blank=True, related_name='passenger', to='flights.Flight')), 20 | ], 21 | ), 22 | ] 23 | -------------------------------------------------------------------------------- /flights/migrations/0005_auto_20200430_2229.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.5 on 2020-04-30 22:29 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('flights', '0004_passenger'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='passenger', 15 | name='flights', 16 | field=models.ManyToManyField(blank=True, related_name='passengers', to='flights.Flight'), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /flights/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saedyousef/unitest-django/e79babfcdcf0db3a0b6f2bb26b9ce498b377b599/flights/migrations/__init__.py -------------------------------------------------------------------------------- /flights/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Airport(models.Model): 6 | code = models.CharField(max_length=3) 7 | city = models.CharField(max_length=64) 8 | 9 | def __str__(self): 10 | return f"{self.city} ({self.code})" 11 | 12 | class Flight(models.Model): 13 | origin = models.ForeignKey(Airport, on_delete = models.CASCADE, related_name="departures") 14 | destination = models.ForeignKey(Airport, on_delete = models.CASCADE, related_name="arrivals") 15 | duration = models.IntegerField() 16 | 17 | def is_valid_flight(self): 18 | return (self.origin != self.destination) and (self.duration >= 0) 19 | 20 | def __str__(self): 21 | return f"{self.id} - {self.origin} to {self.destination} lasting {self.duration} minutes" 22 | 23 | class Passenger(models.Model): 24 | first = models.CharField(max_length=64) 25 | last = models.CharField(max_length=64) 26 | flights = models.ManyToManyField(Flight, blank=True, related_name="passengers") 27 | 28 | def __str__(self): 29 | return f"{self.first} {self.last}" -------------------------------------------------------------------------------- /flights/static/flights/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: black; 3 | } -------------------------------------------------------------------------------- /flights/templates/flights/base.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 |
5 |