Skip to content

Commit

Permalink
Added tests for events
Browse files Browse the repository at this point in the history
  • Loading branch information
Digant C Kasundra committed May 14, 2015
1 parent 0f2aa65 commit 223423e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 12 deletions.
44 changes: 43 additions & 1 deletion hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,49 @@ class Event(Model):
Integer, ForeignKey("event_types.id"), nullable=False, index=True
)
event_type = relationship(EventType, lazy="joined", backref="events")
note = Column(String(length=1024))
note = Column(String(length=1024), nullable=True)

@classmethod
def create(
cls, session,
host, user, event_type, note=None
):
"""Log an Event
Args:
host: the host to which this event pertains
user: the user that created this event, if manually created
event_type: the EventType of this event
note: the optional note to be made about this event
Returns:
a newly created Event
"""
if host is None:
raise exc.ValidationError(
"Host cannot be null for an event"
)
if event_type is None:
raise exc.ValidationError(
"EventType cannot be null for an event"
)
if user is None:
raise exc.ValidationError(
"A user name must be specified for an event"
)

try:
obj = cls(
host=host, user=user, event_type=event_type, note=note
)
obj.add(session)
session.flush()

except Exception:
session.rollback()
raise

return obj


class Achievement(Model):
Expand Down
54 changes: 54 additions & 0 deletions tests/model_tests/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest
from sqlalchemy.exc import IntegrityError

from hermes import exc
from hermes import models

from .fixtures import db_engine, session, sample_data1


def test_creation(sample_data1):
event_types = sample_data1.query(models.EventType).all()
assert len(event_types) == 6
event_type1 = event_types[0]

hosts = sample_data1.query(models.Host).all()
assert len(hosts) == 3
host = hosts[0]

models.Event.create(
sample_data1, host, "testman", event_type1, note="This is a test event"
)
sample_data1.commit()

events = sample_data1.query(models.Event).all()

# the total number of events should be 3 now. We care about the new one
assert len(events) == 3
event = events[2]
assert event.id == 3
assert event.host == host
assert event.user == "testman"
assert event.event_type == event_type1
assert event.note == "This is a test event"


def test_duplicate(sample_data1):
event_types = sample_data1.query(models.EventType).all()
assert len(event_types) == 6
event_type1 = event_types[0]

hosts = sample_data1.query(models.Host).all()
assert len(hosts) == 3
host = hosts[0]

models.Event.create(
sample_data1, host, "testman", event_type1, note="This is a test event"
)
sample_data1.commit()

models.Event.create(
sample_data1, host, "testman", event_type1, note="This is another test event"
)
sample_data1.commit()

22 changes: 11 additions & 11 deletions tests/model_tests/test_fates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@

def test_creation(sample_data1):
event_types = sample_data1.query(models.EventType).all()
assert len(event_types) == 4
assert len(event_types) == 6

event_type1 = event_types[2]
event_type2 = event_types[3]
event_type4 = event_types[4]
event_type5 = event_types[5]

models.Fate.create(sample_data1, event_type1, event_type2, "New fate")
models.Fate.create(sample_data1, event_type4, event_type5, "New fate")
sample_data1.commit()

fates = sample_data1.query(models.Fate).all()

# the total number of fates should be 2 now. We care about the new one
assert len(fates) == 2
fate = fates[1]
assert fate.id == 2
assert fate.creation_event_type == event_type1
assert fate.completion_event_type == event_type2
# the total number of fates should be 3 now. We care about the new one
assert len(fates) == 3
fate = fates[2]
assert fate.id == 3
assert fate.creation_event_type == event_type4
assert fate.completion_event_type == event_type5
assert fate.description == "New fate"


def test_duplicate(sample_data1):
event_types = sample_data1.query(models.EventType).all()
assert len(event_types) == 4
assert len(event_types) == 6

event_type1 = event_types[0]
event_type2 = event_types[1]
Expand Down
27 changes: 27 additions & 0 deletions tests/sample_data/sample_data1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ INSERT INTO event_types
VALUES
(4,'system-maintenance','completed','System maintenance completed.');

INSERT INTO event_types
VALUES
(5,'system-shutdown','required','System shutdown required.');

INSERT INTO event_types
VALUES
(6,'system-shutdown','complete','System shutdown complete.');

INSERT INTO fates
VALUES
(1,1,2,'A system that needs a reboot can be cleared by rebooting the machine.');

INSERT INTO fates
VALUES
(2,3,4,'A system that needs a maintenance must be attended to.');

INSERT INTO hosts
VALUES (1, 'example.dropbox.com');

INSERT INTO hosts
VALUES (2, 'sample.dropbox.com');

INSERT INTO hosts
VALUES (3, 'test.dropbox.com');

INSERT INTO events
VALUES (1, 1, "2015-04-31 10:00:00", "system", 1, "example.dropbox.com needs a reboot");

INSERT INTO events
VALUES (2, 1, "2015-05-01 22:34:03", "system", 2, "example.dropbox.com rebooted.");

0 comments on commit 223423e

Please sign in to comment.