Skip to content

Commit

Permalink
Merge pull request #24 from LukePeltier/Unit-Testing
Browse files Browse the repository at this point in the history
Start of unit testing automatically on push to main and pull request to main
  • Loading branch information
LukePeltier authored May 4, 2021
2 parents 8f0e373 + b35bc3a commit 03e0b55
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 176 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Django CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
max-parallel: 4
matrix:
python-version: [3.9]

services:
mysql:
image: mariadb:latest
env:
MYSQL_ROOT_PASSWORD: ${{secrets.MYSQLPW}}
MYSQL_DATABASE: tenmansstatistics
ports:
- 3306:3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10

env:
DJANGO_SETTINGS_MODULE: LukeWebsite.settings.testing

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Verify MariaDB connection
run: |
docker ps
sudo mysql -h localhost -P 3306 --protocol=tcp -e 'SHOW DATABASES' -uroot --password=${{secrets.MYSQLPW}}
- name: Install Dependencies
working-directory: ./src
run: |
python -m pip install --upgrade pip
pip install pip-tools
pip-sync
- name: Write DB Conf File
working-directory: ./src/LukeWebsite
env:
DB_CONF: ${{ secrets.DB_CONF }}
run: |
mkdir ./privateSettings
echo "$DB_CONF" >> ./privateSettings/db.cnf
- name: Write API Key File
working-directory: ./src/LukeWebsite/tenMans
env:
API_FILE: ${{ secrets.API_KEY }}
run: |
mkdir ./conf
echo "$API_FILE" >> ./conf/api.ini
- name: Build Objects
working-directory: ./src/LukeWebsite
run: |
python manage.py collectstatic --noinput --settings=LukeWebsite.settings.testing
python manage.py migrate --settings=LukeWebsite.settings.testing
- name: Run Tests
working-directory: ./src/LukeWebsite
run: |
pytest
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name: New Release

# Controls when the action will run.
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
release:
Expand Down Expand Up @@ -31,11 +31,9 @@ jobs:
cd src
source venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pip-tools
pip-sync
cd LukeWebsite
python manage.py collectstatic --noinput --settings=LukeWebsite.settings.prod
python manage.py migrate --settings=LukeWebsite.settings.prod
sudo systemctl restart apache2
2 changes: 0 additions & 2 deletions src/LukeWebsite/LukeWebsite/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
# SECURITY WARNING: don't run with debug turned on in production!

ALLOWED_HOSTS = []

RIOT_API_KEY = 'RGAPI-5bbe37b9-51da-4c3b-bbd6-dcea99f38e1c'
20 changes: 20 additions & 0 deletions src/LukeWebsite/LukeWebsite/settings/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from LukeWebsite.settings.common import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '++j8rg@ybk83g1ewqof7++roq!v)i5xg=ucd#%0nryja!)%m03')
DEBUG = True

# SECURITY WARNING: don't run with debug turned on in production!

ALLOWED_HOSTS = []

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'privateSettings', 'db.cnf')
}
}
}
69 changes: 69 additions & 0 deletions src/LukeWebsite/tenMans/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import datetime
import factory
from tenMans.models import Champion, Game, GameBan, GameLaner, Lane, Player
import factory.fuzzy
from dateutil import tz


class GameFactory(factory.django.DjangoModelFactory):
class Meta:
model = Game

gameNumber = factory.Sequence(lambda n: '%d' % n)
gameDate = factory.fuzzy.FuzzyDateTime(datetime.datetime(2020, 6, 1, tzinfo=tz.gettz('US/Central')))
gameBlueWins = factory.fuzzy.FuzzyChoice([True, False])
gameRandomTeams = False
gameMemeStatus = False
gameDuration = factory.fuzzy.FuzzyInteger(1, 3600)


class LaneFactory(factory.django.DjangoModelFactory):
class Meta:
model = Lane


class PlayerFactory(factory.django.DjangoModelFactory):
class Meta:
model = Player

playerName = factory.Faker('name')


class ChampionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Champion

championName = factory.fuzzy.FuzzyText()


class GameLanerFactory(factory.django.DjangoModelFactory):
class Meta:
model = GameLaner

game = factory.Iterator(Game.objects.all())
lane = factory.Iterator(Lane.objects.all())
blueTeam = factory.fuzzy.FuzzyChoice([True, False])
player = factory.Iterator(Player.objects.all())
champion = factory.Iterator(Champion.objects.all())
draftOrder = factory.fuzzy.FuzzyChoice([None, 1, 2, 3, 4, 5, 6, 7, 8])
championSelectOrder = factory.fuzzy.FuzzyChoice([None, 1, 2, 3, 4, 5, 6, 7])


class GameLanerNoSupport(GameLanerFactory):
lane = factory.Iterator(Lane.objects.exclude(laneName__exact="Support"))


class GameLanerRandomGameFactory(GameLanerFactory):
class Meta:
model = GameLaner

game = factory.Iterator(Game.objects.filter(gameRandomTeams=True))
draftOrder = None


class GameBanFactory(factory.django.DjangoModelFactory):
class Meta:
model = GameBan

game = factory.Iterator(Game.objects.filter(gameRandomTeams=False))
champion = factory.Iterator(Champion.objects.all())
12 changes: 3 additions & 9 deletions src/LukeWebsite/tenMans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,12 @@ def playerParticipatedInGame(self, game):
player__exact=self.id, game__exact=game.id).exists()

def getAveragePulledBans(self):
bansTargeted = GameBan.objects.filter(
targetPlayer__exact=self.id).count()
gamesWithBans = GameBan.objects.values('game').distinct()
bansTargeted = GameBan.objects.filter(targetPlayer__exact=self.id).count()
gamesWithBans = GameBan.objects.filter(targetPlayer__exact=self.id).values('game').distinct()
totalGameCount = gamesWithBans.count()
if totalGameCount == 0:
return "N/A"
playerGamesWithBansCount = 0
for gameBanDict in gamesWithBans:
gameObject = Game.objects.get(id__exact=gameBanDict['game'])
if(self.playerParticipatedInGame(gameObject)):
playerGamesWithBansCount += 1
return round(bansTargeted / playerGamesWithBansCount, 2)
return round(bansTargeted / totalGameCount, 2)

def getMostBannedChampionString(self):
bansTotal = GameBan.objects.filter(targetPlayer__exact=self.id)
Expand Down
Loading

0 comments on commit 03e0b55

Please sign in to comment.