Conditions | 7 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import pytest |
||
25 | def test_validation(): |
||
26 | """ |
||
27 | .. test:: Hashing and validation of database actions |
||
28 | :id: T_002 |
||
29 | :tags: gwdbvalidator_plugin; |
||
30 | :links: S_002 |
||
31 | |||
32 | Tests the hashing and validation of existing and newly created database tables. |
||
33 | """ |
||
34 | |||
35 | app = groundwork.App() |
||
36 | validator_plugin = GwDbValidator(app) |
||
37 | validator_plugin.activate() |
||
38 | |||
39 | class My_Plugin(GwSqlPattern): |
||
40 | def __init__(self, app, **kwargs): |
||
41 | self.name = "My_Plugin" |
||
42 | super(My_Plugin, self).__init__(app, **kwargs) |
||
43 | self.db = None |
||
44 | self.Test = None |
||
45 | |||
46 | def activate(self): |
||
47 | self.db = self.app.databases.register("test_db", |
||
48 | "sqlite://", |
||
49 | "database for test values") |
||
50 | |||
51 | class Test(self.db.Base): |
||
52 | __tablename__ = "test" |
||
53 | id = Column(Integer, primary_key=True) |
||
54 | name = Column(String(512), nullable=False, unique=True) |
||
55 | |||
56 | self.Test = self.db.classes.register(Test) |
||
57 | self.db.create_all() |
||
58 | |||
59 | test_plugin = My_Plugin(app) |
||
60 | test_plugin.activate() |
||
61 | |||
62 | test_entry_1 = test_plugin.Test(name="blub") |
||
63 | test_plugin.db.add(test_entry_1) |
||
64 | test_plugin.db.commit() |
||
65 | |||
66 | assert test_plugin.Test.query.count() == 1 |
||
67 | |||
68 | hash_db = app.databases.get("hash_db") |
||
69 | hash_db_model = hash_db.classes.get("Hashes") |
||
70 | assert hash_db is not None |
||
71 | assert hash_db_model is not None |
||
72 | assert hash_db_model.query.count() == 1 |
||
73 | test_plugin.Test.query.all() |
||
74 | |||
75 | test_entry_2 = test_plugin.Test(name="Yehaa") |
||
76 | test_plugin.db.add(test_entry_2) |
||
77 | test_plugin.db.commit() |
||
78 | assert hash_db_model.query.count() == 2 |
||
79 | |||
80 | # Let's change data without triggering the sqlalchemy events, so that the hash gets not updated. |
||
81 | test_plugin.db.engine.execute("UPDATE test SET name='not_working' WHERE id=1") |
||
82 | |||
83 | # Now tell sqlalchemy to refresh/reload the object |
||
84 | with pytest.raises(ValidationError): |
||
85 | test_plugin.db.session.refresh(test_entry_1) |
||
86 | |||
87 | # Let's change our object the normal way and add the changes to the db. |
||
88 | test_entry_1.name = "Boohaaa" |
||
89 | test_plugin.db.add(test_entry_1) |
||
90 | test_plugin.db.commit() |
||
91 | test_plugin.db.session.refresh(test_entry_1) |
||
92 |