Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
[Refact] Slotted every class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 30, 2016
1 parent 4c137d3 commit 4e37bf4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
27 changes: 20 additions & 7 deletions plume/plume.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def to_csv(values, bracket=False):


class Clause(deque):

__slots__ = ()

def __init__(self, value=None):
if value is not None:
self.append(value)
Expand All @@ -125,6 +126,7 @@ def __str__(self):


class Criterion:
__slots__= ('field', 'operator', 'value')

def __init__(self, field, operator, value):
self.field = field
Expand All @@ -148,6 +150,7 @@ class QuerySet:
clause. The user is allowed to add dynamically several criteria on a QuerySet. The QuerySet only
hit the database when it is iterated over or sliced.
"""
__slots__ = ('_model', '_tables', '_fields', '_clause', '_count', '_offset')

def __init__(self, model):
self._model = model
Expand Down Expand Up @@ -263,10 +266,10 @@ def __getitem__(self, key):


class Manager:

__slots__ = ('_model',)

def __init__(self, model):
self._model = model
self._queryset = None

def __get__(self, instance, owner):
# A Model Manager is only accessible as a class attribute.
Expand All @@ -292,7 +295,8 @@ def filter(self, *args):


class RelatedManager(Manager):

__slots__ = ()

def __get__(self, instance, owner):
# A RelatedManager is only accessible as an instance attribute.
if instance is not None:
Expand Down Expand Up @@ -339,6 +343,7 @@ def __new__(cls, clsname, bases, attrs):


class BaseField:
__slots__ = ('value', 'name', 'required', 'unique', 'default')

def __init__(self, required=True, unique=False, default=None):
self.value = None
Expand All @@ -358,7 +363,8 @@ def is_valid(self, value):
return True


class Field(BaseField):
class Field(BaseField):
__slots__ = ()
internal_type = None
sqlite_datatype = None

Expand Down Expand Up @@ -404,6 +410,7 @@ def sql(self):


class TextField(Field):
__slots__ = ()
internal_type = str
sqlite_datatype = SQLiteAPI.TEXT

Expand All @@ -424,7 +431,8 @@ def __lshift__(self, other):


class NumericField(Field):

__slots__ = ()

def __eq__(self, other):
if self.is_valid(other):
return Criterion(self.name, SQLiteAPI.EQ, other)
Expand Down Expand Up @@ -457,16 +465,20 @@ def __lshift__(self, other):


class IntegerField(NumericField):
__slots__ = ()
internal_type = int
sqlite_datatype = SQLiteAPI.INTEGER


class FloatField(NumericField):
__slots__ = ()
internal_type = float
sqlite_datatype = SQLiteAPI.REAL


class PrimaryKeyField(IntegerField):
__slots__ = ()

def __init__(self, **kwargs):
kwargs.update(required=False)
super().__init__(**kwargs)
Expand All @@ -476,6 +488,7 @@ def sql(self):


class ForeignKeyField(IntegerField):
__slots__ = ()

def __init__(self, related_model, related_field):
super().__init__()
Expand All @@ -502,7 +515,6 @@ def _to_sql(self):


class Model(metaclass=BaseModel):

pk = PrimaryKeyField()

def __init__(self, **kwargs):
Expand All @@ -527,6 +539,7 @@ def __eq__(self, other):


class Database:
__slots__ = ('db_name', '_connection')

def __init__(self, db_name):
self.db_name = db_name
Expand Down
5 changes: 5 additions & 0 deletions tests/test_clause.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import plume
from plume.plume import Clause
from utils import Pokemon

import pytest
Expand Down Expand Up @@ -43,3 +44,7 @@ def test_bracket_has_higher_precedence_than_and_operator(self):
)
expected = "level > 18 AND ( name = 'Charamander' OR name = 'Bulbasaur' )"
assert result == expected

def test_is_slotted(self):
with pytest.raises(AttributeError):
Clause().__dict__
10 changes: 10 additions & 0 deletions tests/test_criterion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from plume.plume import Criterion, Field

import pytest


class TestCriterion:

def test_is_slotted(self):
with pytest.raises(AttributeError):
Criterion(Field, '==', 'value').__dict__
4 changes: 4 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def test_all_model_fields_match_table_fields(self):
for model_field in ['pk', 'name', 'age']:
assert model_field in db_fields

def test_is_slotted(self):
with pytest.raises(AttributeError):
self.db.__dict__

def teardown_method(self):
try:
os.remove(DB_NAME)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from plume.plume import (BaseField, Field, FloatField, IntegerField, PrimaryKeyField, TextField)

import pytest


class TestBaseField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
BaseField().__dict__


class TestField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
Field().__dict__


class TestFloatField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
FloatField().__dict__


class TestIntegerField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
IntegerField().__dict__


class TestPrimaryKeyField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
PrimaryKeyField().__dict__


class TestTextField:

def test_is_slotted(self):
with pytest.raises(AttributeError):
TextField().__dict__


7 changes: 6 additions & 1 deletion tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from plume.plume import Manager, Model

import pytest


class TestManagerAPI:
pass

def test_is_slotted(self):
with pytest.raises(AttributeError):
Manager(Model).__dict__
5 changes: 5 additions & 0 deletions tests/test_queryset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plume import *
from plume.plume import QuerySet
from utils import DB_NAME, Trainer

import pytest
Expand Down Expand Up @@ -41,6 +42,10 @@ def test_output_queryset_as_string(self):
result = str(Trainer.objects.filter(Trainer.age > 18, Trainer.name != 'Giovanni'))
expected = "(SELECT * FROM trainer WHERE name != 'Giovanni' AND age > 18)"
assert result == expected

def test_is_slotted(self):
with pytest.raises(AttributeError):
QuerySet(Model).__dict__


class TestQuerySetSlice(Base):
Expand Down

0 comments on commit 4e37bf4

Please sign in to comment.