summaryrefslogtreecommitdiff
path: root/test/test-mapping.cpp
blob: 94536386ca028fb86a5c7e014970eca5f761b010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#define BOOST_TEST_MODULE TypeMappers
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

#include "helpers.h"
#include <compileTimeFormatter.h>
#include <output/pq/typeMapper.h>

using M = MyGrate::Output::Pq::TypeMapper::RegexMapper;

namespace std {
	ostream &
	operator<<(ostream & s, const MyGrate::Output::Pq::TypeMapper::ObsRegex & r)
	{
		return s << r.src;
	}
	ostream &
	operator<<(ostream & s, const M & m)
	{
		return MyGrate::scprintf<"{ type: %?, column: %?, target: %? }">(s, m.typeMatch, m.columnMatch, m.targetType);
	}
}

using regex_mapping_test_data = std::tuple<M, std::string_view, std::string_view, std::optional<std::string>>;
using namespace MyGrate::Output::Pq;
BOOST_DATA_TEST_CASE(regex_mapper,
		boost::unit_test::data::make<regex_mapping_test_data>({
				// Types
				{{R"(int\(\d+\))"_r, {}, "int"}, "int(10)", "any", "int"},
				{{R"(int(\(\d+\))?)"_r, {}, "int"}, "int", "any", "int"},
				{{R"(int\(\d+\))"_r, {}, "int"}, "int(1)", "any", "int"},
				{{R"(int\(\d+\))"_r, {}, "int"}, "varchar(1)", "any", {}},
				{{R"(varchar\(\d+\))"_r, {}, ""}, "varchar(1)", "any", "varchar(1)"},
				{{R"(varchar\(\d+\))"_r, {}, ""}, "varchar(10)", "any", "varchar(10)"},
				{{R"(varchar\(\d+\))"_r, {}, ""}, "int(10)", "any", {}},
				{{R"(varchar\(\d{4,}\))"_r, {}, "text"}, "varchar(1024)", "any", "text"},
				// Names
				{{{}, R"(.*\.id)"_r, "bigint"}, "int(10)", "all.id", "bigint"},
				{{{}, R"(.*\.name)"_r, "text"}, "int(10)", "all.name", "text"},
		}),
		m, inType, inCol, exp)
{
	BOOST_CHECK_EQUAL(m.map(inType, inCol), exp);
}

using mapping_test_data = std::tuple<std::string_view, std::string_view>;
BOOST_DATA_TEST_CASE(default_type_mapping,
		boost::unit_test::data::make<mapping_test_data>({
				// https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html
				{"bit(10)", "bit(10)"},
				{"tinyint", "smallint"},
				{"tinyint(1)", "boolean"},
				{"tinyint(2)", "smallint"},
				{"smallint", "smallint"},
				{"smallint(1)", "smallint"},
				{"smallint(2)", "smallint"},
				{"mediumint", "int"},
				{"mediumint(5)", "int"},
				{"int", "int"},
				{"int(10)", "int"},
				{"bigint", "bigint"},
				{"bigint(10)", "bigint"},
				{"tinyint unsigned", "smallint"},
				{"tinyint(1) unsigned", "boolean"},
				{"tinyint(2) unsigned", "smallint"},
				{"smallint unsigned", "smallint"},
				{"smallint(1) unsigned", "smallint"},
				{"smallint(2) unsigned", "smallint"},
				{"mediumint unsigned", "int"},
				{"mediumint(5) unsigned", "int"},
				{"int unsigned", "int"},
				{"int(10) unsigned", "int"},
				{"bigint unsigned", "bigint"},
				{"bigint(10) unsigned", "bigint"},
				{"decimal", "decimal"},
				{"decimal(1,2)", "decimal(1,2)"},
				{"decimal(1)", "decimal(1)"},
				{"numeric", "numeric"},
				{"numeric(1,2)", "numeric(1,2)"},
				{"numeric(1)", "numeric(1)"},
				{"float", "float"},
				{"float(1,2)", "float"},
				{"real", "real"},
				{"real(1,2)", "real"},
				{"double", "real"},
				{"double(1,2)", "real"},
				{"double precision", "real"},
				{"double precision(1,2)", "real"},
				// https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html
				{"date", "date"},
				{"datetime", "timestamp without time zone"},
				{"datetime(1)", "timestamp without time zone"},
				{"timestamp", "timestamp without time zone"},
				{"timestamp(1)", "timestamp without time zone"},
				{"time", "time without time zone"},
				{"time(1)", "time without time zone"},
				{"year", "smallint"},
				{"time(1)", "time without time zone"},
				{"year", "smallint"},
				// https://dev.mysql.com/doc/refman/8.0/en/string-types.html
				{"binary", "bytea"},
				{"binary(10)", "bytea"},
				{"varbinary", "bytea"},
				{"varbinary(10)", "bytea"},
				{"char", "char"},
				{"char(10)", "char(10)"},
				{"varchar", "varchar"},
				{"varchar(10)", "varchar(10)"},
				{"blob", "bytea"},
				{"blob(100)", "bytea"},
				{"tinyblob", "bytea"},
				{"mediumblob", "bytea"},
				{"longblob", "bytea"},
				{"text", "text"},
				{"text(100)", "text"},
				{"tinytext", "text"},
				{"mediumtext", "text"},
				{"longtext", "text"},
				// enum
				// set
				// https://dev.mysql.com/doc/refman/8.0/en/json.html
				{"json", "json"},
		}),
		inType, exp)
{
	TypeMapper tm;
	BOOST_CHECK_EQUAL(tm.map(inType, {}), exp);
}