Repository: CaffeineDuck/nepse-api (language: python)

Issue:
Tests not managed properly
The tests are crammed into one file making it look like a mess to look at.
Please fix that.


Discussion / hints:

Fix patch:
diff --git a/nepse/security/core.py b/nepse/security/core.py
index 455f0f2..0c883d1 100644
--- a/nepse/security/core.py
+++ b/nepse/security/core.py
@@ -7,7 +7,9 @@
 from nepse.errors import NotFound
 from nepse.security.types import (
     CompanyHistory,
+    Gainer,
     LiveSecurityTrade,
+    Loser,
     SecurityResponse,
     SecurityResponseDetailed,
 )
@@ -199,3 +201,29 @@ async def get_company_history(self, company_id: int) -> List[CompanyHistory]:
             )
         )
         return [CompanyHistory(**date) for date in data]
+
+    async def get_top_gainers(self) -> Gainer:
+        """Returns the top gainers for the current day
+
+        Returns:
+            TopGainers: List of Objects containing the top gainers for the day
+        """
+        data = humps.decamelize(
+            await self._client_wrapper._get_json(
+                f"{BASE_NOTS_URL}/top-ten/top-gainer?all=true"
+            )
+        )
+        return [Gainer(**model) for model in data]
+
+    async def get_top_losers(self) -> Loser:
+        """Returns the top losers for the current day
+
+        Returns:
+            TopLosers: List of Objects containing the top losers for the day
+        """
+        data = humps.decamelize(
+            await self._client_wrapper._get_json(
+                f"{BASE_NOTS_URL}/top-ten/top-loser?all=true"
+            )
+        )
+        return [Loser(**model) for model in data]
diff --git a/nepse/security/types.py b/nepse/security/types.py
index 50d62e6..028ac5c 100644
--- a/nepse/security/types.py
+++ b/nepse/security/types.py
@@ -177,3 +177,23 @@ class CompanyHistory:
     last_traded_price: int
     total_traded_quantity: int
     close_price: int
+
+
+@dataclass
+class TopGainLose:
+    symbol: str
+    ltp: int
+    point_change: float
+    percentage_change: float
+    security_name: str
+    security_id: int
+
+
+@dataclass
+class Gainer(TopGainLose):
+    pass
+
+
+@dataclass
+class Loser(TopGainLose):
+    pass


Test patch:
diff --git a/tests/broker_test.py b/tests/broker_test.py
new file mode 100644
index 0000000..869965e
--- /dev/null
+++ b/tests/broker_test.py
@@ -0,0 +1,28 @@
+import sys
+
+sys.path.append("..")
+
+import httpx
+import pytest
+
+from nepse import Client
+
+
+@pytest.mark.asyncio
+async def test_broker():
+    async with httpx.AsyncClient() as async_client:
+
+        client = Client(httpx_client=async_client)
+        wrapper_broker = await client.broker_client.get_broker(id=116)
+
+    assert wrapper_broker.member_name == "Ashutosh Brokerage & Securities (PVT) Ltd."
+
+
+@pytest.mark.asyncio
+async def test_brokers():
+    async with httpx.AsyncClient() as async_client:
+
+        client = Client(httpx_client=async_client)
+        wrapper_broker = await client.broker_client.get_brokers()
+
+    assert wrapper_broker
diff --git a/tests/market_test.py b/tests/market_test.py
new file mode 100644
index 0000000..20fd273
--- /dev/null
+++ b/tests/market_test.py
@@ -0,0 +1,51 @@
+from http import client
+import sys
+
+from nepse.utils import _ClientWrapperHTTPX
+
+sys.path.append("..")
+
+import httpx
+import pytest
+
+from nepse import Client
+
+
+@pytest.mark.asyncio
+async def test_market_open():
+    async with httpx.AsyncClient() as async_client:
+
+        client = Client(httpx_client=async_client)
+        market_wrapper_data = await client.market_client.market_is_open()
+
+    async with httpx.AsyncClient() as async_client:
+
+        wrapper_client = _ClientWrapperHTTPX(async_client)
+        response = await wrapper_client._get_json(
+            "https://newweb.nepalstock.com/api/nots/nepse-data/market-open"
+        )
+        if response["isOpen"] != "CLOSE":
+            real_data = True
+        real_data = False
+
+    assert real_data == market_wrapper_data
+
+
+@pytest.mark.asyncio
+async def test_IPO():
+    async with httpx.AsyncClient() as async_client:
+
+        client = Client(httpx_client=async_client)
+        ipo_result = await client.market_client.check_IPO("MLBSL", 1301310000123606)
+
+    assert ipo_result == False
+
+
+@pytest.mark.asyncio
+async def test_market_summaries():
+    async with httpx.AsyncClient() as async_client:
+
+        client = Client(httpx_client=async_client)
+        market_summaries = await client.market_client.get_market_summaries()
+
+    assert market_summaries
diff --git a/basic_test.py b/tests/security_test.py
similarity index 53%
rename from basic_test.py
rename to tests/security_test.py
index 44b3ee7..c5c3f47 100644
--- a/basic_test.py
+++ b/tests/security_test.py
@@ -1,3 +1,7 @@
+import sys
+
+sys.path.append("..")
+
 import asyncio
 from dataclasses import asdict
 
