Skip to content

Commit

Permalink
Adding tests for display map
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimmons87 authored and abcdefg30 committed Nov 29, 2024
1 parent 7d26549 commit 14d6193
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
5 changes: 5 additions & 0 deletions openra/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from openra.services.map_search import MapSearch
from openra.services.screenshot_repository import ScreenshotRepository
from openra.services.uploaded_file_importer import UploadedFileImporter
from openra.services.openra_master import OpenraMaster
from openra.services.utility import Utility


Expand Down Expand Up @@ -71,6 +72,10 @@ class Container(containers.DeclarativeContainer):
MapSearch
)

openra_master = providers.Singleton(
OpenraMaster
)


container = Container()
container.config.from_dict(settings.__dict__)
9 changes: 9 additions & 0 deletions openra/fakes/openra_master.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Union


class FakeOpenraMaster:

played_count = 123

def get_played_count(self, map_hash: str):
return self.played_count
19 changes: 19 additions & 0 deletions openra/services/openra_master.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List
from openra.models import Maps
import urllib.request
from openra.models import Maps
import json


class OpenraMaster:

def get_played_count(self, map_hash: str):
try:
played_counter = urllib.request.urlopen("http://master.openra.net/map_stats?hash=%s" % map_hash).read().decode()
played_counter = json.loads(played_counter)
if played_counter:
return played_counter["played"]
else:
return 0
except BaseException:
return None
4 changes: 2 additions & 2 deletions openra/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Meta:
advanced_map = False
lua = False
posted = timezone.now()
viewed = 0
downloaded = 0
viewed = factory.Faker('random_number')
downloaded = factory.Faker('random_number')
rating = 0.0
amount_reports = 0
policy_cc = False
Expand Down
29 changes: 26 additions & 3 deletions openra/tests/routes/test_route_display_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fs.memoryfs import MemoryFS
from openra.classes.file_location import FileLocation
from openra.fakes.map_file_repository import FakeMapFileRepository
from openra.fakes.openra_master import FakeOpenraMaster
from openra.tests.factories import MapsFactory
from openra.tests.routes.test_route_base import TestRouteBase
from openra.containers import container
Expand All @@ -17,7 +18,8 @@ class TestRouteDisplayMap(TestRouteBase):

def test_route_can_be_accessed_by_any_user(self):
overrides = container.override_providers(
map_file_repository=Singleton(FakeMapFileRepository)
map_file_repository=Singleton(FakeMapFileRepository),
openra_master=Singleton(FakeOpenraMaster)
)

data_fs = MemoryFS()
Expand All @@ -30,10 +32,17 @@ def test_route_can_be_accessed_by_any_user(self):
)
]

played_count = 1234

container.openra_master().played_count = played_count

FakeMapFileRepository.map_exists = False

model = MapsFactory(lua=True)
model.lua = True
model = MapsFactory(
lua=True,
downloading=True,
bounds="a,b,123,456",
)

response = self.get(
{},
Expand All @@ -50,6 +59,20 @@ def test_route_can_be_accessed_by_any_user(self):
[
str(model.title),
'sample_lua_name',
model.author,
model.user.username,
model.game_mod,
model.categories,
model.players,
model.tileset[0].upper() + model.tileset[1:],
"123x456",
model.mapformat,
model.posted.strftime("%b. %d - %Y"),
'allowed',
model.parser,
model.viewed,
model.downloaded,
played_count,
],
)

Expand Down
17 changes: 6 additions & 11 deletions openra/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from openra.services.map_search import MapSearch
from openra.classes.pagination import Pagination
from openra.services.screenshot_repository import ScreenshotRepository
from openra.services.openra_master import OpenraMaster

# TODO: Fix the code and reenable some of these warnings
# pylint: disable=invalid-name
Expand Down Expand Up @@ -368,7 +369,6 @@ def map_update_map_info(request, map_id):
@inject
def map_upload_screenshot(request, map_id,
screenshot_repository: ScreenshotRepository = Provide['screenshot_repository']

):
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/')
Expand Down Expand Up @@ -426,7 +426,8 @@ def map_post_comment(request, map_id):

@inject
def display_map(request, map_id,
map_file_repository: MapFileRepository = Provide['map_file_repository']
map_file_repository: MapFileRepository = Provide['map_file_repository'],
openra_master: OpenraMaster = Provide['openra_master']
):
try:
location = map_file_repository.get_oramap_path(map_id)
Expand Down Expand Up @@ -476,15 +477,9 @@ def display_map(request, map_id,

screenshots = Screenshots.objects.filter(ex_name="maps", ex_id=map_id)

try:
played_counter = urllib.request.urlopen("http://master.openra.net/map_stats?hash=%s" % model.map_hash).read().decode()
played_counter = json.loads(played_counter)
if played_counter:
played_counter = played_counter["played"]
else:
played_counter = 0
except BaseException:
played_counter = 'unknown'
played_counter = openra_master.get_played_count(model.map_hash)
if played_counter is None:
played_counter = 'Unknown'

ratesAmount = Rating.objects.filter(ex_id=model.id, ex_name='map')
ratesAmount = len(ratesAmount)
Expand Down

0 comments on commit 14d6193

Please sign in to comment.