Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added some tests for the remove column functionality

  • Loading branch information...
commit 1b28998917894da17fb0911ff69d313bc6b6004b 1 parent a8f0efa
Nick Berardi nberardi authored
BIN  lib/NuGet.exe
View
Binary file not shown
2  src/FluentCassandra.csproj
View
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
24 src/FluentColumnList`1.cs
View
@@ -85,6 +85,28 @@ public void Insert(int index, T item)
OnColumnMutated(MutationType.Changed, item);
}
+ public void Set(int index, T item)
+ {
+ if (index >= Columns.Count)
+ throw new ArgumentOutOfRangeException("index");
+
+ var oldItem = Columns[index];
+ var mutationType = MutationType.Added;
+
+ item.SetParent(Parent);
+ Columns[index] = item;
+
+ if (oldItem != null)
+ {
+ if (oldItem.ColumnName == item.ColumnName)
+ mutationType = MutationType.Changed;
+ else
+ OnColumnMutated(MutationType.Removed, oldItem);
+ }
+
+ OnColumnMutated(mutationType, item);
+ }
+
/// <summary>
///
/// </summary>
@@ -105,7 +127,7 @@ public void RemoveAt(int index)
public T this[int index]
{
get { return Columns[index]; }
- set { Columns.Insert(index, value); }
+ set { Set(index, value); }
}
/// <summary>
6 src/FluentMutation.cs
View
@@ -9,6 +9,7 @@ public class FluentMutation
/// </summary>
internal FluentMutation()
{
+ Timestamp = DateTimeOffset.UtcNow;
}
/// <summary>
@@ -20,5 +21,10 @@ internal FluentMutation()
///
/// </summary>
public IFluentBaseColumn Column { get; set; }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public DateTimeOffset Timestamp { get; private set; }
}
}
18 src/FluentRecord.cs
View
@@ -27,22 +27,6 @@ public FluentRecord()
public abstract IList<T> Columns { get; }
/// <summary>
- /// Checks to see if a column exists.
- /// </summary>
- public virtual bool ColumnExists(string name)
- {
- return ColumnExists(name, StringComparison.InvariantCultureIgnoreCase);
- }
-
- /// <summary>
- /// Checks to see if a column exists.
- /// </summary>
- public virtual bool ColumnExists(string name, StringComparison comparison)
- {
- return Columns.Any(c => String.Equals(c.ColumnName, name, comparison));
- }
-
- /// <summary>
///
/// </summary>
/// <returns></returns>
@@ -165,7 +149,7 @@ public void RemoveColumn(object name)
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
if (col != null)
- OnColumnMutated(MutationType.Removed, col);
+ Columns.Remove(col);
}
protected void ResetMutation()
4 src/Operations/BatchMutate.cs
View
@@ -32,13 +32,13 @@ public override Void Execute(CassandraContext context)
.Where(m => m.Type == MutationType.Removed && m.Column.GetParent().SuperColumn != null);
foreach (var superColumn in superColumnsNeedingDeleted.GroupBy(x => x.Column.GetParent().SuperColumn.ColumnName))
- columnFamilyMutations.Add(Helper.CreateDeletedSuperColumnMutation(superColumn));
+ columnFamilyMutations.AddRange(Helper.CreateDeletedSuperColumnMutation(superColumn));
var columnsNeedingDeleted = columnFamily
.Where(m => m.Type == MutationType.Removed && m.Column.GetParent().SuperColumn == null);
if (columnsNeedingDeleted.Count() > 0)
- columnFamilyMutations.Add(Helper.CreateDeletedColumnMutation(columnsNeedingDeleted));
+ columnFamilyMutations.AddRange(Helper.CreateDeletedColumnMutation(columnsNeedingDeleted));
keyMutations.Add(columnFamily.Key, columnFamilyMutations);
}
47 src/Operations/Helper.cs
View
@@ -58,34 +58,37 @@ public static FluentColumn<CompareWith> ConvertColumnToFluentColumn<CompareWith>
return superCol;
}
- public static Mutation CreateDeletedColumnMutation(IEnumerable<FluentMutation> mutation)
+ public static IEnumerable<Mutation> CreateDeletedColumnMutation(IEnumerable<FluentMutation> mutation)
{
- var columnNames = mutation.Select(m => m.Column.ColumnName).ToList();
-
- var deletion = new Deletion {
- Timestamp = DateTimeOffset.Now.ToTimestamp(),
- Predicate = CreateSlicePredicate(columnNames)
- };
+ foreach (var col in mutation)
+ {
+ var deletion = new Deletion {
+ Timestamp = col.Timestamp.ToTimestamp(),
+ Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
+ };
- return new Mutation {
- Deletion = deletion
- };
+ yield return new Mutation {
+ Deletion = deletion
+ };
+ }
}
- public static Mutation CreateDeletedSuperColumnMutation(IEnumerable<FluentMutation> mutation)
+ public static IEnumerable<Mutation> CreateDeletedSuperColumnMutation(IEnumerable<FluentMutation> mutation)
{
- var superColumn = mutation.Select(m => m.Column.GetPath().SuperColumn.ColumnName).FirstOrDefault();
- var columnNames = mutation.Select(m => m.Column.ColumnName).ToList();
+ foreach (var col in mutation)
+ {
+ var superColumn = col.Column.GetPath().SuperColumn.ColumnName;
- var deletion = new Deletion {
- Timestamp = DateTimeOffset.Now.ToTimestamp(),
- Super_column = superColumn,
- Predicate = CreateSlicePredicate(columnNames)
- };
+ var deletion = new Deletion {
+ Timestamp = col.Timestamp.ToTimestamp(),
+ Super_column = superColumn,
+ Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
+ };
- return new Mutation {
- Deletion = deletion
- };
+ yield return new Mutation {
+ Deletion = deletion
+ };
+ }
}
public static Mutation CreateInsertedOrChangedMutation(FluentMutation mutation)
@@ -141,7 +144,7 @@ public static ColumnOrSuperColumn CreateColumnOrSuperColumn(IFluentBaseColumn co
}
}
- public static SlicePredicate CreateSlicePredicate(List<CassandraType> columnNames)
+ public static SlicePredicate CreateSlicePredicate(IEnumerable<CassandraType> columnNames)
{
return new SlicePredicate {
Column_names = columnNames.Cast<byte[]>().ToList()
138 test/FluentCassandra.Tests/FluentColumnFamilyTest.cs
View
@@ -148,5 +148,143 @@ public void Dynamic_Mutation()
Assert.AreSame(actual, mut1.Column.GetParent().ColumnFamily);
Assert.AreSame(actual, mut2.Column.GetParent().ColumnFamily);
}
+
+ [Test]
+ public void Mutation_Added()
+ {
+ // arrange
+ var col1 = new FluentColumn<AsciiType> { ColumnName = "Test1", ColumnValue = 300M };
+ var col2 = new FluentColumn<AsciiType> { ColumnName = "Test2", ColumnValue = "Hello" };
+
+ // act
+ var actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Columns.Add(col1);
+ actual.Columns.Add(col2);
+
+ // assert
+ var mutations = actual.MutationTracker.GetMutations();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(2, mutations.Count(x => x.Type == MutationType.Added));
+ }
+
+ [Test]
+ public void Mutation_Changed()
+ {
+ // arrange
+ var col1 = new FluentColumn<AsciiType> { ColumnName = "Test1", ColumnValue = 300M };
+ var col2 = new FluentColumn<AsciiType> { ColumnName = "Test1", ColumnValue = "Hello" };
+
+ // act
+ var actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Columns.Add(col1);
+ actual.Columns[0] = col2;
+
+ // assert
+ var mutations = actual.MutationTracker.GetMutations().ToList();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(MutationType.Added, mutations[0].Type);
+ Assert.AreEqual(MutationType.Changed, mutations[1].Type);
+ }
+
+
+ [Test]
+ public void Mutation_Replaced()
+ {
+ // arrange
+ var col1 = new FluentColumn<AsciiType> { ColumnName = "Test1", ColumnValue = 300M };
+ var col2 = new FluentColumn<AsciiType> { ColumnName = "Test2", ColumnValue = "Hello" };
+
+ // act
+ var actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Columns.Add(col1);
+ actual.Columns[0] = col2;
+
+ // assert
+ var mutations = actual.MutationTracker.GetMutations().ToList();
+
+ Assert.AreEqual(3, mutations.Count());
+ Assert.AreEqual(MutationType.Added, mutations[0].Type);
+ Assert.AreEqual(MutationType.Removed, mutations[1].Type);
+ Assert.AreEqual(MutationType.Added, mutations[2].Type);
+ }
+
+ [Test]
+ public void Mutation_Removed()
+ {
+ // arrange
+ var col1 = new FluentColumn<AsciiType> { ColumnName = "Test1", ColumnValue = 300M };
+
+ // act
+ var actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Columns.Add(col1);
+ actual.RemoveColumn("Test1");
+
+ // assert
+ var mutations = actual.MutationTracker.GetMutations().ToList();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(MutationType.Added, mutations[0].Type);
+ Assert.AreEqual(MutationType.Removed, mutations[1].Type);
+ }
+
+ [Test]
+ public void Dynamic_Mutation_Added()
+ {
+ // arrange
+ var colValue1 = 300M;
+ var colValue2 = "Hello";
+
+ // act
+ dynamic actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Test1 = colValue1;
+ actual.Test2 = colValue2;
+
+ // assert
+ var mutations = ((IFluentRecord)actual).MutationTracker.GetMutations();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(2, mutations.Count(x => x.Type == MutationType.Added));
+ }
+
+ [Test]
+ public void Dynamic_Mutation_Changed()
+ {
+ // arrange
+ var colValue1 = 300M;
+ var colValue2 = "Hello";
+
+ // act
+ dynamic actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Test1 = colValue1;
+ actual.Test1 = colValue2;
+
+ // assert
+ var mutations = ((IFluentRecord)actual).MutationTracker.GetMutations().ToList();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(MutationType.Added, mutations[0].Type);
+ Assert.AreEqual(MutationType.Changed, mutations[1].Type);
+ }
+
+ [Test]
+ public void Dynamic_Mutation_Removed()
+ {
+ // arrange
+ var colValue1 = 300M;
+
+ // act
+ dynamic actual = new FluentColumnFamily<AsciiType>("Keyspace1", "Standard1");
+ actual.Test1 = colValue1;
+ actual.RemoveColumn("Test1");
+
+ // assert
+ var mutations = ((IFluentRecord)actual).MutationTracker.GetMutations().ToList();
+
+ Assert.AreEqual(2, mutations.Count());
+ Assert.AreEqual(MutationType.Added, mutations[0].Type);
+ Assert.AreEqual(MutationType.Removed, mutations[1].Type);
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.