Skip to content

Commit a23b4aa

Browse files
authored
Merge pull request #27 from letsbuilda/sd/gh26
Move random number generation to generators module
2 parents e9d9f9b + 09f2ce5 commit a23b4aa

8 files changed

Lines changed: 56 additions & 36 deletions

File tree

docs/source/changelog.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
Changelog
55
=========
66

7+
- :release:`1.8.0 <11th March 2023>`
8+
- :feature:`26` Move random number generation to generators module
9+
710
- :release:`1.7.0 <11th March 2023>`
8-
- :feature:`24` Add random number generation
11+
- :feature:`24` Add bulk uuid generation
912

1013
- :release:`1.6.0 <09th March 2023>`
1114
- :feature:`21` Add random number generation

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ addopts = "tests -r a -v --doctest-modules src"
5858
max-line-length = 120
5959

6060
[tool.pylint.messages_control]
61-
disable = "R0903"
61+
disable = "C0103,R0903,E0213"
6262
# justifications:
63+
# C0103 (invalid-name) - pydantic
6364
# R0903 (too-few-public-methods) - models
65+
# E0213 (no-self-argument) - pydantic

src/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Public API for our projects"""
22

3-
__version__ = "1.7.0"
3+
__version__ = "1.8.0"

src/api/modules/generators.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
"""Generation of things"""
22

33
import uuid
4+
from random import randint
45
from typing import Literal
56

67
from fastapi import APIRouter
78

89
# pylint: disable-next=no-name-in-module
9-
from pydantic import BaseModel, Field
10+
from pydantic import BaseModel, Field, validator
1011

1112
router_generators = APIRouter(prefix="/generators", tags=["generators"])
1213

1314

14-
class UUIDs(BaseModel):
15-
"""Model to hold a list of UUIDs"""
16-
17-
uuids: list[str]
18-
19-
2015
class UUIDConfig(BaseModel):
2116
"""Model to hold configuration for UUID generation"""
2217

2318
uuid_type: Literal[1, 4]
2419
quantity: int = Field(gt=0, default=1)
2520

2621

22+
class UUIDs(BaseModel):
23+
"""Model to hold a list of UUIDs"""
24+
25+
uuids: list[str]
26+
27+
2728
@router_generators.post("/uuids/")
2829
async def bulk_uuids(config: UUIDConfig) -> UUIDs:
2930
"""Generate bulk UUIDs"""
@@ -35,3 +36,32 @@ async def bulk_uuids(config: UUIDConfig) -> UUIDs:
3536
raise ValueError(f"unsupported UUID type: {config.uuid_type}")
3637
uuids = [str(function()) for _ in range(config.quantity)]
3738
return UUIDs(uuids=uuids)
39+
40+
41+
class NumbersConfig(BaseModel):
42+
"""Model to hold configuration for number generation"""
43+
44+
lower_bound: int | None = Field(default=1)
45+
upper_bound: int | None = Field(default=1)
46+
quantity: int | None = Field(gt=0, default=1)
47+
48+
@validator("upper_bound")
49+
def upper_bound_must_be_greater_than_lower_bound(cls, v, values):
50+
"""Confirm upper bound is greater than lower bound"""
51+
if "lower_bound" in values and v < values["lower_bound"]:
52+
raise ValueError("upper bound must be greater than lower bound")
53+
return v
54+
55+
56+
class Numbers(BaseModel):
57+
"""Model to hold a list of numbers"""
58+
59+
numbers: list[int]
60+
total: int
61+
62+
63+
@router_generators.post("/random-numbers/")
64+
async def random_numbers(config: NumbersConfig) -> Numbers:
65+
"""Generate bulk random numbers"""
66+
numbers = [randint(config.lower_bound, config.upper_bound) for _ in range(config.quantity)]
67+
return Numbers(numbers=numbers, total=sum(numbers))

src/api/server.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""API server definition"""
22

33
from os import getenv
4-
from random import randint
54

65
import sentry_sdk
76
from fastapi import APIRouter, FastAPI
@@ -33,13 +32,6 @@ class TextModel(BaseModel):
3332
text: str
3433

3534

36-
class Numbers(BaseModel):
37-
"""Model to hold a list of numbers"""
38-
39-
numbers: list[int]
40-
total: int
41-
42-
4335
app = FastAPI(
4436
title="Let's Build A API",
4537
description="An API to host Let's Build A's projects",
@@ -69,13 +61,5 @@ async def uwuify_route(text: TextModel):
6961
return {"text": uwuify(text.text)}
7062

7163

72-
@router_fun.get("/random-numbers/{quantity}/{range_high}")
73-
async def random_numbers(quantity: int, range_high: int) -> Numbers:
74-
"""Generate bulk random numbers"""
75-
numbers = [randint(1, range_high) for _ in range(quantity)]
76-
return Numbers(numbers=numbers, total=sum(numbers))
77-
78-
7964
app.include_router(router_fun)
80-
8165
app.include_router(router_generators)

tests/modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test the modules"""
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@
99
client = TestClient(app)
1010

1111

12-
def test_read_random_numbers():
12+
def test_bulk_uuids():
1313
response = client.post("/generators/uuids/", json={"uuid_type": 1, "quantity": 1})
1414
assert response.status_code == 200
1515
first = response.json()["uuids"][0]
1616
assert isinstance(uuid.UUID(first), uuid.UUID)
17+
18+
19+
def test_random_numbers():
20+
response = client.post("/generators/random-numbers/", json={})
21+
assert response.status_code == 200
22+
assert response.json() == {
23+
"numbers": [1],
24+
"total": 1,
25+
}

tests/test_server.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,3 @@ def test_read_main():
1515
"message": "Welcome to the API",
1616
"version": __version__,
1717
}
18-
19-
20-
def test_read_random_numbers():
21-
response = client.get("/fun/random-numbers/1/1")
22-
assert response.status_code == 200
23-
assert response.json() == {
24-
"numbers": [1],
25-
"total": 1,
26-
}

0 commit comments

Comments
 (0)