Skip to content
Permalink
Browse files

Tests for Postgres complex arrays with duplicate fields.

Postgres complex arrays seem to have additional issues when used with duplicate fields in results. These two tests will test for that.
  • Loading branch information...
cthames committed Mar 27, 2017
1 parent e9af073 commit f2992aa85333a468f2764e1dd1a6141625d50804
Showing with 105 additions and 2 deletions.
  1. +105 −2 src/ServiceStack.OrmLite.PostgreSQL.Tests/Issues/CustomSqlIssue.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Data;
using NUnit.Framework;
using ServiceStack.DataAnnotations;
@@ -99,6 +99,109 @@ public void test_model_with_array_and_json()
}
}
}

[Alias("my_table")]
public class MyModel
{
public int MyModelId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string NewField { get; set; }
}

public class MyNewModel
{
public int MyModelId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string NewField { get; set; }
public int RenamedId { get; set; }
}

[Test]
public void test_model_with_simple_array_and_duplicate_fields()
{

OrmLiteConfig.DialectProvider = PostgreSqlDialect.Provider;

using (var db = OpenDbConnection())
{

db.DropAndCreateTable<MyModel>();

db.Insert(new MyModel { MyModelId = 100, Name = "Test Name", NewField = "New Field", Type = "My Type" });
db.Insert(new MyModel { MyModelId = 200, Name = "Tester Name 2", NewField = "New Field 2", Type = "My Type 2" });

const string sql = @"
SELECT *, t2.my_model_id AS renamed_id
FROM (
SELECT *
FROM my_table
CROSS JOIN (SELECT ARRAY[1,2,3,4] AS int_array) AS c
) AS t1
INNER JOIN my_table AS t2 ON t1.my_model_id = t2.my_model_id;";

var results = db.Select<MyNewModel>(sql);

Assert.That(results.Count, Is.GreaterThan(1));

foreach (var result in results)
{
Console.WriteLine("{0} - {1} - {2}".Fmt(result.MyModelId, result.Name, result.RenamedId));
Assert.That(result.MyModelId, Is.Not.EqualTo(0));
Assert.That(result.RenamedId, Is.Not.EqualTo(0));
Assert.That(result.Name, Is.Not.Empty);
}

}
}

[Test]
public void test_model_with_complex_array_and_duplicate_fields()
{

OrmLiteConfig.DialectProvider = PostgreSqlDialect.Provider;

using (var db = OpenDbConnection())
{
db.DropAndCreateTable<ColorModel>();

db.Insert(new ColorModel { Color = "red", Value = "#f00" });
db.Insert(new ColorModel { Color = "green", Value = "#0f0" });
db.Insert(new ColorModel { Color = "blue", Value = "#00f" });
db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" });
db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" });
db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" });
db.Insert(new ColorModel { Color = "black", Value = "#000" });

db.DropAndCreateTable<MyModel>();

db.Insert(new MyModel { MyModelId = 100, Name = "Test Name", NewField = "New Field", Type = "My Type" });
db.Insert(new MyModel { MyModelId = 200, Name = "Tester Name 2", NewField = "New Field 2", Type = "My Type 2" });

const string sql = @"
SELECT *, t2.my_model_id AS renamed_id
FROM (
SELECT *
FROM my_table
CROSS JOIN (SELECT array_agg(color.*) AS color_array FROM color) AS c
) AS t1
INNER JOIN my_table AS t2 ON t1.my_model_id = t2.my_model_id;";

var results = db.Select<MyNewModel>(sql);

Assert.That(results.Count, Is.GreaterThan(1));

foreach (var result in results)
{
Console.WriteLine("{0} - {1} - {2}".Fmt(result.MyModelId, result.Name, result.RenamedId));
Assert.That(result.MyModelId, Is.Not.EqualTo(0));
Assert.That(result.RenamedId, Is.Not.EqualTo(0));
Assert.That(result.Name, Is.Not.Empty);
}

}
}
}

}
}

0 comments on commit f2992aa

Please sign in to comment.
You can’t perform that action at this time.