Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datetime properties and validations #94

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ temp
venv/*
dist
build
.venv
.history
.vscode
.coverage
__pycache__
*.pdf
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Given a version number MAJOR.MINOR.PATCH, increment:


## [Unreleased]
### Fixed
- CreditNote datetime properties

## [0.16.0] - 2025-03-31
### Added
Expand Down
5 changes: 3 additions & 2 deletions starkinfra/creditnote/__creditnote.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date, datetime
from .invoice.__invoice import Invoice
from .invoice.__invoice import _resource as _invoice_resource
from ..creditsigner.__creditsigner import CreditSigner
Expand All @@ -7,7 +8,7 @@
from ..utils import rest
from starkcore.utils.api import from_api_json
from starkcore.utils.resource import Resource
from starkcore.utils.checks import check_datetime, check_date
from starkcore.utils.checks import check_datetime, check_date, check_datetime_or_date


class CreditNote(Resource):
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(self, template_id, name, tax_id, scheduled, invoices, payment, sign
self.template_id = template_id
self.name = name
self.tax_id = tax_id
self.scheduled = scheduled
self.scheduled = check_datetime_or_date(scheduled)
self.invoices = _parse_invoices(invoices)
self.signers = _parse_signers(signers)
self.external_id = external_id
Expand Down
2 changes: 1 addition & 1 deletion starkinfra/creditnote/invoice/__invoice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from starkcore.utils.api import from_api_json
from starkcore.utils.resource import Resource
from starkcore.utils.checks import check_datetime, check_datetime_or_date, check_timedelta
from starkcore.utils.checks import check_datetime, check_timedelta, check_datetime_or_date
from .__discount import Discount
from .__discount import resource as _discount_resource
from .__description import Description
Expand Down
23 changes: 19 additions & 4 deletions tests/sdk/testCreditNote.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import starkinfra
from unittest import TestCase, main
from tests.utils.user import exampleProject
from tests.utils.creditNote import generateExampleCreditNoteJson
from tests.utils.creditNote import generateExampleCreditNoteIsoDatetimeJson, generateExampleCreditNoteJson, generateExampleCreditNoteStringDatesJson


starkinfra.user = exampleProject
Expand All @@ -10,11 +10,26 @@
class TestCreditNotePost(TestCase):

def test_success(self):
notes = generateExampleCreditNoteJson(n=1)
notes = starkinfra.creditnote.create(notes)
for note in notes:
request = generateExampleCreditNoteJson(n=1)
response = starkinfra.creditnote.create(request)
self.assertTrue(len(request) == len(response))
for note in response:
self.assertIsNotNone(note.id)

def test_success_iso_datetime(self):
request = generateExampleCreditNoteIsoDatetimeJson(n=1)
response = starkinfra.creditnote.create(request)
self.assertTrue(len(request) == len(response))
for note in response:
self.assertIsNotNone(note.id)

def test_success_string_datetime(self):
request = generateExampleCreditNoteStringDatesJson(n=1)
response = starkinfra.creditnote.create(request)
self.assertTrue(len(request) == len(response))
for note in response:
self.assertIsNotNone(note.id)


class TestCreditNoteQuery(TestCase):

Expand Down
132 changes: 80 additions & 52 deletions tests/utils/creditNote.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
# coding=utf-8
from uuid import uuid4
from copy import deepcopy
from datetime import datetime
from random import randint, choice
from datetime import timedelta, datetime
from datetime import timedelta, datetime, date
from .user import template_id
from .names.names import get_full_name
from .taxIdGenerator import TaxIdGenerator
from .date import randomDatetimeBetween, randomFutureDatetime
from .date import futureDateTime, randomDateBetween, randomDatetimeBetween, randomFutureDate, randomFutureDatetime
from starkinfra import CreditNote
from starkinfra.creditsigner import CreditSigner
from starkinfra.creditnote import Invoice, Transfer, Description


def _generateCreditNote():
scheduled = futureDateTime(days=1)
nominal_amount = randint(100000, 1000000)
invoice_nominal_amount = nominal_amount // 2

return CreditNote(
template_id=template_id,
name="Jamie Lannister",
tax_id="012.345.678-90",
nominal_amount=100000,
scheduled="2022-04-28",
invoices=[
Invoice(
due="2023-06-25",
amount=120000,
)
],
name=get_full_name(),
tax_id=TaxIdGenerator.taxId(),
nominal_amount=nominal_amount,
scheduled=datetime.strftime(scheduled, "%Y-%m-%d"),
expiration=timedelta(days=1),
invoices=generateExampleInvoiceStringDateJson(n=randint(3, 4), note_nominal_amount=invoice_nominal_amount,
note_scheduled=scheduled),
tags=["test", "testing"],
payment=_generate_payment(),
signers=[
CreditSigner(
name="Jamie Lannister",
contact="[email protected]",
method="link"
)
],
external_id="1234",
payment=Transfer(
bank_code=choice(["18236120", "60701190"]),
branch_code="{:04}".format(randint(1, 10 ** 4)),
account_number="{:07}".format(randint(1, 10 ** 7)),
name=get_full_name(),
tax_id=TaxIdGenerator.taxId(),
),
payment_type="transfer",
signers=generateExampleSignersJson(n=randint(1, 3)),
external_id=str(uuid4()),
street_line_1="Rua ABC",
street_line_2="Ap 123",
district="Jardim Paulista",
Expand All @@ -44,57 +46,83 @@ def _generateCreditNote():
)


def _generate_payment(payment_type="transfer"):
return {
"transfer": _generateTransfer(),
}[payment_type]
def generateExampleCreditNoteJson(n=1, nominal_amount=None):
credit_notes = []
for _ in range(n):
note = _generateCreditNote()

note_nominal_amount = randint(100000, 1000000)
if nominal_amount is not None:
note_nominal_amount = int(nominal_amount)

def _generateTransfer():
return Transfer(
bank_code="00000000",
branch_code="1234",
account_number="129340-1",
name="Jamie Lannister",
tax_id="012.345.678-90",
)
note.nominal_amount = note_nominal_amount
note.scheduled = randomFutureDate(days=600)

note.invoices = generateExampleInvoiceJson(n=randint(3, 4), note_nominal_amount=note.nominal_amount // 2,
note_scheduled=note.scheduled)
credit_notes.append(note)
return credit_notes

def generateExampleCreditNoteJson(n=1, nominal_amount=None):
def generateExampleCreditNoteIsoDatetimeJson(n=1, nominal_amount=None):
credit_notes = []
for _ in range(n):
note = deepcopy(_generateCreditNote())

note.name = get_full_name()
note.tax_id = TaxIdGenerator.taxId()
note = _generateCreditNote()

note_nominal_amount = randint(100000, 1000000)
if nominal_amount is not None:
note_nominal_amount = int(nominal_amount)

note.nominal_amount = note_nominal_amount
note.scheduled = randomFutureDatetime(days=600)

note.invoices = generateExampleInvoiceJson(n=randint(3, 4), note_nominal_amount=note.nominal_amount // 2,
note.scheduled = randomFutureDatetime(days=600)
note.invoices = generateExampleInvoiceDatetimeJson(n=randint(3, 4), note_nominal_amount=note.nominal_amount // 2,
note_scheduled=note.scheduled)
note.signers = generateExampleSignersJson(n=randint(1, 3))

note.payment = Transfer(
bank_code=choice(["18236120", "60701190"]),
branch_code="{:04}".format(randint(1, 10 ** 4)),
account_number="{:07}".format(randint(1, 10 ** 7)),
name=get_full_name(),
tax_id=TaxIdGenerator.taxId(),
)
note.payment_type = "transfer"
credit_notes.append(note)
return credit_notes

note.external_id = str(uuid4())
def generateExampleCreditNoteStringDatesJson(n=1, nominal_amount=None):
"""
Generate a list of credit notes with string dates.
Note: This function does not overwrite `schedule` and `Invoice.due`
dates in the CreditNote object.
"""
credit_notes = []
for _ in range(n):
note = _generateCreditNote()

credit_notes.append(note)
return credit_notes


def generateExampleInvoiceJson(n=1, note_nominal_amount=0, note_scheduled=datetime.now()):
def generateExampleInvoiceJson(n=1, note_nominal_amount=0, note_scheduled=date.today()):
invoices = []

for _ in range(n):
invoices.append(Invoice(
due=randomDateBetween(note_scheduled + timedelta(days=500), note_scheduled + timedelta(days=1000)),
amount=randint(note_nominal_amount, note_nominal_amount + 100000),
descriptions=[Description(key="taxes", value="RS1000")],
))

return invoices


def generateExampleInvoiceStringDateJson(n=1, note_nominal_amount=0, note_scheduled=datetime.now()):
invoices = []

for _ in range(n):
date = randomDatetimeBetween(note_scheduled + timedelta(days=500), note_scheduled + timedelta(days=1000))
invoices.append(Invoice(
due=datetime.strftime(date, "%Y-%m-%d"),
amount=randint(note_nominal_amount, note_nominal_amount + 100000),
descriptions=[Description(key="taxes", value="RS1000")],
))

return invoices


def generateExampleInvoiceDatetimeJson(n=1, note_nominal_amount=0, note_scheduled=datetime.now()):
invoices = []

for _ in range(n):
Expand Down
3 changes: 3 additions & 0 deletions tests/utils/date.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from random import randint
from datetime import datetime, timedelta, date

def futureDateTime(days=7):
return datetime.now() + timedelta(days=days)


def futureDate(days=7):
return date.today() + timedelta(days=days)
Expand Down