Last active
January 25, 2020 20:34
-
-
Save valaparthvi/0c9bc0e8ea57de7c6009dcf2cdb36065 to your computer and use it in GitHub Desktop.
Sentaku Code Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sentaku | |
# Domain object class definition | |
class ObjectCollection(sentaku.modelling.ElementMixiin): | |
create = sentaku.ContextualMethod() | |
ENTITY = ObjectEntity | |
class ObjectEntity(sentaku.modelling.Element): | |
update = sentaku.ContextualMethod() | |
delete = sentaku.ContextualMethod() | |
exists = sentaku.ContextualProperty() | |
class MiqImplementationContext(sentaku.ImplementationContext): | |
""" Our context for Sentaku""" | |
pass | |
class ViaUI(): | |
# context object for UI Implementation | |
pass | |
class ViaREST(): | |
# context object for REST Implementation | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sentaku | |
from main import ObjectCollection, ObjectEntity, ViaREST, MiqImplementationContext | |
@MiqImplementationContext.external_for(ObjectCollection, ViaREST) | |
def create(self, data) | |
response = self.rest_api.action.create(**data) | |
if response.status_code == 200: | |
instance = self.instantiate(**data) | |
return instance | |
else: | |
return response.error | |
@MiqImplementationContext.external_for(ObjectEntity, ViaREST) | |
def update(self, updates): | |
response = self.rest_api.actions.edit(**data) | |
if response.status_code == 200: | |
return True | |
else: | |
return response.error | |
@MiqImplementationContext.external_for(ObjectEntity, ViaREST) | |
def delete(self): | |
response = self.rest_api.actions.delete() | |
if response.status_code == 200: | |
return True | |
else: | |
return response.error | |
@MiqImplementationContext.external_for(ObjectEntity, ViaREST) | |
def exists(self): | |
return self.rest_api.exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from main import ViaUI, ViaREST | |
@pytest.mark.parametrize('context', [ViaUI, ViaREST]) | |
def test_some_entity(some_object, context): | |
with context: | |
# if the context is ViaUI, Sentaku will select UI methods and attributes from ui.py | |
# if the context is ViaREST, Sentaku will select UI methods and attributes from rest.py | |
entity = some_object.create(name='random') | |
with update(entity): | |
entity.description = 'new' | |
entity.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sentaku | |
from main import ObjectCollection, ObjectEntity, ViaUI, MiqImplementationContext | |
@MiqImplementationContext.external_for(ObjectCollection, ViaUI) | |
def create(self, data): # self is the ObjectCollection | |
# Navigate to the "Add" page on UI where the object can be created | |
view = navigate_to(self, "Add") | |
# fill the data in the UI form and click on add button | |
view.fill(**data) | |
view.add_button.click() | |
# instantiate the object and return it | |
instance = self.instantiate(**data) | |
return instance | |
@MiqImplementationContext.external_for(ObjectEntity, ViaUI) | |
def update(self, updated): # self is the ObjectEntity | |
# Navigate to the "Edit" page on UI where the object can be updated | |
view = navigate_to(self, "Edit") | |
view.fill(**updates) | |
view.save_button.click() | |
@MiqImplementationContext.external_for(ObjectCollection, ViaUI) | |
def delete(self): | |
# Navigate to the "Details" page on UI where the object can be updated | |
view = navigate_to(self, "Details") | |
view.config_dropdown.select("Delete the selected item") | |
@MiqImplementationContext.external_for(ObjectCollection, ViaUI) | |
def exists(self): | |
view = navigate_to(self, "Details") | |
if view.is_displayed: | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment