public class RelationCollection : IEnumerable<Relation>,
IEnumerable
Public Class RelationCollection
Implements IEnumerable(Of Relation), IEnumerable
type RelationCollection =
class
interface IEnumerable<Relation>
interface IEnumerable
end
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Firefly.Box;
using Firefly.Box.Testing;
namespace TestFirefly.Box.Documentation
{
[TestFixture]
public class RelationTypes
{
[Test]
public void Find()
{
var employees = new Pubs.Employees();
var jobs = new Pubs.Jobs();
employees.InitializeWithTestData();
jobs.InitializeWithTestData();
DeleteJobBusinessOperationsManager();
var bp = new BusinessProcess
{
From = employees
};
bp.Relations.Add(jobs, RelationType.Find,//The default Relation Type
jobs.Id.IsEqualTo(employees.JobId));
string namesAndJobs = "";
bp.ForEachRow(() =>
{
namesAndJobs += employees.LastName.Value.TrimEnd() + " - " +
jobs.Description.Value.TrimEnd() + "\n";
if (bp.Counter == 5)
bp.Exit();
});
namesAndJobs.ShouldBe("Cruz - Productions Manager\n" +
"Devon - \n" +//This row in the job entity was not found
"Roulet - Managing Editor\n" +
"Domingues - Public Relations Manager\n" +
"Hernadez - Publisher\n");
}
[Test]
public void Insert()
{
var jobs = new Pubs.Jobs();
jobs.InitializeWithTestData();
jobs.CountRows().ShouldBe(14);
var bp = new BusinessProcess();
bp.Relations.Add(jobs, RelationType.Insert);
bp.ForFirstRow(() =>
{
//Settings the value for the new relation
jobs.Id.Value = 15;
jobs.Description.Value = "Software Developer";
});
jobs.CountRows().ShouldBe(15);//Now there are 15 jobs, as the 15th job was inserted
}
[Test]
public void InsertIfNotFound()
{
var employees = new Pubs.Employees();
var jobs = new Pubs.Jobs();
employees.InitializeWithTestData();
jobs.Truncate();//Truncating the jobs table, so it'll be
//recreated using InsertIfNotFound
string employeeAndJobs = "";
var bp = new BusinessProcess();
bp.From = employees;
var relationToJobs = bp.Relations.Add(jobs, RelationType.InsertIfNotFound,
jobs.Id.IsEqualTo(employees.JobId));
bp.ForEachRow(() =>
{
if (!relationToJobs.RowFound)
{
jobs.Id.Value = employees.JobId;
jobs.Description.Value = "Job " + jobs.Id;
}
employeeAndJobs += employees.LastName.ToString().TrimEnd()
+ " job "
+ employees.JobId.ToString().Trim() + " " +
(relationToJobs.RowFound ? "Exists" : "Doesn't Exist") + "\n";
if (bp.Counter == 15)
bp.Exit();
});
employeeAndJobs.ShouldBe(
"Cruz job 10 Doesn't Exist\n" + //At this time job 10 doesn't exist
"Devon job 3 Doesn't Exist\n" +
"Roulet job 6 Doesn't Exist\n" +
"Domingues job 8 Doesn't Exist\n" +
"Hernadez job 5 Doesn't Exist\n" +
"Schmitt job 13 Doesn't Exist\n" +
"Tonini job 11 Doesn't Exist\n" +
"Roel job 6 Exists\n" +//Because job 6 was already inserted on row 3, it exists here
"Lincoln job 14 Doesn't Exist\n" +
"Chang job 4 Doesn't Exist\n" +
"Thomas job 9 Doesn't Exist\n" +
"Nagy job 7 Doesn't Exist\n" +
"Snyder job 12 Doesn't Exist\n" +
"Bennett job 12 Exists\n" +//Was added on the previous row
"Labrune job 5 Exists\n"//was added on the 5th row
);
}
[Test]
public void Join()
{
var employees = new Pubs.Employees();
var jobs = new Pubs.Jobs();
employees.InitializeWithTestData();
jobs.InitializeWithTestData();
DeleteJobBusinessOperationsManager();
var bp = new BusinessProcess
{
From = employees
};
bp.OrderBy.Segments.Add(employees.Id);
bp.Relations.Add(jobs, RelationType.Join,
jobs.Id.IsEqualTo(employees.JobId));
string namesAndJobs = "";
bp.ForEachRow(() =>
{
namesAndJobs += employees.LastName.Value.TrimEnd() + " - " +
jobs.Description.Value.TrimEnd() + "\n";
if (bp.Counter == 5)
bp.Exit();
});
namesAndJobs.ShouldBe("Cruz - Productions Manager\n" +
"Roulet - Managing Editor\n" +//Devon was not written as his job (5) was deleted
"Domingues - Public Relations Manager\n" +
"Hernadez - Publisher\n"+
"Schmitt - Sales Representative\n");
}
[Test]
public void OuterJoin()
{
var employees = new Pubs.Employees();
var jobs = new Pubs.Jobs();
employees.InitializeWithTestData();
jobs.InitializeWithTestData();
DeleteJobBusinessOperationsManager();
var bp = new BusinessProcess
{
From = employees
};
bp.OrderBy.Segments.Add(employees.Id);
bp.Relations.Add(jobs, RelationType.OuterJoin,
jobs.Id.IsEqualTo(employees.JobId));
string namesAndJobs = "";
bp.ForEachRow(() =>
{
namesAndJobs += employees.LastName.Value.TrimEnd() + " - " +
jobs.Description.Value.TrimEnd() + "\n";
if (bp.Counter == 5)
bp.Exit();
});
namesAndJobs.ShouldBe("Cruz - Productions Manager\n" +
"Roulet - Managing Editor\n" +
"Devon - \n" +
"Domingues - Public Relations Manager\n" +
"Hernadez - Publisher\n");
}
void DeleteJobBusinessOperationsManager()
{
var jobs = new Pubs.Jobs();
var bp = new BusinessProcess()
{
From = jobs,
Activity = Activities.Delete
};
bp.Where.Add(jobs.Id.IsEqualTo(3));
bp.Run();
}
}
}
Add(Entity) | Adds a relation toe this collection |
Add(Relation) | Adds a relation to the collection |
Add(Entity, FilterBase) | Adds a relation toe this collection |
Add(Entity, RelationType) | Adds a relation toe this collection |
Add(Entity, Sort) | Adds a relation toe this collection |
Add(Entity, FilterBase, Sort) | Adds a relation toe this collection |
Add(Entity, RelationType, FilterBase) | Adds a relation toe this collection |
Add(Entity, RelationType, Sort) | Adds a relation toe this collection |
Add(Entity, RelationType, FilterBase, Sort) | Adds a relation toe this collection |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) |
GetEnumerator | Returns an enumerator that iterates through a collection. |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) |
ToString | Returns a string that represents the current object. (Inherited from Object) |
ShouldBeArray | (Defined by Should) |