Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
10f4e09
Smoke Test Sample for Track 2 libraries
JonathanCrd Jul 17, 2019
d6e763c
simpleQuery method added
JonathanCrd Jul 17, 2019
45e9699
Method's names updated
JonathanCrd Jul 17, 2019
1b5ef9a
Create README.md
JonathanCrd Jul 19, 2019
aee43e1
Update README.md
JonathanCrd Jul 19, 2019
97e4f60
Commented lines deleted
JonathanCrd Jul 19, 2019
c587a82
README.md moved to correct folder
JonathanCrd Jul 19, 2019
7909abd
Create requirements.txt
JonathanCrd Jul 22, 2019
1d393e6
Update README.md
JonathanCrd Jul 22, 2019
81d389e
Update README.md
JonathanCrd Jul 23, 2019
ab8fa9d
Imports changed
JonathanCrd Jul 23, 2019
e7f7346
Use of literals instead of append
JonathanCrd Jul 23, 2019
7066da0
Database Name variable to class level.
JonathanCrd Jul 23, 2019
81adc4c
Use of Pythonic with statements
JonathanCrd Jul 23, 2019
4b79c6a
Update requirements.txt
JonathanCrd Jul 23, 2019
a3df531
Revert "Update requirements.txt"
JonathanCrd Jul 23, 2019
27b2a2d
Revert "Use of Pythonic with statements"
JonathanCrd Jul 23, 2019
4c2d73d
Revert "Revert "Use of Pythonic with statements""
JonathanCrd Jul 23, 2019
15e025e
requiriments.txt encoded as a txt file
JonathanCrd Jul 23, 2019
3d9ce90
requirements.txt as text file
JonathanCrd Jul 23, 2019
22927ad
Misspelling in "Key concepts"
JonathanCrd Jul 23, 2019
17a53cb
Update .docsettings.yml to match the tittle of Smoke Test
JonathanCrd Jul 23, 2019
35c6223
Went trought Suyog comments
JonathanCrd Jul 24, 2019
ada5a74
Revert "Went trought Suyog comments"
JonathanCrd Jul 24, 2019
ff419ff
Gone trought Suyog comments
JonathanCrd Jul 24, 2019
e2190c9
use of snake case in file names
JonathanCrd Jul 24, 2019
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
Prev Previous commit
Next Next commit
Revert "Went trought Suyog comments"
This reverts commit 35c6223.
  • Loading branch information
JonathanCrd committed Jul 24, 2019
commit ada5a74b80531d2399872b175001383fcd14050b
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"git.ignoreLimitWarning": true,
"python.testing.pytestArgs": [],
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black"
"python.unitTest.pyTestArgs": [],
"python.unitTest.pyTestEnabled": true
}
64 changes: 38 additions & 26 deletions samples/smoketest/CosmosDB.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from azure.cosmos import CosmosClient
from azure.cosmos.partition_key import PartitionKey
import os

class CosmosDB:
def __init__(self):
URL = os.environ["COSMOS_ENDPOINT"]
KEY = os.environ["COSMOS_KEY"]
self.client = CosmosClient(URL, {"masterKey": KEY})
self.dbName = "pySolarSystem"
self.client = CosmosClient(URL,{'masterKey': KEY})
self.dbName="pySolarSystem"

def CreateDatabase(self):
print("Creating '{0}' database...".format(self.dbName))
Expand All @@ -16,41 +16,53 @@ def CreateDatabase(self):
def CreateContainer(self, db):
collectionName = "Planets"
print("Creating '{0}' collection...".format(collectionName))
partition_key = PartitionKey(path="/id", kind="Hash")
partition_key = PartitionKey(path='/id', kind='Hash')
return db.create_container(id="Planets", partition_key=partition_key)

def CreateDocuments(self, container):
# Cosmos will look for an 'id' field in the items, if the 'id' is not specify Cosmos is going to assing a random key.
planets = [
{
"id": "Earth",
"HasRings": False,
"Radius": 3959,
"Moons": [{"Name": "Moon"}],
},
'id' : "Earth",
'HasRings' : False,
'Radius' : 3959,
'Moons' :
[
{
'Name' : "Moon"
}
]
},
{
"id": "Mars",
"HasRings": False,
"Radius": 2106,
"Moons": [{"Name": "Phobos"}, {"Name": "Deimos"}],
},
]
"id" : "Mars",
"HasRings" : False,
"Radius" : 2106,
"Moons" :
[
{
"Name" : "Phobos"
},
{
"Name" : "Deimos"
}
]
}
]

print("Inserting items in the collection...")
for planet in planets:
container.create_item(planet)
print("\t'{0}' created".format(planet["id"]))
print("\t'{0}' created".format(planet['id']))
print("\tdone")

def SimpleQuery(self, container):
print("Quering the container...")
items = list(
container.query_items(
query="SELECT c.id FROM c", enable_cross_partition_query=True
)
)
items = list(container.query_items(
query="SELECT c.id FROM c",
enable_cross_partition_query = True
))
print("\tdone: {0}".format(items))

def DeleteDatabase(self):
print("Cleaning up the resource...")
self.client.delete_database(self.dbName)
Expand All @@ -66,18 +78,18 @@ def Run(self):
print("3) Insert Documents (items) into the Container")
print("4) Delete Database (Clean up the resource)")
print()

