octabytes / FireO

Google Cloud Firestore modern and simplest convenient ORM package in Python. FireO is specifically designed for the Google's Firestore
https://fireo.octabyte.io
Apache License 2.0
249 stars 29 forks source link

Default values not being set #218

Closed gmcdowell closed 10 months ago

gmcdowell commented 10 months ago
class AType(Enum):
    ACTIVE: str = "Active"
    PENDING: str = "Pending"
    CANCELLED: str = "Cancelled"

class AModel(Model):    
    id: IDField(include_in_document=True)
    text: TextField()
    num: NumberField(
        int_only=True,
        default=10,
    )
    bool: BooleanField(default=True)
    type: EnumField(
        enum=AType,
        default=AType.PENDING,
    )

   class Meta:
      collection_name='AModel'

# test case psuedo code
def test_save_default(self):
   model = AModel()
   model.text = "text"
   model.save()

   # auto generated onSave
   self.assertIsNotNone(model.id)

   # default values that 'should be' included
   self.assertEqual(model.num, 10)
   self.assertTrue(model.bool)
   self.assertEqual(model.type, AType.PENDING)

Fails test and throws AttributeError: 'AModel' object has no attribute 'num' (first assertion) Same goes for 'bool' and 'type'

ADR-007 commented 10 months ago

Hi! I see you are using.: instead of =. Option 1:

class AModel(Model):
   # ...
   type = EnumField(
        enum=AType,
        default=AType.PENDING,
    )

Option 2 (using type annotation and TypedModel):

class AModel(TypedModel):
  # ...
  type: AType = AType.PENDING
gmcdowell commented 10 months ago

Ah yeah.. that did the trick Thx heaps eh.