Exemplo n.º 1
0
        public void Insert_Poco_With_Enum_Collections_Test()
        {
            Session.Execute(string.Format(PocoWithEnumCollections.DefaultCreateTableCql, "tbl_with_enum_collections"));
            var mapper = new Mapper(Session, new MappingConfiguration().Define(
                                        PocoWithEnumCollections.DefaultMapping.TableName("tbl_with_enum_collections")));
            var collectionValues = new[] { HairColor.Blonde, HairColor.Gray, HairColor.Red };
            var mapValues        = new SortedDictionary <HairColor, TimeUuid>
            {
                { HairColor.Brown, TimeUuid.NewId() },
                { HairColor.Red, TimeUuid.NewId() }
            };
            var expectedCollection = collectionValues.Select(x => (int)x).ToArray();
            var expectedMap        = mapValues.ToDictionary(kv => (int)kv.Key, kv => (Guid)kv.Value);
            var poco = new PocoWithEnumCollections
            {
                Id          = 1000L,
                List1       = new List <HairColor>(collectionValues),
                List2       = collectionValues,
                Array1      = collectionValues,
                Set1        = new SortedSet <HairColor>(collectionValues),
                Set2        = new SortedSet <HairColor>(collectionValues),
                Set3        = new HashSet <HairColor>(collectionValues),
                Dictionary1 = new Dictionary <HairColor, TimeUuid>(mapValues),
                Dictionary2 = mapValues,
                Dictionary3 = new SortedDictionary <HairColor, TimeUuid>(mapValues)
            };

            mapper.Insert(poco);

            var statement = new SimpleStatement("SELECT * FROM tbl_with_enum_collections WHERE id = ?", 1000L);
            var row       = Session.Execute(statement).First();

            Assert.AreEqual(1000L, row.GetValue <long>("id"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("list1"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("list2"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("array1"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("set1"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("set2"));
            Assert.AreEqual(expectedCollection, row.GetValue <IEnumerable <int> >("set3"));
            Assert.AreEqual(expectedMap, row.GetValue <IDictionary <int, Guid> >("map1"));
            Assert.AreEqual(expectedMap, row.GetValue <IDictionary <int, Guid> >("map2"));
            Assert.AreEqual(expectedMap, row.GetValue <IDictionary <int, Guid> >("map3"));

            // BONUS: Attempt insert with null values
            Assert.DoesNotThrow(() => mapper.Insert(new PocoWithEnumCollections {
                Id = 1001L
            }));
        }
Exemplo n.º 2
0
        public void Insert_Poco_With_Enum_Collections()
        {
            string query = null;

            object[] parameters = null;
            var      config     = new MappingConfiguration().Define(PocoWithEnumCollections.DefaultMapping);
            var      mapper     = GetMappingClient(() => TaskHelper.ToTask(RowSet.Empty()), (q, p) =>
            {
                query      = q;
                parameters = p;
            }, config);
            var collectionValues = new[] { HairColor.Blonde, HairColor.Gray };
            var mapValues        = new SortedDictionary <HairColor, TimeUuid>
            {
                { HairColor.Brown, TimeUuid.NewId() },
                { HairColor.Red, TimeUuid.NewId() }
            };
            var expectedCollection = collectionValues.Select(x => (int)x).ToArray();
            var expectedMap        = mapValues.ToDictionary(kv => (int)kv.Key, kv => (Guid)kv.Value);
            var poco = new PocoWithEnumCollections
            {
                Id          = 2L,
                List1       = new List <HairColor>(collectionValues),
                List2       = collectionValues,
                Array1      = collectionValues,
                Set1        = new SortedSet <HairColor>(collectionValues),
                Set2        = new SortedSet <HairColor>(collectionValues),
                Set3        = new HashSet <HairColor>(collectionValues),
                Dictionary1 = new Dictionary <HairColor, TimeUuid>(mapValues),
                Dictionary2 = mapValues,
                Dictionary3 = new SortedDictionary <HairColor, TimeUuid>(mapValues)
            };

            mapper.Insert(poco, false);
            Assert.AreEqual("INSERT INTO tbl1 (id, list1, list2, array1, set1, set2, set3, map1, map2, map3)" +
                            " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", query);

            Assert.AreEqual(
                new object[]
            {
                2L, expectedCollection, expectedCollection, expectedCollection, expectedCollection,
                expectedCollection, expectedCollection, expectedMap, expectedMap, expectedMap
            }, parameters);
        }
Exemplo n.º 3
0
        public void Update_Poco_With_Enum_Collections_Test()
        {
            var expectedCollection = new[] { HairColor.Blonde, HairColor.Gray };
            var expectedMap        = new SortedDictionary <HairColor, TimeUuid>
            {
                { HairColor.Brown, TimeUuid.NewId() },
                { HairColor.Red, TimeUuid.NewId() }
            };
            var collectionValues = expectedCollection.Select(x => (int)x).ToArray();
            var mapValues        =
                new SortedDictionary <int, Guid>(expectedMap.ToDictionary(kv => (int)kv.Key, kv => (Guid)kv.Value));

            var pocoToUpdate = new PocoWithEnumCollections
            {
                Id          = 3000L,
                Dictionary1 = expectedMap.ToDictionary(x => x.Key, x => x.Value),
                Dictionary2 = expectedMap.ToDictionary(x => x.Key, x => x.Value),
                Dictionary3 = expectedMap,
                List1       = expectedCollection.ToList(),
                List2       = expectedCollection.ToList(),
                Set1        = new SortedSet <HairColor>(expectedCollection),
                Set2        = new SortedSet <HairColor>(expectedCollection),
                Set3        = new HashSet <HairColor>(expectedCollection)
            };

            pocoToUpdate.Array1 = new[] { HairColor.Blonde, HairColor.Red, HairColor.Black };
            pocoToUpdate.Dictionary1.Add(HairColor.Black, Guid.NewGuid());
            pocoToUpdate.Dictionary2.Add(HairColor.Black, Guid.NewGuid());
            pocoToUpdate.List1.Add(HairColor.Black);
            pocoToUpdate.Set1.Add(HairColor.Black);
            pocoToUpdate.Set2.Add(HairColor.Black);
            pocoToUpdate.Set3.Add(HairColor.Black);
            const string insertQuery =
                "INSERT INTO tbl_with_enum_collections (id, list1, list2, array1, set1, set2, set3, map1, map2, map3)" +
                " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            _session.Execute(new SimpleStatement(insertQuery, pocoToUpdate.Id, collectionValues, collectionValues,
                                                 collectionValues, collectionValues, collectionValues, collectionValues, mapValues, mapValues,
                                                 mapValues));

            var config =
                new MappingConfiguration().Define(
                    PocoWithEnumCollections.DefaultMapping.TableName("tbl_with_enum_collections"));
            var mapper = new Mapper(_session, config);

            mapper.Update(pocoToUpdate);
            var statement = new SimpleStatement("SELECT * FROM tbl_with_enum_collections WHERE id = ?", pocoToUpdate.Id);

            var row = _session.Execute(statement).First();

            Assert.AreEqual(pocoToUpdate.Id, row.GetValue <long>("id"));
            CollectionAssert.AreEquivalent(pocoToUpdate.List1.Select(x => (int)x).ToList(), row.GetValue <IEnumerable <int> >("list1"));
            CollectionAssert.AreEquivalent(pocoToUpdate.List2.Select(x => (int)x).ToList(), row.GetValue <IEnumerable <int> >("list2"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Array1.Select(x => (int)x).ToArray(), row.GetValue <IEnumerable <int> >("array1"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Set1.Select(x => (int)x), row.GetValue <IEnumerable <int> >("set1"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Set2.Select(x => (int)x), row.GetValue <IEnumerable <int> >("set2"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Set3.Select(x => (int)x), row.GetValue <IEnumerable <int> >("set3"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Dictionary1.ToDictionary(x => (int)x.Key, x => (Guid)x.Value),
                                           row.GetValue <IDictionary <int, Guid> >("map1"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Dictionary2.ToDictionary(x => (int)x.Key, x => (Guid)x.Value),
                                           row.GetValue <IDictionary <int, Guid> >("map2"));
            CollectionAssert.AreEquivalent(pocoToUpdate.Dictionary3.ToDictionary(x => (int)x.Key, x => (Guid)x.Value),
                                           row.GetValue <IDictionary <int, Guid> >("map3"));
        }