# Ensure that the database does not exists
try:
self.DeleteDatabase()
except:
pass

try:
db = self.CreateDatabase()
container = self.CreateContainer(db=db)
self.CreateDocuments(container=container)
self.SimpleQuery(container=container)
self.SimpleQuery(container=container)
finally:
# if something goes wrong, the resource should be cleaned anyway
self.DeleteDatabase()
self.DeleteDatabase()
39 changes: 13 additions & 26 deletions samples/smoketest/EventHubs.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
import os
from datetime import datetime
from azure.eventhub import EventHubClient, EventData, EventPosition

from datetime import datetime
import os

class EventHub:
def __init__(self):
# This test requires a previusly created Event Hub.
# In this example the name is "myeventhub", but it could be change below
connectionString = os.environ["EVENT_HUBS_CONNECTION_STRING"]
eventHubName = "myeventhub"
self.client = EventHubClient.from_connection_string(
connectionString, eventHubName
)
eventHubName = 'myeventhub'
self.client = EventHubClient.from_connection_string(connectionString, eventHubName)

def GetPartitionIds(self):
print("Getting partitions id...")
partition_ids = self.client.get_partition_ids()
print("\tdone")
return partition_ids
return partition_ids

def SendAndReceiveEvents(self, partitionID):
with self.client.create_consumer(
consumer_group="$default",
partition_id=partitionID,
event_position=EventPosition(datetime.utcnow()),
) as consumer:
with self.client.create_consumer(consumer_group="$default", partition_id=partitionID, event_position=EventPosition(datetime.utcnow())) as consumer:

print("Sending events...")
with self.client.create_producer(partition_id=partitionID) as producer:
event_list = [
EventData(b"Test Event 1 in Python"),
EventData(b"Test Event 2 in Python"),
EventData(b"Test Event 3 in Python"),
]
EventData(b"Test Event 2 in Python"),EventData(b"Test Event 3 in Python")]
producer.send(event_list)
print("\tdone")

print("Receiving events...")
received = consumer.receive(max_batch_size=len(event_list), timeout=2)
for event_data in received:
print("\tEvent Received: " + event_data.body_as_str())

print("\tdone")

if len(received) != len(event_list):
raise Exception(
"Error, expecting {0} events, but {1} were received.".format(
str(len(event_list)), str(len(received))
)
)
if(len(received) != len(event_list)):
raise Exception("Error, expecting {0} events, but {1} were received.".format(str(len(event_list)),str(len(received))))

def Run(self):
print()
Expand All @@ -61,6 +48,6 @@ def Run(self):
print()

partitionID = self.GetPartitionIds()
# In this sample the same partition id is going to be used for the producer and consumer,
# It is the first one, but it could be any (is not relevant as long as it is the same in both producer and consumer)
self.SendAndReceiveEvents(partitionID[0])
#In this sample the same partition id is going to be used for the producer and consumer,
#It is the first one, but it could be any (is not relevant as long as it is the same in both producer and consumer)
self.SendAndReceiveEvents(partitionID[0])
9 changes: 3 additions & 6 deletions samples/smoketest/KeyVaultSecrets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

import os

class KeyVault:
def __init__(self):
Expand All @@ -10,9 +9,7 @@ def __init__(self):
# * AZURE_CLIENT_SECRET
# * AZURE_TENANT_ID
credential = DefaultAzureCredential()
self.secret_client = SecretClient(
vault_url=os.environ["AZURE_PROJECT_URL"], credential=credential
)
self.secret_client = SecretClient(vault_url=os.environ["AZURE_PROJECT_URL"], credential=credential)

def SetSecret(self):
print("Setting a secret...")
Expand Down Expand Up @@ -43,4 +40,4 @@ def Run(self):
self.SetSecret()
self.GetSecret()
finally:
self.DeleteSecret()
self.DeleteSecret()
2 changes: 1 addition & 1 deletion samples/smoketest/Program.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
KeyVault().Run()
StorageBlob().Run()
EventHub().Run()
CosmosDB().Run()
CosmosDB().Run()
22 changes: 9 additions & 13 deletions samples/smoketest/StorageBlobs.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import os
from azure.storage.blob import BlobClient
from azure.core import exceptions

import os

class StorageBlob:
def __init__(self):
connectionString = os.environ["STORAGE_CONNECTION_STRING"]
self.blob = BlobClient.from_connection_string(
connectionString, container="mycontainer", blob="pyTestBlob.txt"
)
self.blob = BlobClient.from_connection_string(connectionString, container="mycontainer", blob="pyTestBlob.txt")

def UploadBLob(self):
print("uploading blob...")
data = "This is a sample data for Python Test"
self.blob.upload_blob(data)
self.data = "This is a sample data for Python Test"
self.blob.upload_blob(self.data)
print("\tdone")

def DownloadBlob(self):
Expand All @@ -37,15 +33,15 @@ def Run(self):
print("2) Download a Blob")
print("3) Delete that Blob (Clean up the resource)")
print()

# Ensure that the blob does not exists before the tests
#Ensure that the blob does not exists before the tests
try:
self.DeleteBlob()
except exceptions.AzureError:
except:
pass

try:
self.UploadBLob()
self.DownloadBlob()
finally:
self.DeleteBlob()
self.DeleteBlob()