This app was created within the scope of Coding challenge presented to Mohammad S.Niaei by Postmedia technical team.
The back-end consistes of two part.
- Core
- API
Core is consists of three classes.
- City
- Country
- Weather
City
is a dataclass. It provides id
, name
, lng
, lat
, country
and state
(if USA).
This data type would be used to obtain coordinates or city name to provide to openweathermap's API.
Country
is a class that get's a json file which has a certain schema and provides list of cities.
Using this information Country
can return
- a
city
object by given name. - list of available cities (Canadian cities).
Providing a city
object Weather can return
- current weather of given city from openweathermap using city name or coordinates.
n
days forecats of given city by name or coordinates (requires a valid API KEY)
The service was created using Django
and Django Ninja
and it provides an API.
The api has three endpoints.
- Available Cities
/api/cities/available
- City information
/api/cities/{city}
- Current Weather for given City
/api/weather/current/{city}
see: api/docs
Using API provided by pmweather
two pages for front-end was created using jquery
and bootstrap
Home page Results on home page
All available cities Results on all available cities
API documentation provided by ninja
Django==4.0.5
django-ninja==0.19.0
orjson==3.7.5
requests==2.28.0
First clone the project:
git clone https://github.com/mshemuni/postmeda.git
Change directory to the project's root:
cd postmeda/
Install Requirements:
pip install -r requirements.txt
and run it by:
python manage.py runserver
Mohammad SHAMEONI NIAEI
example.py
Getting list of cities
curl -X 'GET' \
'http://127.0.0.1:8080/api/cities/available' \
-H 'accept: */*' | jq
Getting a city's information
curl -X 'GET' \
'http://127.0.0.1:8080/api/cities/toronto' \
-H 'accept: */*' | jq
Getting weather data of a given city name from openweathermap
curl -X 'GET' \
'http://127.0.0.1:8080/api/weather/current/toronto' \
-H 'accept: */*' | jq
Note:
the
jq
is optional for pretty viewing on terminal
unittest
s are done for core library using test_core.py
Simply run the file.
Note:
Since
City
is adataclass
I couldn't find a way to test it.
Tests are done for Country
and Weaher
classes
For core test condition as below was tested:
-
TestCountry.test_init
: Check for number of cities available -
TestCountry.test_cities
: Check if first element returned bycities
method is"Abbey"
-
TestCountry.test_get_city_info
: Check if the method returns correct element, from cities -
TestCountry.test_get_city_info_case
: Check if the method returns correct element, from cities even if the city name is given with different cases -
TestCountry.test_get_city_info_spaces
: Check if the method returns correct element, from cities even if there are redundant white-spaces before and after the city name -
TestCountry.test_get_city_info_error
: Check if the method raises correct error if the given city name is not available -
TestWeather.test_init
: Check if the given city is correct -
TestWeather.test_current_by_coordinates
: Check if the data returned by openweathermap belongs to the given city by checking the coordinates -
TestWeather.test_current_by_name
: Chek-ck if the data returned by openweathermap belongs to the given city by checking the coordinates -
TestWeather.test_current_by_name_wrong_api
: Check if the method raise correct error on wrong api key
For the API, the tests are done differently. For api application of our django project we create a test.py
file and
running it by:
Simply run the code below in root directory.
python manage.py test api
APITestCase.test_available_cities
: Check if api returns correct number of element and if the first element is correctAPITestCase.test_get_city
: Check if the given city name returns correct city informationAPITestCase.test_get_city_error
: Check if the error is returned by the api if wrong (not a city) city name is givenAPITestCase.test_current_by_name
: Check if weather data contains correct coordinates for the given cityAPITestCase.test_current_by_name_error
: Check if the error is returned by the api if wrong (not a city) city name is given