@@ -11,8 +15,9 @@
 
 @pytest.mark.asyncio
 async def test_all_companies():
+    """This is a test for seeing if the all companies method works"""
+
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
         nepse_client = Client(httpx_client=async_client)
         api_data = await nepse_client.security_client.get_companies()
         assert api_data
@@ -22,8 +27,9 @@ async def test_all_companies():
 
 @pytest.mark.asyncio
 async def test_right_detailed_security_data():
+    """This is a test for seeing if the `get_company_detailed` method provides right data"""
+
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
         wrapper_client = _ClientWrapperHTTPX(async_client)
         data = await wrapper_client._post_json_defualt_body(
             "https://newweb.nepalstock.com/api/nots/security/2792"
@@ -31,7 +37,6 @@ async def test_right_detailed_security_data():
         api_data = humps.decamelize(data)
 
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
         nepse_client = Client(httpx_client=async_client)
         wrapper_data = asdict(
             await nepse_client.security_client.get_company_detailed(2792)
@@ -42,8 +47,9 @@ async def test_right_detailed_security_data():
 
 @pytest.mark.asyncio
 async def test_right_security_data():
+    """This is a test for seeing if the `get_company` method provides right data"""
+
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
         wrapper_client = _ClientWrapperHTTPX(async_client)
         data = await wrapper_client._get_json(
             "https://newweb.nepalstock.com/api/nots/securityDailyTradeStat/58"
@@ -54,7 +60,6 @@ async def test_right_security_data():
         api_data = humps.decamelize(security)
 
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
         nepse_client = Client(httpx_client=async_client)
         wrapper_data = asdict(
             await nepse_client.security_client.get_company(symbol="UPPER")
@@ -63,65 +68,10 @@ async def test_right_security_data():
     assert api_data == wrapper_data
 
 
-@pytest.mark.asyncio
-async def test_market_open():
-    async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
-
-        client = Client(httpx_client=async_client)
-        market_wrapper_data = await client.market_client.market_is_open()
-
-    async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
-
-        wrapper_client = _ClientWrapperHTTPX(async_client)
-        response = await wrapper_client._get_json(
-            "https://newweb.nepalstock.com/api/nots/nepse-data/market-open"
-        )
-        if response["isOpen"] != "CLOSE":
-            real_data = True
-        real_data = False
-
-    assert real_data == market_wrapper_data
-
-
-@pytest.mark.asyncio
-async def test_broker():
-    async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
-
-        client = Client(httpx_client=async_client)
-        wrapper_broker = await client.broker_client.get_broker(id=116)
-
-    assert wrapper_broker.member_name == "Ashutosh Brokerage & Securities (PVT) Ltd."
-
-
-@pytest.mark.asyncio
-async def test_brokers():
-    async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
-
-        client = Client(httpx_client=async_client)
-        wrapper_broker = await client.broker_client.get_brokers()
-
-    assert wrapper_broker
-
-
-@pytest.mark.asyncio
-async def test_IPO():
-    async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
-
-        client = Client(httpx_client=async_client)
-        ipo_result = await client.market_client.check_IPO("MLBSL", 1301310000123606)
-
-    assert ipo_result == False
-
-
 @pytest.mark.asyncio
 async def test_company_history():
+    """This is a test for seeing if the `get_company_history` method provides right data"""
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
 
         client = Client(httpx_client=async_client)
         wrapper_data = [
@@ -130,7 +80,6 @@ async def test_company_history():
         ]
 
     async with httpx.AsyncClient() as async_client:
-        await asyncio.sleep(0.1)
 
         wrapper_client = _ClientWrapperHTTPX(async_client)
         response = await wrapper_client._post_json_defualt_body(
@@ -139,13 +88,3 @@ async def test_company_history():
         api_data = humps.decamelize(response)
 
     assert api_data == wrapper_data
-
-
-# @pytest.mark.asyncio
-# async def test_floorsheet():
-#     async with httpx.AsyncClient() as async_client:
-
-#         client = Client(httpx_client=async_client)
-#         floorsheet = await client.market_client.get_floorsheets()
-
-#     assert floorsheet